Answers for "c how to read a file line by line"

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
0

c read a whole string from a file

#define  _GNU_SOURCE  //Necessary for getline to work with clang in Ubuntu
getline(&line, &len, fp);
Posted by: Guest on November-01-2020

Code answers related to "c how to read a file line by line"

Code answers related to "C"

Browse Popular Code Answers by Language