Reading line by line from a file in C
Posted
by
mh234
on Stack Overflow
See other posts from Stack Overflow
or by mh234
Published on 2014-05-29T21:03:55Z
Indexed on
2014/05/29
21:26 UTC
Read the original article
Hit count: 122
What I am trying to do is print out the contents of a file line by line. I run the program in terminal by doing: ./test testText.txt. When I do this, random characters are printed out but not what is in the file. The text file is located in the same folder as the makefile. What's wrong?
#include <stdio.h>
FILE *fp;
int main(int argc, char *argv[])
{
char line[15];
fp = fopen(*argv, "r");
while((fgets(line, 15, fp)) != NULL)
{
printf(line);
printf("\n");
}
}
© Stack Overflow or respective owner