error handlling in C

B

broli

Is it generally a good idea to use perror() function to handle all the
error situations ?

For eg in one of my modules I have used it extensively.

int reader()
{
FILE *zeus_file;
....................
.....................

zeus_file = fopen("sphere.zeus", "r");
if(zeus_file == NULL)
{
perror("File open error");
return -1;
}

..............................................

}
 
V

vippstar

Is it generally a good idea to use perror() function to handle all the
error situations ?

For eg in one of my modules I have used it extensively.

int reader()
{
FILE *zeus_file;
....................
.....................

zeus_file = fopen("sphere.zeus", "r");
if(zeus_file == NULL)
{
perror("File open error");
return -1;
}

..............................................

}

If you afterwards decide to use another stream than strerr, then yes
it's bad.
Use fprintf(stream, %s%s\n", "File open error", strerror(errno))
strerror() is declared in <string.h>
 
F

Flash Gordon

broli wrote, On 28/03/08 15:02:
Is it generally a good idea to use perror() function to handle all the
error situations ?

As ever, it depends.
For eg in one of my modules I have used it extensively.

int reader()
{
FILE *zeus_file;
....................
.....................

zeus_file = fopen("sphere.zeus", "r");
if(zeus_file == NULL)
{
perror("File open error");
return -1;
}

..............................................

}

Now consider changing it to take the file name as a parameter and then
doing something like:
if (reader("main-config-file"));
else if (reader("alternate-config-file");
else ...

Sometimes you can handle the error without bothering the users, other
times you want to log it to a file, sometimes send it to stderr. It all
depends.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,184
Latest member
ZNOChrista

Latest Threads

Top