Why is this code not working? - file reading

This code normally outputs the contents of a file, while numbering the lines!
This is the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>

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;
        scanf("%s",argv[1]);
        FILE * f = fopen(argv[1],"r");
        while (!feof(f)) {
                if (c == '\n')
                        printf("%d ",i++);
                c = fgetc(f);
                putchar(c);
        }
        fclose(f);
        return 0;
}

Thx in advance for any reply!
Last edited on
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <conio.h>

int main(void)
{
    printf("Enter the filename: ");
    char filename[30];
    scanf("%s",filename);
    printf("Trying to read %s \n",filename);
    char c = '\n';
    int i = 1;
    FILE * f = fopen(filename,"r");
    while (!feof(f)) 
    {
          if (c == '\n')
             printf("%d ",i++);
          c = fgetc(f);
          putchar(c);
    }
    fclose(f);
    getch();
    return 0;
}


I hope I've helped you...and sory for my bad english...it's not my first language.
Last edited on
even without conio you can use getch();?
The function getch() is not in the standard C++ libraries, it belongs to the conio library.
Last edited on
@Celtc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#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".
Topic archived. No new replies allowed.