Usage of rdbuf

Discussion in 'C++' started by Omid, Jul 22, 2004.

  1. Omid

    Omid Guest

    Hi.

    I have a piece of code that can be compiled and linked both with
    Cygwin g++ and with VC++. The code is:

    //WORKS WITH CYGWIN G++
    //BUT NOT WITH CL.EXE (VC++) (compiles, but error when executed)
    //################################
    #include <iostream>
    #include <sstream>
    using namespace std;

    int main ()
    {

    int val;
    string mystr;
    stringstream ss (stringstream::in | stringstream::eek:ut);

    cout.rdbuf(ss.rdbuf()); //Is this ok to do?
    cout << "120 42 377 6 5 2000";

    //If the two lines above are replaced with
    //this line it works fine with both
    //g++ and VC++:
    //ss << "120 42 377 6 5 2000";

    for (int n=0; n<6; n++)
    {
    ss >> val;
    cerr << val*2 << '\n';
    }

    cerr << "Hello World! " << endl;
    return 0;
    }
    //################################



    My problem is that during run time the exe-file that was created with
    Cygwin g++ (3.3.1) works fine, but the exe-file that was created with
    VC++ 6.0 crashes.

    Here is the error message:
    The instruction at "0x00402f7f" referenced memory at "0x0000000sd".
    The memory could not be "read".


    Is there any errors in my code, or do I do something I'm not allowed
    to?
    Since it works in Cygwin g++, but not in VC++ 6.0 I think this is
    really strange, since I believe that my code is 100% ANSI-C++ code. Or
    is it?

    I compile with:
    G++: g++ test.cpp
    VC++: cl /GX /TP test.cpp (also compiled with the entire VC++
    development GUI)

    I'm thankful for any information that might help me with this problem.

    /Omid
     
    Omid, Jul 22, 2004
    #1
    1. Advertising

  2. "Omid" <> wrote in message
    news:...
    > Hi.
    >
    > I have a piece of code that can be compiled and linked both with
    > Cygwin g++ and with VC++. The code is:
    >
    > //WORKS WITH CYGWIN G++
    > //BUT NOT WITH CL.EXE (VC++) (compiles, but error when executed)
    > //################################
    > #include <iostream>
    > #include <sstream>
    > using namespace std;
    >
    > int main ()
    > {
    >
    > int val;
    > string mystr;
    > stringstream ss (stringstream::in | stringstream::eek:ut);
    >
    > cout.rdbuf(ss.rdbuf()); //Is this ok to do?
    > cout << "120 42 377 6 5 2000";
    >
    > //If the two lines above are replaced with
    > //this line it works fine with both
    > //g++ and VC++:
    > //ss << "120 42 377 6 5 2000";
    >
    > for (int n=0; n<6; n++)
    > {
    > ss >> val;
    > cerr << val*2 << '\n';
    > }
    >
    > cerr << "Hello World! " << endl;
    > return 0;
    > }
    > //################################
    >
    >
    >
    > My problem is that during run time the exe-file that was created with
    > Cygwin g++ (3.3.1) works fine, but the exe-file that was created with
    > VC++ 6.0 crashes.
    >
    > Here is the error message:
    > The instruction at "0x00402f7f" referenced memory at "0x0000000sd".
    > The memory could not be "read".
    >
    >
    > Is there any errors in my code, or do I do something I'm not allowed
    > to?
    > Since it works in Cygwin g++, but not in VC++ 6.0 I think this is
    > really strange, since I believe that my code is 100% ANSI-C++ code. Or
    > is it?
    >
    > I compile with:
    > G++: g++ test.cpp
    > VC++: cl /GX /TP test.cpp (also compiled with the entire VC++
    > development GUI)
    >
    > I'm thankful for any information that might help me with this problem.
    >
    > /Omid


    You must restore the old buffer before you exit.

    streambuf* save_buffer = cout.rdbuf(ss.rdbuf()); //Is this ok to do?
    cout << "120 42 377 6 5 2000";
    cout.rdbuf(save_buffer);

    john
     
    John Harrison, Jul 22, 2004
    #2
    1. Advertising

  3. "Omid" <> wrote in message
    news:...
    > Hi.
    >
    > I have a piece of code that can be compiled and linked both with
    > Cygwin g++ and with VC++. The code is:
    >
    > //WORKS WITH CYGWIN G++
    > //BUT NOT WITH CL.EXE (VC++) (compiles, but error when executed)
    > //################################
    > #include <iostream>
    > #include <sstream>
    > using namespace std;
    >
    > int main ()
    > {
    >
    > int val;
    > string mystr;
    > stringstream ss (stringstream::in | stringstream::eek:ut);
    >
    > cout.rdbuf(ss.rdbuf()); //Is this ok to do?
    > cout << "120 42 377 6 5 2000";
    >
    > //If the two lines above are replaced with
    > //this line it works fine with both
    > //g++ and VC++:
    > //ss << "120 42 377 6 5 2000";
    >
    > for (int n=0; n<6; n++)
    > {
    > ss >> val;
    > cerr << val*2 << '\n';
    > }
    >
    > cerr << "Hello World! " << endl;
    > return 0;
    > }
    > //################################
    >
    >
    >
    > My problem is that during run time the exe-file that was created with
    > Cygwin g++ (3.3.1) works fine, but the exe-file that was created with
    > VC++ 6.0 crashes.
    >
    > Here is the error message:
    > The instruction at "0x00402f7f" referenced memory at "0x0000000sd".
    > The memory could not be "read".
    >
    >
    > Is there any errors in my code, or do I do something I'm not allowed
    > to?
    > Since it works in Cygwin g++, but not in VC++ 6.0 I think this is
    > really strange, since I believe that my code is 100% ANSI-C++ code. Or
    > is it?
    >
    > I compile with:
    > G++: g++ test.cpp
    > VC++: cl /GX /TP test.cpp (also compiled with the entire VC++
    > development GUI)
    >
    > I'm thankful for any information that might help me with this problem.
    >
    > /Omid


    I just tried with VS.NET and it compiles and runs cleanly. You can download
    the compiler for free http://msdn.microsoft.com/visualc/vctoolkit2003/.
     
    DM McGowan II, Jul 24, 2004
    #3
  4. Omid

    Omid Guest

    "John Harrison" <> wrote in message news:<>...
    > "Omid" <> wrote in message
    > news:...
    > > Hi.
    > >
    > > I have a piece of code that can be compiled and linked both with
    > > Cygwin g++ and with VC++. The code is:
    > >
    > > //WORKS WITH CYGWIN G++
    > > //BUT NOT WITH CL.EXE (VC++) (compiles, but error when executed)
    > > //################################
    > > #include <iostream>
    > > #include <sstream>
    > > using namespace std;
    > >
    > > int main ()
    > > {
    > >
    > > int val;
    > > string mystr;
    > > stringstream ss (stringstream::in | stringstream::eek:ut);
    > >
    > > cout.rdbuf(ss.rdbuf()); //Is this ok to do?
    > > cout << "120 42 377 6 5 2000";
    > >
    > > //If the two lines above are replaced with
    > > //this line it works fine with both
    > > //g++ and VC++:
    > > //ss << "120 42 377 6 5 2000";
    > >
    > > for (int n=0; n<6; n++)
    > > {
    > > ss >> val;
    > > cerr << val*2 << '\n';
    > > }
    > >
    > > cerr << "Hello World! " << endl;
    > > return 0;
    > > }
    > > //################################
    > >
    > >
    > >
    > > My problem is that during run time the exe-file that was created with
    > > Cygwin g++ (3.3.1) works fine, but the exe-file that was created with
    > > VC++ 6.0 crashes.
    > >
    > > Here is the error message:
    > > The instruction at "0x00402f7f" referenced memory at "0x0000000sd".
    > > The memory could not be "read".
    > >
    > >
    > > Is there any errors in my code, or do I do something I'm not allowed
    > > to?
    > > Since it works in Cygwin g++, but not in VC++ 6.0 I think this is
    > > really strange, since I believe that my code is 100% ANSI-C++ code. Or
    > > is it?
    > >
    > > I compile with:
    > > G++: g++ test.cpp
    > > VC++: cl /GX /TP test.cpp (also compiled with the entire VC++
    > > development GUI)
    > >
    > > I'm thankful for any information that might help me with this problem.
    > >
    > > /Omid

    >
    > You must restore the old buffer before you exit.
    >
    > streambuf* save_buffer = cout.rdbuf(ss.rdbuf()); //Is this ok to do?
    > cout << "120 42 377 6 5 2000";
    > cout.rdbuf(save_buffer);
    >
    > john


    Thanks John!

    That solved my problem.


    /Omid
     
    Omid, Jul 26, 2004
    #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. Bob Smith
    Replies:
    1
    Views:
    564
    tom_usenet
    Oct 16, 2003
  2. wukexin
    Replies:
    1
    Views:
    3,386
    Phlip
    Dec 18, 2003
  3. Siemel Naran
    Replies:
    4
    Views:
    482
    Buster
    Dec 22, 2004
  4. Alex Vinokur

    infile.rdbuf() for empty file

    Alex Vinokur, Feb 2, 2005, in forum: C++
    Replies:
    5
    Views:
    822
    Dietmar Kuehl
    Feb 3, 2005
  5. Replies:
    2
    Views:
    1,421
Loading...

Share This Page