stderr question

This is a discussion on stderr question within the C Programming forums, part of the General Programming Boards category; so I have this code on main: fprintf(stderr, "ERROR"); then how do I know that in stderr contains ERROR?? I ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    stderr question

    so I have this code on main:

    fprintf(stderr, "ERROR");

    then how do I know that in stderr contains ERROR?? I tried to do cat stderr but it doesn't work

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    Typically stderr would also print to the console, like stdout.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Umm . . . stderr is much like stdout. It's a standard stream. By default, both streams output to the screen. However, you can redirect stdout to a file quite easily, leaving stderr still "pointing" to the screen.
    Code:
    #include <stdio.h>
    
    int main() {
        fprintf(stderr, "stderr\n");
        printf("stdout\n");  /* same as fprintf(stdout, "stdout\n") */
        return 0;
    }
    An ordinary run of that will give this output:
    Code:
    stderr
    stdout
    But run it from the command line like this:
    Code:
    program > file.txt
    and "stderr" will go to the screen while file.txt will contain "stdout".

    On Linux systems, you can usually redirect stderr with "2>". I'm not sure about Windows.

    So, here's a sample session of some shell or another:
    Code:
    $ cat program.c
    #include <stdio.h>
    
    int main() {
        fprintf(stderr, "stderr\n");
        printf("stdout\n");  /* same as fprintf(stdout, "stdout\n") */
        return 0;
    }
    $ gcc program -o program
    $ ./program
    stderr
    stdout
    $ cat output.txt
    $ ./program > output.txt
    stderr
    $ cat output.txt
    stdout
    $ ./program 2> output.txt
    stdout
    $ cat output.txt
    stderr
    $ ./program >output.txt 2>&1
    $ cat output.txt
    stdout
    stderr
    $
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    stderr is the output stream for error logging I believe. Common errors such as this one get logged:
    Code:
    if ( (fp = fopen( filename, mode )) == NULL ) {
      fprintf( stderr, "Couldn't open: &#37;s\n", filename );
    }
    Most implementations simply display the stderr stream on the screen unless you redirect it. I believe redirection for stderr is indicated like this:

    cprog 2> mylog.txt

    And then errors are no longer displayed on the screen but rather written to the file. The terminal opens and closes the stream for you, and will always empty previous versions of mylog.txt.

    To append an existing file, you might try cprog 2>> mylog.txt

    There's all sorts of shell specific jargon for this type of thing. You've probably caught on to the fact that you could open your own file and do error logging. I really don't see much harm in that, though it may be less convenient.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    this may be unrelated but if I use a diff function and I get this error:

    1c1
    < usage: ./main [--binary] floatnumber
    ---
    > usage: ./main [--binary] floatnumber


    what does it mean??

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    It means that that line has changed. You probably added some whitespace or changed the newline ending of that line.

    It's not an error. It's indicating that line one on the left (indicated by "<") has changed to line one on the right (">"). "1c1". 1 changed to 1.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    so what should I do to make this diff works

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    yes I did added a \n at that line, but if I remove that it will complaint of not finding a new line.. this is freakin me out

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Look, it's not a problem. The diff is telling you that something has changed. You know that, because you changed it. It's not an error. It's not a problem. It's merely telling you that something changed.

    diff tells you the differences between files. It doesn't give you errors that you have to deal with or anything.

    As for why it complains about not finding newlines at the end of the file -- some UNIX tools work better with files that end with blank lines. GCC, for example, issues a warning if there's no newline at the end of the file.

    It's not really that important. You can have it either way. But don't worry about diff outputs, they just tell you what you did.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    how about this one:

    0a1,3
    > ERROR: sizeof(int_u4) is not 4, it's 8
    > ERROR: sizeof(int_4) is not 4, it's 8
    > aborting


    what does it mean??

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    "0a1,3": At line 0, lines from 1 to 3 have been added. (They don't exist in the left file, but they do in the right.) As far as I can tell. I don't use diff very often . . . .

    Also see Wikipedia. http://en.wikipedia.org/wiki/Diff
    Code:
    0a1,6
    > This is an important
    > notice! It should
    > therefore be located at
    > the beginning of this
    > document!
    >
    8,14c14
    < compress the size of the
    < changes.
    <
    < This paragraph contains
    < text that is outdated.
    < It will be deleted in the
    < near future.
    ---
    > compress anything.
    17c17
    < check this dokument. On
    ---
    > check this document. On
    24a25,28
    >
    > This paragraph contains
    > important new additions
    > to this document.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fprintf to stderr crash programs
    By jlai in forum Windows Programming
    Replies: 2
    Last Post: 04-12-2005, 08:51 AM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21