--- In [email protected], "David Hamill" <[EMAIL PROTECTED]> wrote:
>
> Bill asked:
> > Is this the way to use perror and clearerr ?
> 
> Try this instead:
> 
>   fp = fopen("nonexistent.file", "r");
>   if (fp == NULL)
>   {
>     perror("Unable to open file");

It would be useful to include the name of the file you are attempting
to open, although it does make the code more complicated- you have to
create the string you want to output in a temporary buffer:

    static const char filename[] = "nonexistent.file";

    if (!(fp = fopen(filename, "r")))
    {
        char tmp[100];
        sprintf(tmp, "Unable to open file \"%s\"", filename);
        perror(tmp);

resulting in:

Unable to open file "nonexistent.file": No such file or directory

John

Reply via email to