kelvSYC said:
As long as there are no virtual methods or non pod members, it should
still work.
I see. Adding a nonvirtual member function would not affect its
pod-ness.
So what would I do if I had something like
struct bar {
int a;
char b[32];
};
and I wanted to add a constructor/non-pod member/virtual member
function/etc. to make it non-pod? How would I change the code snippet
above?
But I'd give it proper streaming operators.
friend std:

stream& operator<<( std:

stream& out, const foo& f );
friend std::istream& operator>>( std::istream& out, foo& f );
I thought streaming operators were meant for text input and output
only. std::fwrite is binary output.
No sir, those operators are binary.
As far as a non-pod class is concerned, why not develop a Bar class and a
container to serialize the data. A std::string is implemented instead of a
fixed char array:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <iterator>
#include <stdexcept>
class Bar
{
int m_n;
std::string m_s;
public:
Bar(int n, std::string s) : m_n(n), m_s(s) { }
~Bar() { }
friend std:

stream& operator<<( std:

stream& os, const Bar& r_bar_ )
{
os << r_bar_.m_n << " ";;
os << r_bar_.m_s;
return os;
}
friend std::istream& operator>>( std::istream& is, Bar& r_bar )
{
is >> r_bar.m_n >> r_bar.m_s;
return is;
}
}; // class Bar
class BarContainer
{
std::string s_filename;
std::vector<Bar> v_bar;
public:
BarContainer(std::string s) : v_bar(), s_filename(s) { }
~BarContainer() { }
// member functions
void push_back(const Bar& r_bar_)
{
v_bar.push_back(r_bar_);
}
void write() const
{
std:

fstream ofs;
ofs.open(s_filename.c_str());
if (!ofs)
{
throw std::exception("while opening file for output");
}
std:

stream& os = ofs;
std::copy( v_bar.begin(),
v_bar.end(),
std:

stream_iterator<Bar>(os, "\n") );
ofs.close();
std::cout << "\nBarContainer::write() ... file ";
std::cout << s_filename << " created successfully.\n";
} // write()
void display() const
{
std::cout << "BarContainer::display() [ size = ";
std::cout << v_bar.size() << " ]\n";
std:

stream& r_os = std::cout;
std::copy( v_bar.begin(),
v_bar.end(),
std:

stream_iterator<Bar>(r_os, "\n") );
/*
// the above code is simpler than...
typedef std::vector<Bar>::iterator VITER;
VITER viter = v_bar.begin();
for (viter; viter != v_bar.end(); ++viter)
{
std::cout << *viter << "\n";
}
*/
} // display()
}; // class Bar
int main()
{
// initialize Container with filename
BarContainer bc("data.dat");
// load Container
bc.push_back(Bar(0, "string_zero"));
bc.push_back(Bar(1, "string_one"));
bc.push_back(Bar(2, "string_two"));
bc.push_back(Bar(3, "string_three"));
bc.push_back(Bar(4, "string_four"));
// display its contents
bc.display();
try
{
// serialize Container to file
bc.write();
}
catch (const std::exception& e)
{
std::cerr << "Error !\n";
std::cerr << e.what() << "\n";
}
return 0;
} // main(...)
/*
BarContainer::display() [ size = 5 ]
0 string_zero
1 string_one
2 string_two
3 string_three
4 string_four
BarContainer::write() ... file data.dat created successfully.
*/