Writing to null?

Discussion in 'C++' started by Bryan, Apr 19, 2007.

  1. Bryan

    Bryan Guest

    In unix if I want to dump some output into never never land I can do
    something like:
    std::eek:fstream outfile("/dev/null");

    What is the equivalent in windows? /dev/null seems not to work properly.

    Is it std::eek:fstream outfile("NUL")?

    Thanks,
    B
     
    Bryan, Apr 19, 2007
    #1
    1. Advertising

  2. Bryan

    Mark P Guest

    Bryan wrote:
    > In unix if I want to dump some output into never never land I can do
    > something like:
    > std::eek:fstream outfile("/dev/null");
    >
    > What is the equivalent in windows? /dev/null seems not to work properly.
    >
    > Is it std::eek:fstream outfile("NUL")?
    >
    > Thanks,
    > B


    Not certain of this, but I would expect that you can simply default
    construct an ofstream without specifying a file name.
     
    Mark P, Apr 19, 2007
    #2
    1. Advertising

  3. Bryan wrote:
    > In unix if I want to dump some output into never never land I can do
    > something like:
    > std::eek:fstream outfile("/dev/null");
    >
    > What is the equivalent in windows? /dev/null seems not to work properly.
    >
    > Is it std::eek:fstream outfile("NUL")?


    Don't know about now, but back in the old DOS days you could
    indeed safely write to a special file/device called "NUL".

    I am not sure that you are 100% guaranteed to be able
    to bind an std::eek:fstream to a "special file", but I would expect
    it to work.

    HTH,
    - J.
     
    Jacek Dziedzic, Apr 19, 2007
    #3
  4. Bryan

    James Kanze Guest

    On Apr 19, 2:13 am, Mark P <>
    wrote:
    > Bryan wrote:
    > > In unix if I want to dump some output into never never land I can do
    > > something like:
    > > std::eek:fstream outfile("/dev/null");


    > > What is the equivalent in windows? /dev/null seems not to work properly.


    > > Is it std::eek:fstream outfile("NUL")?


    > Not certain of this, but I would expect that you can simply default
    > construct an ofstream without specifying a file name.


    Yes, but that will result in an error; typically, an output
    error will cause the code to fail. (Output errors are normally
    fairly serious, except for secondary output, like logging.)

    It's pretty simple to define a nulstreambuf, which emulates the
    behavior of "/dev/null" exactly. (EOF for all reads, success
    for all writes.) Then just create an ostream with this as the
    streambuf.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Apr 19, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kivak Wolf
    Replies:
    2
    Views:
    10,118
    Kivak Wolf
    Jun 28, 2005
  2. vizlab
    Replies:
    3
    Views:
    4,277
    Michael Bar-Sinai
    Oct 17, 2007
  3. Replies:
    16
    Views:
    7,436
    Mike Schilling
    Oct 12, 2005
  4. Replies:
    5
    Views:
    27,592
    Mike Schilling
    Mar 29, 2006
  5. Bo Peng
    Replies:
    13
    Views:
    13,488
    Siemel Naran
    Jul 18, 2004
Loading...

Share This Page