Serialize signed values

I am trying to serialize data using a stringstream.
I ran the following test, and it failed at
CHECK_EQUAL(result6, check6);
What am I missing here?

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
TEST(SerializeTest, basic)
{
	std::stringstream stream;

	int8_t check1 = -120;
	int16_t check2 = -16666;
	int32_t check3 = -12345678;
	int64_t check4 = -8877665544332211;
	uint8_t check5 = 120;
	uint16_t check6 = 16666;
	uint32_t check7 = 12345678;
	uint64_t check8 = 8877665544332211;
	float check9 = 66.5544332211;
	double check0 = 66.5544332211;
	std::string delim = "delim";

	int8_t result1 = 0;
	int16_t result2 = 0;
	int32_t result3 = 0;
	int64_t result4 = 0;
	uint8_t result5 = 0;
	uint16_t result6 = 0;
	uint32_t result7 = 0;
	uint64_t result8 = 0;
	float result9 = 0;
	double result0 = 0;
	std::string string1;
	std::string string2;
	std::string string3;

	stream << check1;
	stream << check2;
	stream << check3;
	stream << check4;
	stream << delim << std::string(";");

	stream << check5;
	stream << check6;
	stream << check7;
	stream << check8;
	stream << delim << std::string(";");

	stream << check9;
	stream << check0;
	stream << delim << std::string(";");

	TFString(stream.tellg()).print();

	stream >> result1;
	stream >> result2;
	stream >> result3;
	stream >> result4;
	std::getline(stream, string1, ';');

	stream >> result5;;
	stream >> result6;
	stream >> result7;
	stream >> result8;
	std::getline(stream, string2, ';');

	stream >> result9;
	stream >> result0;
	std::getline(stream, string3, ';');

	CHECK_EQUAL(result1, check1);
	CHECK_EQUAL(result2, check2);
	CHECK_EQUAL(result3, check3);
	LONGS_EQUAL(result4, check4);

	CHECK_EQUAL(result5, check5);
	CHECK_EQUAL(result6, check6);
	CHECK_EQUAL(result7, check7);
	LONGS_EQUAL(result8, check8);
	DOUBLES_EQUAL(result9, check9, 0.1);
	DOUBLES_EQUAL(result0, check0, 0.1);

	STRCMP_EQUAL(delim.c_str(), string1.c_str());
	STRCMP_EQUAL(delim.c_str(), string2.c_str());
	STRCMP_EQUAL(delim.c_str(), string3.c_str());
}
Last edited on
You're writing text without delimiters, so your stream is a contiguous block of numbers, more or less. The read is trying to read the whole thing, which isn't what you want.

You can write contiguous blocks to a binary stream, where the lengths are implied, or you can write text with a space or tab between fields. You can use any delimiter, but it's a little bit more work, and less readable during debug.
stream << and >> are not reciprocal. Just because some data was stream inserted using << doesn't mean that the same data will/can be stream extracted using >>
Thank you for helping.

Would you recommend using a binary stream instread?
If I use space as a delimiter, would it then support strings that contain spaces?
Is std::getline() the best way to retrieve a string?
I'd suggest reading this.
https://isocpp.org/wiki/faq/serialization

If you choose text, then floats and doubles will not read back the same as you wrote them out. Floating point is already imprecise, and the text representation of them is even worse.

If you choose binary, then anything like std::string or any other C++ class thing becomes tricky to deal with.
This link looks really good.
Thank you very much. :-)
Topic archived. No new replies allowed.