Search code examples
cfilefopen

fopen() returning null, with invalid argument error


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


Solution

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