freopen & cout

Discussion in 'C++' started by Alex Vinokur, Dec 2, 2003.

  1. Alex Vinokur

    Alex Vinokur Guest

    Hi,

    Where has output-to-cout gone in the program below?

    Thanks,

    ========= C++ code : File foo.cpp : BEGIN =========
    #include <cstdio>
    #include <cassert>
    #include <iostream>
    #include <fstream>
    using namespace std;

    #define FILE_NAME "foobar"

    int main ()
    {
    ofstream ofs;

    remove (FILE_NAME);

    ofs.open (FILE_NAME);
    assert (ofs);
    assert (ofs.is_open());

    FILE* stdof = freopen (FILE_NAME, "w", stdout);
    assert (stdof);

    cout << "to cout (first time)" << endl; // Problematic line
    ofs << "to ofs (first time)" << endl;
    cout << "to cout (second time)" << endl; // Problematic line
    ofs << "to ofs (second time)" << endl;

    ofs.close();
    assert (!ofs.is_open());

    int rc = fclose (stdof);
    assert (!rc);

    return 0;
    }
    ========= C++ code : File foo.cpp : END ===========


    ========= Compilation & Run : BEGIN =========

    $ g++ -v
    [---omitted---]
    gcc version 3.3.1 (cygming special)

    $ g++ -W -Wall foo.cpp

    $ a
    // No output; Where has output-to-cout gone?

    $ cat foobar
    to ofs (first time)
    to ofs (second time)

    ========= Compilation & Run : END ===========


    --
    =====================================
    Alex Vinokur
    mailto:
    http://up.to/alexvn
    =====================================
    Alex Vinokur, Dec 2, 2003
    #1
    1. Advertising

  2. Alex Vinokur

    Jack Klein Guest

    On Tue, 2 Dec 2003 09:30:33 +0200, "Alex Vinokur" <>
    wrote in comp.lang.c++:

    > Hi,
    >
    > Where has output-to-cout gone in the program below?
    >
    > Thanks,
    >
    > ========= C++ code : File foo.cpp : BEGIN =========
    > #include <cstdio>
    > #include <cassert>
    > #include <iostream>
    > #include <fstream>
    > using namespace std;
    >
    > #define FILE_NAME "foobar"
    >
    > int main ()
    > {
    > ofstream ofs;
    >
    > remove (FILE_NAME);
    >
    > ofs.open (FILE_NAME);
    > assert (ofs);
    > assert (ofs.is_open());
    >
    > FILE* stdof = freopen (FILE_NAME, "w", stdout);
    > assert (stdof);
    >
    > cout << "to cout (first time)" << endl; // Problematic line
    > ofs << "to ofs (first time)" << endl;
    > cout << "to cout (second time)" << endl; // Problematic line
    > ofs << "to ofs (second time)" << endl;
    >
    > ofs.close();
    > assert (!ofs.is_open());
    >
    > int rc = fclose (stdof);
    > assert (!rc);
    >
    > return 0;
    > }
    > ========= C++ code : File foo.cpp : END ===========
    >
    >
    > ========= Compilation & Run : BEGIN =========
    >
    > $ g++ -v
    > [---omitted---]
    > gcc version 3.3.1 (cygming special)
    >
    > $ g++ -W -Wall foo.cpp
    >
    > $ a
    > // No output; Where has output-to-cout gone?
    >
    > $ cat foobar
    > to ofs (first time)
    > to ofs (second time)
    >
    > ========= Compilation & Run : END ===========


    Ask in a compiler/OS specific newsgroup. The language has nothing at
    all to say about what happens when you try to open a file for output
    more than once simultaneously. Even if you do it entirely with C++
    streams or C stdio FILE pointers.

    This is not a language issue.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
    Jack Klein, Dec 3, 2003
    #2
    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. abi

    man cout or info cout

    abi, Jun 27, 2003, in forum: C++
    Replies:
    2
    Views:
    1,633
  2. Pmb

    std::cout vs cout

    Pmb, Jun 2, 2004, in forum: C++
    Replies:
    2
    Views:
    4,389
    Leor Zolman
    Jun 2, 2004
  3. Replies:
    4
    Views:
    307
    Howard
    Sep 15, 2005
  4. Stefan Ram

    Re: cout vs std::cout

    Stefan Ram, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    459
  5. Hendrik Schober

    Re: cout vs std::cout

    Hendrik Schober, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    423
    Jerry Coffin
    Oct 7, 2008
Loading...

Share This Page