You are asking for the name of the text file twice. Look, you ask for it as an argument of the program and the you asked again at the line 15.
So the code should look like these:
#include <stdio.h>
#include <conio.h> // so i can pause it using getch()
int main( int argc, char* argv[])
{
if(argc<2)
{
printf("Usage: %s <filename>\n",argv[0]);
return 2;
}
if(argc>2)
{
printf("Please use only 1 filename\n");
return 2;
}
printf("Trying to read %s \n",argv[1]);
char c = '\n';
int i = 1;
FILE * f = fopen(argv[1],"r");
while (!feof(f))
{
if (c == '\n')
printf("%d ",i++);
c = fgetc(f);
putchar(c);
}
fclose(f);
getch(); // To ask for confirmation and then close the program
return 0;
}
The another way is to ask for the filename while runtime, and not to pass it as a program argument:
#include <stdio.h>
#include <conio.h> // so i can pause it using getch()
int main( int argc, char* argv[])
{
if(argc<2)
{
printf("Usage: %s <filename>\n",argv[0]);
return 2;
}
if(argc>2)
{
printf("Please use only 1 filename\n");
return 2;
}
printf("Trying to read %s \n",argv[1]);
char c = '\n';
int i = 1;
FILE * f = fopen(argv[1],"r");
while (!feof(f))
{
if (c == '\n')
printf("%d ",i++);
c = fgetc(f);
putchar(c);
}
fclose(f);
getch(); // To ask for confirmation and then close the program
return 0;
}
It's C not C++. See the Code above. That's why i said that getch(); can work without conio.h in "C".