Afficher un message
Vieux 05/05/2008, 22h15   #7
William Pursell
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.)


  Réponse avec citation
 
Page generated in 0,05725 seconds with 9 queries