0

I was creating a c program to read two files from the argument from the command line and compare the size of the two files Here's what I coded

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[]){
FILE *file1, *file2;
int char1, char2;
char fname1 = argv[1];
char fname2 = argv[2];
file1 = fopen(fname1, "r");
file2 = fopen(fname2, "r");

if (file1 == NULL){
    printf("Cannot open %s\n", fname1);
    exit(1);
} else if (file2 == NULL) {
    printf("Cannot open %s for reading\n", fname2);
    exit(1);
}else{
    char1 = getc(file1);
    char2 = getc(file2);

    while((char1 != EOF) && (char2 != EOF) && (char1 == char2)) {
        char1 = getc(file1);
        char2 = getc(file2);
    }

    if (char1 == char2)
        printf("Files are the same\n");
    else if (char1 > char2){
        printf("%s is larger\n", file1);
    } else if(char1 < char2){
        printf("%s is larger\n", file2);
    }
    fclose(file1);
    fclose(file2);
}
return (0);

}

However, the terminal returns a lot of error message.

test.c: In function ‘main’:
test.c:7:19: warning: initialization makes integer from pointer without a cast [enabled by default]
     char fname1 = argv[1];
               ^
test.c:8:19: warning: initialization makes integer from pointer without a cast [enabled by default]
     char fname2 = argv[2];
               ^
test.c:9:5: warning: passing argument 1 of ‘fopen’ makes pointer from integer without a cast [enabled by default]
     file1 = fopen(fname1, "r");
     ^
In file included from test.c:1:0:
/usr/include/stdio.h:272:14: note: expected ‘const char * __restrict__’ but argument is of type ‘char’
 extern FILE *fopen (const char *__restrict __filename,
              ^
test.c:10:5: warning: passing argument 1 of ‘fopen’ makes pointer from integer without a cast [enabled by default]
     file2 = fopen(fname2, "r");
     ^
In file included from test.c:1:0:
/usr/include/stdio.h:272:14: note: expected ‘const char * __restrict__’ but argument is of type ‘char’
 extern FILE *fopen (const char *__restrict __filename,
              ^
test.c:13:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
         printf("Cannot open %s\n", fname1);
         ^
test.c:16:9: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
         printf("Cannot open %s for reading\n", fname2);
         ^
test.c:30:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘struct FILE *’ [-Wformat=]
             printf("%s is larger\n", file1);
             ^
test.c:32:13: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘struct FILE *’ [-Wformat=]
             printf("%s is larger\n", file2);

The errors are mainly about the type of the char type and the int type. I refered this page and only changed some parts in main function http://www.c4learn.com/c-programs/c-program-to-compare-two-textdata-files-in-c-programming.html but however this does not work for reading the files from the command line.

Sorry for being bothering but this is really annoying stucking on this kind of problem. Please give necessary help. Thanks

Zhou Jie
  • 31
  • 1
  • 1
  • 2
  • 1
    The code in the example is terrible, do not use it. You should read a book about C, for example [K&R](https://www.amazon.com/Programming-Language-Brian-W-Kernighan/dp/0131103628). And read about types in C. Especially about pointers. – alexanius Jul 19 '16 at 09:11
  • Yes I was thinking about that, and I may need to find some other sample code... – Zhou Jie Jul 19 '16 at 09:17
  • How do you tell a good sample from a bad sample? Read K&R. That's how. Also, do the exercises... – autistic Jul 19 '16 at 09:25
  • 2
    `char *fname1 = argv[1]; char *fname2 = argv[2];` instead of. – BLUEPIXY Jul 19 '16 at 09:32

0 Answers0