Hello, i'm just trying the challenge set on this site where i have to read a file and count the number of lines in this file. I've read several tutorials and FAQs on file handling but i still can't work out where i'm going wrong (it will be an extrememly simple error) I have the following code so far:
Code:
#include <stdio.h>
int main(int argc, char* argv[])
{
int line = 0, c = 0;
//char c;
FILE *myfile_in;
myfile_in = fopen(argv[1], "r");
if (argc!=2)
{
printf("ERROR: Input should be in form fileio filename.txt");
}
if (myfile_in == NULL)
{
printf("ERROR: Invalid filename supplied ( %s )", argv[1]);
return 1;
}
while(fscanf(myfile_in, "%d", &c) != EOF )
{
if (fscanf(myfile_in,"%d", &c) == '\n')
{
line++;
}
}
fclose(myfile_in);
printf("Lines in file: %d", line);
return 0;
}
The problem is that it says line is 0 every time. The text file contains a series of numbers on different lines, eg.
As usual no doubt i'm doing something totally wrong, so any input or tips/pointers are greatly appreciated. thanks.