How do you use fscanf to read in a string into a character array, say buf, INCLUDING the spaces???
This is a discussion on quick fscanf question... within the C++ Programming forums, part of the General Programming Boards category; How do you use fscanf to read in a string into a character array, say buf, INCLUDING the spaces???...
How do you use fscanf to read in a string into a character array, say buf, INCLUDING the spaces???
fscanf( myfile, "%[^|]", buf);
Although fgets is often more suited to the task of line input.
Demonographic rhinology is not the only possible outcome, but why take the chance
My input file is:
0
0
3
0
+9999999999
+8000000001
+1001002003
-8003000000
+9999999999
My code is:
FILE *fp = fopen("test.ail", "r");
char buf[18];
while(1)
{
//fscanf( fp, "%[^|]", buf);
fgets (buf, 18, fp);
printf ("\n%s", buf);
if ( (strcmp(buf, "+9999999999")) == 0)
break;
}
...but for some reason, the program keeps printing out a whole line of space between each number like so for the output:
0
0
3
0
+9999999999
+8000000001
+1001002003
-8003000000
+9999999999
How can I fix this?
Perhaps you're storing the newline / CR character from your file in the string. When you combine that with the "\n" in your printf statement you get 2 line feeds instead of one.
Demonographic rhinology is not the only possible outcome, but why take the chance
>>How do you use fscanf to read in a string into a character array, say buf, INCLUDING the spaces???
You use something better suited to line input :-)
fscanf really should be used only for specific formatting, if you ever need to read a whole line then use a function designed for that job, it's safer and more natural. :-)Code:cin.getline(buf, delim);
*Cela*