std::stringstream fails to read what it just wrote!

Why does this not work when it's C counterpart does work? I would much rather use the >> and << operators, but read and write are not working as they should, and it makes me wonder why I use C++ for input/output at all with python and other languages about.

FAILED:::::
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <cassert>
#include <sstream>
#include <iostream>
#include <cstdint>
#include <cstdio>

using namespace std;

int main() {
  
  FILE * file = tmpfile();
  stringstream ss( ios::binary );
  
  uint32_t ints[10] = { 0,1,2,3,4,5,6,7,8,9 };
  uint32_t ints2[10];
  
  for( int i = 0; i != 10; ++i ) {
    ints[i] = i;
  }
  for( int i = 0; i != 10; ++i ) {
    //fwrite( (void*)&ints[i], sizeof(uint32_t), 1, file );
    ss.write( (char*)&ints[i], sizeof(uint32_t) );
  }
  
  fseek( file, 0, SEEK_SET );
  ss.seekp( ios::beg );
  
  for( int i = 0; i != 10; ++i ) {
    //fread( (void*)&ints2[i], sizeof(uint32_t), 1, file );
    ss.read( (char*)&ints2[i], sizeof(uint32_t) );
  }
  for( int i = 0; i != 10; ++i ) {
    assert( ints[i] == ints2[i] );
  }
  
  return 0;
}



SUCCESS:::::
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <cassert>
#include <sstream>
#include <iostream>
#include <cstdint>
#include <cstdio>

using namespace std;

int main() {
  
  FILE * file = tmpfile();
  stringstream ss( ios::binary );
  
  uint32_t ints[10] = { 0,1,2,3,4,5,6,7,8,9 };
  uint32_t ints2[10];
  
  for( int i = 0; i != 10; ++i ) {
    ints[i] = i;
  }
  for( int i = 0; i != 10; ++i ) {
    fwrite( (void*)&ints[i], sizeof(uint32_t), 1, file );
    //ss.write( (char*)&ints[i], sizeof(uint32_t) );
  }
  
  fseek( file, 0, SEEK_SET );
  ss.seekp( ios::beg );
  
  for( int i = 0; i != 10; ++i ) {
    fread( (void*)&ints2[i], sizeof(uint32_t), 1, file );
    //ss.read( (char*)&ints2[i], sizeof(uint32_t) );
  }
  for( int i = 0; i != 10; ++i ) {
    assert( ints[i] == ints2[i] );
  }
  
  return 0;
}
I guess it's seekg() // Note the 'g' on line 26
What happens if you do stringstream ss;
instead of stringstream ss( ios::binary );


EDIT:
The problem is that you only specify ios::binary as the mode for the stringstream.
If you do not specify any mode, then the default parameters for the mode is ios::in and ios::out.

So because you only specified the binary mode, then in and out modes are not set , so the stringstream does nothing.

If you want the stringstream to have binary mode as well then you will have to specicy all the required modes yourself - so really you should write:
stringstream ss( ios_base::in | ios_base::out|ios::binary );

Refer to the std::basic_stringstream constructor.

PS:
The binary mode is redundant in your case - binary mode is to do with carriage returns and line feeds (line endings) - so as I originally said just stringstream ss; will be sufficient
Last edited on
Topic archived. No new replies allowed.