how do you write to stderr

This is a discussion on how do you write to stderr within the C Programming forums, part of the General Programming Boards category; How do I write: ("Country %d and country %d are not connected\n", d1, d2); ("Returning to the main menu"); to ...

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

    how do you write to stderr

    How do I write:

    ("Country %d and country %d are not connected\n", d1, d2);
    ("Returning to the main menu");


    to the file stderr, assuming that d1 and d2 already exists above??

    I tried to use fprintf but it prints out the sentence too.

    I tried to to this but it also prints out the Country.... bla bla bla, how do I just write it to stderr without having to print it?
    fprintf(stderr, "Country %d and country %d are not connected\n", d1, d2);
    fprintf(stderr, "Returning to the main menu");
    Last edited by -EquinoX-; 02-12-2008 at 01:31 PM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,670
    Are you saying you don't want stderr output to show up on the terminal/console output?

    gg

  3. #3
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    If that is the case, if you want to redirect the output of stderr to somewhere else, you would use freopen(), on stderr.
    Check freopen reference for details on how to do it.
    Last edited by xuftugulus; 02-12-2008 at 05:50 PM.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by -EquinoX- View Post
    How do I write:

    ("Country %d and country %d are not connected\n", d1, d2);
    ("Returning to the main menu");


    to the file stderr, assuming that d1 and d2 already exists above??

    I tried to use fprintf but it prints out the sentence too.

    I tried to to this but it also prints out the Country.... bla bla bla, how do I just write it to stderr without having to print it?
    fprintf(stderr, "Country %d and country %d are not connected\n", d1, d2);
    fprintf(stderr, "Returning to the main menu");
    Well, first of all, stderr is not a file. It's a stream. When you send data to it via functions line fprintf(), they get written to where ever the stream is pointing. Typically, that's the console.

    Now, you are very confusing when you ask how do I just write it to stderr without having to print it?, that is illogical. To have output show up on stderr, you have to print to it.

    stderr is no different (functionally) that stdout. How do you write to stdout without printing? You don't!

    Printing here, encapsulates printf(), fprintf(), putc(), fputc(), blah blah blah.

    What are you trying to do?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    what I mean is how do I redirect that string "Country.... bla bla bla" to stderr

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    fprintf(), just like you are doing. By default, stdout and stderr go to the same place. You can redirect stderr via a command line switch. See your OS doc.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,670
    You can also redirect stderr to a real file with freopen(), which would prevent it from showing on to the terminal/console.

    gg

  8. #8
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Code:
    what I mean is how do I redirect that string "Country.... bla bla bla" to stderr
    Or you could try what xuftugulus said and use reopen. There's an example given on how to use it in the link.

    So you'll have some portable code (i didn't look at the rest of your code, tough) instead of using some OS specific function call.

    (Oops, too slow)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-18-2006, 10:25 AM
  2. program to make a floppy write protected
    By shrijeetp in forum C Programming
    Replies: 1
    Last Post: 10-03-2005, 06:00 AM
  3. Reroute where programs write to
    By willc0de4food in forum C Programming
    Replies: 7
    Last Post: 09-21-2005, 04:48 PM
  4. Function to write string 3 times
    By Giggs in forum C++ Programming
    Replies: 15
    Last Post: 12-24-2002, 03:00 PM
  5. write in c
    By PutoAmo in forum C Programming
    Replies: 6
    Last Post: 04-03-2002, 06:53 PM

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