printing error messages

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gsa

    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
    help.

  • Walter Roberson

    #2
    Re: printing error messages

    In article <a13081bb-7012-480f-85e2-de62db236222@j3 3g2000pri.googl egroups.com>,
    gsa <hsggwk@gmail.c omwrote:
    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

    Comment

    • dj3vande@csclub.uwaterloo.ca.invalid

      #3
      Re: printing error messages

      In article <a13081bb-7012-480f-85e2-de62db236222@j3 3g2000pri.googl egroups.com>,
      gsa <hsggwk@gmail.c omwrote:
      >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
      >help.
      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

      Comment

      • Eric Sosman

        #4
        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
        help.
        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

        Comment

        • gsa

          #5
          Re: printing error messages

          Thanks all! Did it with function error().



          Comment

          • gsa

            #6
            Re: printing error messages

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

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

            Thanks again for your help!!

            Comment

            • dj3vande@csclub.uwaterloo.ca.invalid

              #7
              Re: printing error messages

              In article <dd7b86c4-554d-439d-ac90-a522770ce6ce@l1 7g2000pri.googl egroups.com>,
              gsa <hsggwk@gmail.c omwrote:
              >I just stuck the error function call in the if block and it worked.
              >
              >if((fp = fopen(stretches File, "w")) == NULL) {
              error(1, errno, "could not open file %s", stretchesFile);
              exit(1);
              }
              >
              >Thanks again for your help!!
              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

              Comment

              • pereges

                #8
                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

                Comment

                • Bill Cunningham

                  #9
                  Re: printing error messages


                  "gsa" <hsggwk@gmail.c omwrote in message
                  news:dd7b86c4-554d-439d-ac90-a522770ce6ce@l1 7g2000pri.googl egroups.com...
                  >I just stuck the error function call in the if block and it worked.
                  >
                  if((fp = fopen(stretches File, "w")) == NULL) {
                  error(1, errno, "could not open file %s", stretchesFile);
                  exit(1);
                  }
                  >
                  Thanks again for your help!!
                  For my 2 cents worth exit(1) isn't portable. Try exit(EXIT_FAILU RE) from
                  stdlib.h. For me it has made the difference between a working and non
                  working program.

                  Bill


                  Comment

                  • Richard

                    #10
                    Re: printing error messages

                    "Bill Cunningham" <nospam@nspam.c omwrites:
                    "gsa" <hsggwk@gmail.c omwrote in message
                    news:dd7b86c4-554d-439d-ac90-a522770ce6ce@l1 7g2000pri.googl egroups.com...
                    >>I just stuck the error function call in the if block and it worked.
                    >>
                    >if((fp = fopen(stretches File, "w")) == NULL) {
                    > error(1, errno, "could not open file %s", stretchesFile);
                    > exit(1);
                    > }
                    >>
                    >Thanks again for your help!!
                    >
                    For my 2 cents worth exit(1) isn't portable. Try exit(EXIT_FAILU RE) from
                    stdlib.h. For me it has made the difference between a working and non
                    working program.
                    >
                    Bill
                    I do not believe you. I would be interested in seeing this program that
                    suddenly magically worked when you change the code you call exit with.

                    Comment

                    • santosh

                      #11
                      Re: printing error messages

                      Bill Cunningham wrote:
                      >
                      "gsa" <hsggwk@gmail.c omwrote in message
                      >
                      news:dd7b86c4-554d-439d-ac90-a522770ce6ce@l1 7g2000pri.googl egroups.com...
                      >>I just stuck the error function call in the if block and it worked.
                      >>
                      >if((fp = fopen(stretches File, "w")) == NULL) {
                      > error(1, errno, "could not open file %s", stretchesFile);
                      > exit(1);
                      > }
                      >>
                      >Thanks again for your help!!
                      >
                      For my 2 cents worth exit(1) isn't portable. Try
                      exit(EXIT_FAILU RE) from
                      stdlib.h. For me it has made the difference between a working and non
                      working program.
                      exit(1) didn't work? What's your system and what exactly happened, i.e.,
                      what was the behaviour that made you conclude that exit(1) failed.

                      Comment

                      • Richard

                        #12
                        Re: printing error messages

                        santosh <santosh.k83@gm ail.comwrites:
                        Bill Cunningham wrote:
                        >
                        >>
                        >"gsa" <hsggwk@gmail.c omwrote in message
                        >>
                        news:dd7b86c4-554d-439d-ac90-a522770ce6ce@l1 7g2000pri.googl egroups.com...
                        >>>I just stuck the error function call in the if block and it worked.
                        >>>
                        >>if((fp = fopen(stretches File, "w")) == NULL) {
                        >> error(1, errno, "could not open file %s", stretchesFile);
                        >> exit(1);
                        >> }
                        >>>
                        >>Thanks again for your help!!
                        >>
                        > For my 2 cents worth exit(1) isn't portable. Try
                        > exit(EXIT_FAILU RE) from
                        >stdlib.h. For me it has made the difference between a working and non
                        >working program.
                        >
                        exit(1) didn't work? What's your system and what exactly happened, i.e.,
                        what was the behaviour that made you conclude that exit(1) failed.
                        He is trolling. I replied before realising who it was.

                        Comment

                        • Keith Thompson

                          #13
                          Re: printing error messages

                          "Bill Cunningham" <nospam@nspam.c omwrites:
                          "gsa" <hsggwk@gmail.c omwrote in message
                          news:dd7b86c4-554d-439d-ac90-a522770ce6ce@l1 7g2000pri.googl egroups.com...
                          >>I just stuck the error function call in the if block and it worked.
                          >>
                          >if((fp = fopen(stretches File, "w")) == NULL) {
                          > error(1, errno, "could not open file %s", stretchesFile);
                          > exit(1);
                          > }
                          >>
                          >Thanks again for your help!!
                          >
                          For my 2 cents worth exit(1) isn't portable. Try
                          exit(EXIT_FAILU RE) from stdlib.h. For me it has made the difference
                          between a working and non working program.
                          Really?

                          You're absolutely right that exit(1) isn't portable, and
                          exit(EXIT_FAILU RE) is preferred.

                          But it won't affect the behavior of the program itself until it
                          terminates -- and on most systems you're likely to be using
                          (including, I'm fairly sure, all variations of Unix and Windows),
                          exit(1) and exit(EXIT_FAILU RE) happen to behave the same way. (You're
                          not using VMS, are you?)

                          Perhaps you happened to make some other fix at the same time that you
                          changed the exit() call.

                          --
                          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                          Nokia
                          "We must do something. This is something. Therefore, we must do this."
                          -- Antony Jay and Jonathan Lynn, "Yes Minister"

                          Comment

                          • CBFalconer

                            #14
                            Re: printing error messages

                            santosh wrote:
                            Bill Cunningham wrote:
                            >
                            .... snip ...
                            >
                            >For my 2 cents worth exit(1) isn't portable. Try
                            > exit(EXIT_FAILU RE) from
                            >stdlib.h. For me it has made the difference between a working
                            >and non working program.
                            >
                            exit(1) didn't work? What's your system and what exactly happened,
                            i.e., what was the behaviour that made you conclude that exit(1)
                            failed.
                            "exit(1);" never is portable, and has absolutely no guarantees.
                            Bill is right here, the only guaranteed values for exit are 0,
                            EXIT_OK, and EXT_FAILURE. The latter two require include stdlib.h.

                            --
                            [mail]: Chuck F (cbfalconer at maineline dot net)
                            [page]: <http://cbfalconer.home .att.net>
                            Try the download section.


                            ** Posted from http://www.teranews.com **

                            Comment

                            • Keith Thompson

                              #15
                              Re: printing error messages

                              CBFalconer <cbfalconer@yah oo.comwrites:
                              santosh wrote:
                              >Bill Cunningham wrote:
                              >>
                              ... snip ...
                              >>
                              >>For my 2 cents worth exit(1) isn't portable. Try
                              >> exit(EXIT_FAILU RE) from
                              >>stdlib.h. For me it has made the difference between a working
                              >>and non working program.
                              >>
                              >exit(1) didn't work? What's your system and what exactly happened,
                              >i.e., what was the behaviour that made you conclude that exit(1)
                              >failed.
                              >
                              "exit(1);" never is portable, and has absolutely no guarantees.
                              Bill is right here, the only guaranteed values for exit are 0,
                              EXIT_OK, and EXT_FAILURE. The latter two require include stdlib.h.
                              Bill is of course 100% correct that exit(1) is not portable. But
                              that's not all he said. He also said that it "has made the difference
                              between a working and non working program". I find that claim
                              surprising (for reasons that happen to be beyond the scope of the C
                              standard).

                              Do you have reason to believe that it actually did make such a
                              difference?

                              --
                              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                              Nokia
                              "We must do something. This is something. Therefore, we must do this."
                              -- Antony Jay and Jonathan Lynn, "Yes Minister"

                              Comment

                              Working...