|
|
C: String I/O Part 2
|
|
|
|
Junior Member
Join Date: Jul 2002
Location: Australia
Status:
Offline
|
|
Hey all thanks for the quick replies to part 1 of my post. Though it seems that I have stumbled onto another problem regarding strings
How can I insert this 1 character into another string. I thought this would work:
Code:
#include <stdio.h>
int main()
{
FILE *fp;
char fileOutput[255],test[255];
int i;
fp = fopen( "data.dat", "rt" );
while( !feof(fp) ) {
fgets( fileOutput, 255, fp );
sscanf( fileOutput, "%.1s", test );
printf("%s\n",test);
}
fclose(fp);
return 0;
}
Though the above code doesn't work
(
Last edited by Syphor; Mar 5, 2004 at 05:12 AM.
)
|
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
I'm not sure I understand what you are trying to do. Are you trying to build up a string in test? Because if so, continually copying into it with sscanf will just keep overwriting what you did before. You'd need to increment a counter with the offset into the string (i.e. so that you're copying into test + counter).
At any rate, I don't think it's meaningful to scan a string with a length of 0.1.
And also, sscanf isn't really the best way to copy a string. You'd probably be better off looking at strncpy and strcat, though I'm still not entirely sure what you're trying to do.
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Feb 2000
Location: Nashua NH, USA
Status:
Offline
|
|
strstr or strchr to find the offset
then strncpy the front then your insert then strcat
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|