Which method can be used to read one line at a time from a file in C?
I am using the fgets function, but it\'s not working. It\'s reading the space
use either fgets
if you know that your lines will fit into buffer or use fgetc
for more control over reading
If you are coding for a platform that has the GNU C library available, you can use getline():
http://www.gnu.org/s/libc/manual/html_node/Line-Input.html
Source of Error:
Actually it was not my fault.. In this case . I was using strtok function and and accidently modified my original my original string. Hence while printing I was getting the error...
Thanks Everyone for Helping me.. :)
The fgets
function will read a single line from a file or num
characters where num
is the second parameter passed to fgets
. Are you passing a big enough number to read the line?
For Example
// Reads 500 characters or 1 line, whichever is shorter
char c[500];
fgets(c, 500, pFile);
Vs.
// Reads at most 1 character
char c;
fgets(&c,1,pFile);