Reading one line at a time in C

后端 未结 10 877
萌比男神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:38

    Use fgets to read from the line, and then use getc(...) to chew up the newline or end-of-line to continue reading....here's an example of forever reading a line...

    // Reads 500 characters or 1 line, whichever is shorter
    char c[500], chewup;
    while (true){
        fgets(c, sizeof(c), pFile);
        if (!feof(pFile)){
            chewup = getc(pFile); // To chew up the newline terminator
            // Do something with C
        }else{
            break; // End of File reached...
        }
    }
    

提交回复
热议问题