0

I need to pass two files through my program in command line form

./your_executable inputfile.txt outputfile.txt

In my example, I'm using

gcc coursework.c CircleCode.txt CircleCode_tmp.txt

Therefore the second file doesn't exist and is opened in the program.

int main(int argc, char **argv[])
{
    FILE *orginalFile = fopen(*argv[1], "r");
    FILE *newFile = fopen(*argv[2], "w");

    if (orginalFile == NULL || newFile == NULL)
    {
        printf("Cannot open file");
        exit(0);
    }
}

The error in Clang:

error: no such file or directory: 'CircleCode_tmp.txt'
Student
  • 769
  • 1
  • 6
  • 11
Aimee Boyle
  • 11
  • 1
  • 4
  • 2
    Your declaraction of parameter `argv` is incorrect. You may use `char **argv`, or you may use `char *argv[]`; these are equivalent. On the other hand, your `char **argv[]` means something different, and it will not match the actual argument. Once you correct the declaration, correct also the uses, which each should have one fewer level of indirection. – John Bollinger Dec 20 '18 at 04:09
  • 1
    You should also check `if (argc < 3) { fputs ("error: insufficient input.\n", stderr); return 1; }` prior to using `argv[1]` and `argv[2]`. If no arguments are passed, `argv[1]` will be `NULL` and `argv[2]` an indeterminate address. – David C. Rankin Dec 20 '18 at 04:14
  • 5
    Er... you're passing those filenames to your **compiler**. You want them as command line arguments to the program itself when you run it. – Shawn Dec 20 '18 at 04:31
  • You need to compile and run as two different steps. Compile with **gcc -Wall coursework.c -o coursework** Run with **./coursework CircleCode.txt CircleCode_tmp.txt** Of course, you also need to fix the problems mentioned in the other comments. – user3386109 Dec 20 '18 at 04:35

2 Answers2

3

The signature of your main() is not right.

It could change it to

int main(int argc, char *argv[])                                       

or

int main(int argc, char **argv)                                        

since argv is to point to an array of strings.

See this post.


Since you need the input and output files for your program to work, you should check if the required number of arguments are received or not.

argc will have the number of arguments. Since the name of the command used to run the program itself is counted as an argument, along with the two files, the program needs at least 3 arguments.

So you may do something like

if(argc<3)
{                                                                        
   perror("Not enough arguments.");                                       
   return 1;                                                              
} 

Also, with

gcc coursework.c CircleCode.txt CircleCode_tmp.txt  

you are asking the compiler to compile your input and output text files as well which is probably not what you want.

Instead, you could do

gcc -Wall coursework.c -o your_executable

to compile the program and then run it like

./your_executable CircleCode.txt CircleCode_tmp.txt

The -Wall option of gcc is used to enable some warnings which may better help you spot and correct errors.

See this discussion as well.

J...S
  • 4,713
  • 1
  • 15
  • 34
0

argv type is char**, not need []

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

int main(int argc, char** argv) {
    FILE* orginalFile = fopen(argv[1], "r");
    FILE* newFile = fopen(argv[2], "w");

    if (orginalFile == NULL || newFile == NULL) {
      printf("Cannot open file");
      exit(0);
    }
}
sundb
  • 356
  • 1
  • 7