J
John Ratliff
I'm trying to overload the << and >> operators to serialize a class to
disk. It writes things correctly, but it doesn't read them back correctly.
Can someone tell what I'm doing wrong? Here is an example demonstrating
my problem.
------- tmp.cc --------------
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iostream>
class foo {
private:
long keymap[5];
int joymap[5];
int joystick;
bool useJoystick, useDpad;
friend std:stream &operator<<(std:stream &, const foo &);
friend std::istream &operator>>(std::istream &, foo &);
public:
foo(bool init = true);
};
foo::foo(bool init) {
if (init) {
for (int i = 0; i < 5; i++) {
keymap = static_cast<long>(rand() % 1000);
joymap = rand() % 16;
}
joystick = -1;
useJoystick = useDpad = false;
}
}
std:stream &operator<<(std:stream &output, const foo &f) {
for (int i = 0; i < 5; i++) {
output << f.keymap;
output << f.joymap;
}
output << f.joystick << f.useJoystick << f.useDpad;
return output;
}
std::istream &operator>>(std::istream &input, foo &f) {
for (int i = 0; i < 5; i++) {
input >> f.keymap;
input >> f.joymap;
}
input >> f.joystick >> f.useJoystick >> f.useDpad;
return input;
}
int main(int, char **) {
srand(time(NULL));
foo f, f2(false);
std::fstream out("foo.bar",
std::ios_base:ut | std::ios_base::binary);
if (!out) {
std::cerr << "fatal: unable to open foo.bar for writing\n";
return -1;
}
out << f;
out.close();
std::cout << "wrote the following to foo.bar...\n" << f << "\n\n";
std::fstream in("foo.bar",
std::ios_base::in | std::ios_base::binary);
if (!in) {
std::cerr << "fatal: unable to open foo.bar for reading\n";
return -1;
}
in >> f2;
in.close();
std::cout << "read the following from foo.bar...\n" << f2 << '\n';
return 0;
}
-----------------------------
This is the output I get with mingw/g++ 3.4.2
$ g++ -W -Wall -O2 tmp.cc
$ ./a.exe
wrote the following to foo.bar...
252694219161177692989-100
read the following from foo.bar...
-12009252579208987889302009252574400902539976962368368020092919242009145480255255
$ cat foo.bar
252694219161177692989-100
The data is being written correctly, I'm just not sure why I can't do
the reverse when I read it from the file.
Thanks,
--John Ratliff
disk. It writes things correctly, but it doesn't read them back correctly.
Can someone tell what I'm doing wrong? Here is an example demonstrating
my problem.
------- tmp.cc --------------
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iostream>
class foo {
private:
long keymap[5];
int joymap[5];
int joystick;
bool useJoystick, useDpad;
friend std:stream &operator<<(std:stream &, const foo &);
friend std::istream &operator>>(std::istream &, foo &);
public:
foo(bool init = true);
};
foo::foo(bool init) {
if (init) {
for (int i = 0; i < 5; i++) {
keymap = static_cast<long>(rand() % 1000);
joymap = rand() % 16;
}
joystick = -1;
useJoystick = useDpad = false;
}
}
std:stream &operator<<(std:stream &output, const foo &f) {
for (int i = 0; i < 5; i++) {
output << f.keymap;
output << f.joymap;
}
output << f.joystick << f.useJoystick << f.useDpad;
return output;
}
std::istream &operator>>(std::istream &input, foo &f) {
for (int i = 0; i < 5; i++) {
input >> f.keymap;
input >> f.joymap;
}
input >> f.joystick >> f.useJoystick >> f.useDpad;
return input;
}
int main(int, char **) {
srand(time(NULL));
foo f, f2(false);
std::fstream out("foo.bar",
std::ios_base:ut | std::ios_base::binary);
if (!out) {
std::cerr << "fatal: unable to open foo.bar for writing\n";
return -1;
}
out << f;
out.close();
std::cout << "wrote the following to foo.bar...\n" << f << "\n\n";
std::fstream in("foo.bar",
std::ios_base::in | std::ios_base::binary);
if (!in) {
std::cerr << "fatal: unable to open foo.bar for reading\n";
return -1;
}
in >> f2;
in.close();
std::cout << "read the following from foo.bar...\n" << f2 << '\n';
return 0;
}
-----------------------------
This is the output I get with mingw/g++ 3.4.2
$ g++ -W -Wall -O2 tmp.cc
$ ./a.exe
wrote the following to foo.bar...
252694219161177692989-100
read the following from foo.bar...
-12009252579208987889302009252574400902539976962368368020092919242009145480255255
$ cat foo.bar
252694219161177692989-100
The data is being written correctly, I'm just not sure why I can't do
the reverse when I read it from the file.
Thanks,
--John Ratliff