Thread: how do you write to stderr

  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 02:31 PM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    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 06:50 PM.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    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?
    Mainframe assembler programmer by trade. C coder when I can.

  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
    Chappell Hill, Texas
    Posts
    2,332
    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.
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    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, 11: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, 04:00 PM
  5. write in c
    By PutoAmo in forum C Programming
    Replies: 6
    Last Post: 04-03-2002, 07:53 PM