Home > Software design >  What happens when I open a text file and it's equal to NULL?
What happens when I open a text file and it's equal to NULL?

Time:12-05

We recently learned opening files in the uni and the prof didn't tell us this. So I open a text file and do a check whether the file is equal to NULL or not. What if it is equal to NULL? It means that I have no data in the file?

FILE* file = fopen(filename, "r");
    
    if (file == NULL)
    {
        return 1;
    }

CodePudding user response:

if fopen() returns a NULL, it means that the file opening was not successfully accomplished, also in that case it sets the errno global variable is set to duplicate an error. You can also read 'man fopen' in your terminal for more details.

  • Related