Reading one line at a time in C

后端 未结 10 870
萌比男神i
萌比男神i 2020-12-01 13:01

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

相关标签:
10条回答
  • 2020-12-01 13:54

    use either fgets if you know that your lines will fit into buffer or use fgetc for more control over reading

    0 讨论(0)
  • 2020-12-01 13:56

    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

    0 讨论(0)
  • 2020-12-01 13:58

    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.. :)

    0 讨论(0)
  • 2020-12-01 14:00

    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);
    
    0 讨论(0)
提交回复
热议问题