Hi everyone. I got a problem with, i suppose fscanf. I need to read strings from a .txt file, line by line, a total of 200 lines. But it seems that fscanf can read only up to 30 lines, after that, it stops working, and offers Debug or Close...
Here's the code...
If you must use fscanf(), make sure you put an upper limit on the number of characters to store using a width specifier. Make sure to read the docs: http://linux.die.net/man/3/scanf
Note, in particular, that the width does not include space needed to store the terminating '\0'.
1 2
char s[ 50 ];
fgets( s, 50, f ); // reads an entire line, including the newline character
1 2
char s[ 50 ];
fscanf( f, "%49s", s ); // discards leading whitespace, stops on whitespace
1 2
char s[ 50 ];
fscanf( f, "%49[^\n]\n", s ); // like fgets(), but does not store the newline character
Considering that as Duoas said, fscanf does not read lines with the %s specifier, but instead words, and that fgets reads lines, it probably indicates that the IO is not the problem. I mean you should have gotten the error at different loop iterations, unless each line of your input file contains exactly one word.