-2

I'm new to C and struggling to see why this won't compile. The script should take two text files as args - read the first and copy the contents to the second. I'm getting the following error at compile:

root@debian:/home/kevin# gcc -Wall mycat.c -o mycat
mycat.c: In function ‘main’:
mycat.c:4:3: error: too few arguments to function ‘fgetc’
In file included from mycat.c:1:0:
/usr/include/stdio.h:533:12: note: declared here
mycat.c:5:5: error: too few arguments to function ‘fputc’

I'm not sure why it says fgetc should take more arguments because it's shown on my lecture slides taking one argument?

#include <stdio.h>
#include <stdlib.h>
int main(){
const char∗ ifilename = $1;
FILE∗ istream = fopen(ifilename, ”r”);
if (istream == NULL) {
fprintf(stderr, ”Cannot open %s \n”,ifilename);
exit(1);
}
const char∗ ofilename = ”$2”;
FILE∗ ostream = fopen(ofilename, ”w”);
if (ostream == NULL) {
fprintf(stderr, ”Cannot open %s \n” , ofilename);
exit(1);
}
int = c;
while ((c = fgetc(istream)) != EOF)
fputc(ostream);
return 0;
}
COOLBEANS
  • 729
  • 3
  • 13
  • 31
  • 2
    What are `$1` and `$2`? – Some programmer dude Feb 27 '14 at 18:35
  • 1
    Also, the line numbers in those error messages (4 and 5 respectively) doesn't match the code you pasted. – Some programmer dude Feb 27 '14 at 18:35
  • Something doesn't look copied over correctly from your original. The error says `mycat.c:4:3` but your `fgetc` is on a very different line in the code you're showing. – lurker Feb 27 '14 at 18:36
  • 1
    First of all, what is `$1`.. this isn't valid C syntax. Also, it's not a script – Engineer2021 Feb 27 '14 at 18:37
  • fputc takes an int and a File *, fputc(int c,FILE *fp), not aware of an fputc that takes only 1 argument, 1 argument doesn't make sense anyway. maybe you're confusing with getc and putc. – tesseract Feb 27 '14 at 18:41
  • 4
    Typographical quotes `”...”` are also not accepted by the compiler. This seems to by copied from some PDF document or similar, instead of copying the real source code. – Martin R Feb 27 '14 at 18:44

1 Answers1

3

I was bored so I coded from scratch. Tested it right now and it worked. Maybe you can get your mistakes from my code (After all the algorithm I used is the same as yours)

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

int main(int argc, char* argv[]){
    FILE* filesrc = NULL;
    FILE* filedest = NULL;
    int c = 0;

    if(argc != 3){
        fprintf(stderr,"Usage: programname sourcefile destinationfile");
        return EXIT_FAILURE;
    }

    if((filesrc = fopen(argv[1],"r")) == NULL){
        fprintf(stderr,"Cannot open sourcefile!");
        return EXIT_FAILURE;
    }
    if((filedest = fopen(argv[2],"w")) == NULL){
        fprintf(stderr,"Cannot open destinationfile!");
        return EXIT_FAILURE;
    }

    while ((c = fgetc(filesrc)) != EOF)
        fputc(c,filedest);

    return EXIT_SUCCESS;
}

To call the program type in console: ./programname sourcefilename destinationfilename Your major mistake is your argument handling, or it's a completely new c feature I never heard of ;) Arguments are passed to the main() function as an array of strings (char pointers) argv[0] = program name || argv[1] = 1st argument || argv[2] = 2nd argument etc... And as already mentioned: you forgot the first argument for the fgetc() (your variable 'c' that is)

ViGi
  • 140
  • 1
  • 11