#83 ostrstream.str() problem in Windows xl

closed-invalid
gcc (462)
2002-08-05
2001-10-24
Anonymous
No

I have to deal with a binary file from an outside
source that I need to tweak for my purposes. In the
program below, I'm converting an integer to formatted
output to a string, copying the string to another and
outputting the result. In Windows NT the program
produces the output below:

***********************PROGRAM********************
C:>type iostreamX.cpp
#include <iostream>
#include <fstream>
#include <strstream>
#include <string>
#include <iomanip>
using namespace std;
int main(){
int i=123;
cout.width(5);
cout.fill('0');
cout << i << endl;
ostrstream ost;
ost.width(5);
ost.fill('0');
ost << i;
cout << ost.str() << endl;
string mystring="xxxxxxxxxxxxxxxxxxxxxxxx";
cout << mystring << endl;
mystring.replace(3,5,ost.str(),0,5);
cout << mystring << endl;
mystring.replace(12,1,ost.str(),0,5);
cout << mystring << endl;
ofstream out("test.txt",ios::out|ios::bin);
out << ost.str();
out.write((char*) &i, sizeof(i));
out.close();
return 0;
}
C:>g++ iostreamx.cpp
***********************OUTPUT********************
C:>a
00123
00123
xxxxxxxxxxxxxxxxxxxxxxxx
xxx00123xxxxxxxxxxxxxxxx
xxx00123xxxx00123xxxxxxxxxxx

*************************************************
*************************************************

IN WINDOWS XL, THE OUTPUT IS
00123
00123[04][61]
xxxxxxxxxxxxxxxxxxxxxxxx
xxx00123xxxxxxxxxxxxxxxx
xxx00123xxxx00123xxxxxxxxxxx

WHERE THE [04][61] IN THE SECOND LINE STANDS FOR TWO
EXTRA BYTES CONTAINING ASCII CHARACTERS 4 AND 61
DECIMAL. APPARENTLY IN WINDOWS XL, MINGW DOESN'T KNOW
HOW LONG THE STRING IS, WHILE IN NT IT DOES. AS AN
UNRECONSTRUCTED FORTRAN PROGRAMMER, I FREQUENTLY
FUMBLE WITH THE SIMPLEST MECHAINCS OF C++ PROGRAMMING,
BUT I DON'T THINK IT'S ME THIS TIME. OF COURSE, THE
SAME THINGS CAN BE ACCOMPLISHED WITH MEMMOVE AND C
INPUT/OUTPUT AND I HAVE A VERSION LIKE THAT WHICH
WORKS IN BOTH NT AND XL. (FOR THAT MATTER, I HAVE A
VERSION IN FORTRAN THAT WORKS IN NT AND XL WITH THE
g77 COMPILER.)

Tom Messenger

Discussion

  • Nobody/Anonymous

    Logged In: NO

    OOPS! I meant WINDOWS XP throughout, of course.
    Tom Messenger

     
  • Danny Smith

    Danny Smith - 2001-10-25

    Logged In: YES
    user_id=11494

    I don't have XP so I can't test, but this looks very
    similar to some fseek problems on W9x. Would you be
    willing to try a rebuild of libstdc++.a to test if that is
    the case?

    BTW what does GetVersionEx return in dwPlatformId field of
    OSVERSIONINFO for Windows XP

    Danny

     
  • Torsten Rennett

    Torsten Rennett - 2001-10-29

    Logged In: YES
    user_id=362093

    The bug is in the C++ program: the line
    ost << i;
    has to be changed to
    ost << i << ends;
    or the c_string returned by 'ost.str()' will not be
    terminated with '\0'.

    Torsten Rennett

     
  • Earnie Boyd

    Earnie Boyd - 2002-08-05
    • assigned_to: nobody --> earnie
    • status: open --> closed-invalid
     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks