Home > Blockchain >  fopen() returning null, with invalid argument error
fopen() returning null, with invalid argument error

Time:02-22

I have a very simple program prog.c:

int main(int argc, char ** argv) {
  char * file_path = argv[2];
  FILE * fp = fopen(file_path, "-rb");
  if (fp == NULL) {
    perror("\nerror");
  }
}

I compile the program with gcc -o prog prog.c -lrt and run with ./prog --file Tiny.txt

and prog and Tiny.txt are both directly under the src directory (both are at the same level). I cannot see why fopen is giving me an invalid argument error.

I am running on Ubuntu 20.04.3

CodePudding user response:

Sorry, I just realized I used a - in -rb. It should simply be rb.

CodePudding user response:

The problem is that you have used an invalid character in the open mode parameter, the error message is clear, a parameter is invalid, the first is a file name, which allows anything to be put inside... so look at the only other parameter you have. The '-' finally was the guilty.

CodePudding user response:

There should be:

FILE * fp = fopen(file_path, "rb");

instead of:

FILE * fp = fopen(file_path, "-rb");
  • Related