I want to read lines from a file line-by-line, but it's not working for me.
Here is what I tried to do:
FILE *file;
char *line = NULL;
int len = 0;
char read;
file=fopen(argv[1], "r");
if (file == NULL)
return 1;
while ((read = getline(&line, len, file)) != -1) {
printf("Retrieved line of length %s :\n", &read);
printf("%s", line);
}
if (line)
free(line);
return 0;
Any suggestions why that isn't working?
getline()
works. It allocates memory for the line itself. – Ivonnegetline
is wrong. It expects a pointer and not an integer. – Cookgeneral