Scan whole line from file in C Programming

c eol
user2039831 picture user2039831 · Oct 16, 2013 · Viewed 11.5k times · Source

I was writing a program to input multiple lines from a file. the problem is i don't know the length of the lines, so i cant use fgets cause i need to give the size of the buffer and cant use fscanf cause it stops at a space token I saw a solution where he recommended using malloc and realloc for each character taken as input but i think there's an easier way and then i found someone suggesting using

fscanf(file,"%[^\n]",line);

Does anyone have a better solution or can someone explain how the above works?(i haven't tested it)

i use GCC Compiler, if that's needed

Answer

Ben picture Ben · Oct 16, 2013

You can use getline(3). It allocates memory on your behalf, which you should free when you are finished reading lines.