Answers for "how to read the entire file and get the data line by line in C"

C
3

read file in c line by line

#include <stdio.h>

int main(int argc, char* argv[])
{
    char const* const fileName = argv[1]; /* should check that argc > 1 */
    FILE* file = fopen(fileName, "r"); /* should check the result */
    char line[256];

    while (fgets(line, sizeof(line), file)) {
        /* note that fgets don't strip the terminating \n, checking its
           presence would allow to handle lines longer that sizeof(line) */
        printf("%s", line); 
    }
    /* may check feof here to make a difference between eof and io failure -- network
       timeout for instance */

    fclose(file);

    return 0;
}
Posted by: Guest on May-03-2021

Code answers related to "how to read the entire file and get the data line by line in C"

Code answers related to "C"

Browse Popular Code Answers by Language