Why can't I use the file pointer after the first read attempt fails?

36 views Asked by At

I want to open a file for reading and writing if it exists, or if not, create it and open it for writing.

The following code

FILE *file = fopen(argv[file_position], "r+");
if (!file) 
    file = fopen(argv[file_position], "w");

causes the error file_name: Bad file descriptor.

What is the cause of the error and how to avoid it?

0

There are 0 answers