Re: An interesting c program for beginners
On 5 May, 07:45, apati...@gmail.com wrote:
>
> /* Always check to make sure that you succeeded in opening the file.
> */
>
> infile = fopen("result.txt", "r");
> if (infile == NULL)
> printf("Unable to open result.txt\n");
>
>
Much preferred is:
char *path;
FILE *infile;
infile = fopen( path = "result.txt", "r");
if( infile == NULL )
perror( path );
This is preferred for 2 reasons (at least):
1) the error message goes to the error stream
where it belongs instead of the output stream
2) the error message includes the reason for
the failure. (Not on all systems, but on
most. Those systems on which fopen fails to
properly set errno on a failure are...deranged.)
|