|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 . |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
Thanks all! Did it with function error().
|
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 !! |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|