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'
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. Commented Dec 20, 2018 at 4: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. Commented Dec 20, 2018 at 4: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. Commented Dec 20, 2018 at 4:31
  • 1
    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. Commented Dec 20, 2018 at 4:35

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.