PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > printing error messages
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
printing error messages

Réponse
 
LinkBack Outils de la discussion
Vieux 30/05/2008, 17h07   #1
gsa
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut printing error messages

Hi,
I am new to C so please bear with my stupid questions. I am
trying to open a file to write, but the code complains that it is
unable to do so. How do I get C to print the reason it is failing?
Something like the $! function in perl. Thanks a lot for all your
.

  Réponse avec citation
Vieux 30/05/2008, 17h14   #2
dj3vande@csclub.uwaterloo.ca.invalid
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: printing error messages

In article <a13081bb-7012-480f-85e2-de62db236222@j33g2000pri.googlegroups.com>,
gsa <hsggwk@gmail.com> wrote:
>Hi,
> I am new to C so please bear with my stupid questions. I am
>trying to open a file to write, but the code complains that it is
>unable to do so. How do I get C to print the reason it is failing?
>Something like the $! function in perl. Thanks a lot for all your
>.


perror() may be what you're looking for.
I don't believe fopen is required to set errno when it fails, but on
every implementation I've encountered it does. (That may be influenced
by the fact that most of the implementations I've used are unixy, and I
think POSIX *does* require the underlying system calls to set errno.)


dave

--
Dave Vandervies dj3vande at eskimo dot com
I've been curious about what 'lingua franca' actually meant for a while, and
this seemed the perfect excuse to find out - and it appears to have worked.
comp.lang.c will never cease to amaze me. --Richard Heathfield
  Réponse avec citation
Vieux 30/05/2008, 17h18   #3
Walter Roberson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: printing error messages

In article <a13081bb-7012-480f-85e2-de62db236222@j33g2000pri.googlegroups.com>,
gsa <hsggwk@gmail.com> wrote:
> I am new to C so please bear with my stupid questions. I am
>trying to open a file to write, but the code complains that it is
>unable to do so. How do I get C to print the reason it is failing?
>Something like the $! function in perl.


See the documentation on perror(). (Note: perror has some subtle
limitations that can make it a better idea to use the lower-level
equivilents... which you will likely find references to in the
perror() documentation.)
--
"To all, to each! a fair good-night,
And pleasing dreams, and slumbers light" -- Sir Walter Scott
  Réponse avec citation
Vieux 30/05/2008, 17h36   #4
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: printing error messages

gsa wrote:
> Hi,
> I am new to C so please bear with my stupid questions. I am
> trying to open a file to write, but the code complains that it is
> unable to do so. How do I get C to print the reason it is failing?
> Something like the $! function in perl. Thanks a lot for all your
> .


FILE *stream = fopen(filename, mode);
if (stream == NULL) {
perror(filename);
... whatever else ...
}

This is not absolutely airtight, because fopen() is not
*required* to describe its reason for failing by storing a
value in `errno'. So there's a chance you'll get some
completely bogus "reason" for the failure, and that could
confuse the person reading the message. In my experience,
most C implementations *do* set `errno' when fopen() fails,
so I feel the benefit of displaying what `errno' says outweighs
the risk that what it says might be nonsense.

Two warnings: First, display information from `errno'
only after you've determined that there's been a failure;
`errno' itself is not a failure-detection mechanism. Second,
don't call other functions between the point of failure and
the point when you call perror(), because they may change the
value of `errno' even on successful calls.

--
Eric.Sosman@sun.com
  Réponse avec citation
Vieux 30/05/2008, 17h57   #5
gsa
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: printing error messages

Thanks all! Did it with function error().



  Réponse avec citation
Vieux 30/05/2008, 18h01   #6
gsa
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: printing error messages

I just stuck the error function call in the if block and it worked.

if((fp = fopen(stretchesFile, "w")) == NULL) {
error(1, errno, "could not open file %s", stretchesFile);
exit(1);
}

Thanks again for your !!

  Réponse avec citation
Vieux 30/05/2008, 18h09   #7
dj3vande@csclub.uwaterloo.ca.invalid
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: printing error messages

In article <dd7b86c4-554d-439d-ac90-a522770ce6ce@l17g2000pri.googlegroups.com>,
gsa <hsggwk@gmail.com> wrote:
>I just stuck the error function call in the if block and it worked.
>
>if((fp = fopen(stretchesFile, "w")) == NULL) {
> error(1, errno, "could not open file %s", stretchesFile);
> exit(1);
> }
>
>Thanks again for your !!


Note that error() is not portable; the man page on my system says:
These functions and variables are GNU extensions, and should not be
used in programs intended to be portable.
So if you plan to use this program anywhere other than on Linux, you
should probably use the portable building blocks (error() appears to be
built on strerror(), exit(), and a few stdio functions, plus an
extension to get the value of argv[0] somewhere outside of main())
instead of the GNU convenience wrapper.
(In your case, it may be acceptable to plan to write your own version
of error() when you want to run somewhere other than Linux, since it
gives you a one-line call that does a set of things that's not-quite-
trivial to write yourself. But you should be aware of the
nonportability and make sure that the benefits do indeed exceed the
costs.)


dave

--
Dave Vandervies dj3vande at eskimo dot com
I've been curious about what 'lingua franca' actually meant for a while, and
this seemed the perfect excuse to find out - and it appears to have worked.
comp.lang.c will never cease to amaze me. --Richard Heathfield
  Réponse avec citation
Vieux 30/05/2008, 19h04   #8
pereges
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: printing error messages

I personally use fprintf. For eg:

typedef struct xyz_struct
{
..
}*xyzptr;

void foo()
{
xyzptr p;

p = malloc(sizeof(*p));

if(p == NULL)
frpintf("Unable to allocate memory: %s %d %s", __FILE__,
__LINE__,__func__);
..
}



I'm not sure if this is a good method though
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h30.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17987 seconds with 16 queries