I want to write a function that fetches one line from a text file and writes it to a buffer. I just want to pass the path and/or name of the file and a reference to a buffer. With each function call the next line in the file is written to the buffer.
Please tell me, if the code below is okay or needs some modification.
Code:
#include <stdio.h>
#define SIZE 500
int getline(char filename[], char buf[])
{
static FILE *in = NULL;
if(in == NULL)
in = fopen(filename, "r");
if(!fgets(buf, SIZE, in))
{
/* EOF reached ... hopefully */
fclose(in);
return 0;
}
return 1;
}
int main()
{
char buf[SIZE] = {'\0'};
while(getline("blabla.txt", buf))
{
puts(buf);
memset(buf, '\0', SIZE);
}
return 0;
}