0

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;
}
2
Contributors
1
Reply
9
Views
1 Year
Discussion Span
Last Post by Moschops
0

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.

Edited by Moschops

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.