I have seen both ways for reading file error messages. Is one better than the other? Is exit(0) better than return 1 or vice versa?

FILE *fp;
fp = fopen(argv[1], "r");                         //Open file for read.

if(!fp)
{
    printf("Error opening file %s!",argv[1]);    //Program prints error message and closes if file is not found.
    exit(0);
}




FILE *input;
input = fopen (argv[1],"r");
if (input == NULL)
{
    printf("Error while opening the file.\n");
    return 1;
}

Neither of these are anything to do with "reading file error messages".

return ends the function, and goes back to whatever called that function. The program continues running.

exit finishes the program. Everything stops running, the program ends.

They do different things. Neither is better that the other. They do different things.

Use the one that does what you want; if you want the function to end and the program to carry on, user return. If you want the whole program to end, it would actually be more elegant to still use return and finish the program gracefully, but if you don't care about that and just want to stop, use exit. return and exit do different things and you should use the one that does what you want.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.