Reputation: 21
I pretty much have the whole code figured it, the problem is that the file has names, some names include the middle name some don't. each name in the file is in a new line
Lincoln, Abraham
Obama, Barak Hussien
Doe, John
now my problem with this is I originally had two arrays reading in each name with fscanf but it reads Hussien as a new line? So then I try to do three arrays for the middle name as well, but when it read in the file it displayed the output as.
Lincoln, Abraham Obama
Barak Hussien Doe
John
while ( fscanf( input,"%s %s", &last[i],&first[i] ) != EOF )
{ i++; }
what am I doing wrong? I would like to scan the whole line in but in another part of the program I must have first and last name seperated as "user ids" will be created ex. a_lincoln, etc.
Upvotes: 1
Views: 4824
Reputation: 2172
Read a line from file using
while ( fscanf( input,"%[^\n]s", line) != EOF )
"line could be character array or char pointer"
now you have complete line from the file
Upvotes: 0
Reputation: 1454
Use "%[^,], %[^\r\n]"
as the format string in your fscanf
. It ignores the colon for the last name and read the remaining characters in the same line for the first name.
Upvotes: 2
Reputation: 8065
In the format string for fscanf or scanf or sscanf, each instance of whitespace causes the function to read and discard any whitespace that appears in the input at that place, including newline characters. So fscanf isn't going to read things line-by-line.
You need to call a different function to read a line into a string. Then call sscanf to read the string, first the part up to a comma, and then the part after a comma.
Upvotes: 0
Reputation: 5183
do this
char *last[30];
char *first[30];
while ( fscanf( input,"%s %s", last[i],first[i] ) != EOF )
Upvotes: 0