views:

549

answers:

6

Hi, how would one go about writing the contents of a structure in C++ to a text file? What I want to do is, say I have a structure called A, with data members A.one, A.two, and so on, and I want to write them out line by line into a text file, like so

Structure A
A.one = value
A.two = value ...

Structure B
B.one = value
B.two = value

Basically I want to make a dump.. Is there an easy way to do this? What's the best I/O method for this? CFile? iostream? Thanks.. Is there an easier way to dump all of the values of member data of a class?


EDIT: Crap, thanks guys..

+5  A: 

C++ isn't introspective. That is, it doesn't know anything about the contents of its structures. You have to use some sort of manual annotation or a reflection system to do this.

Of course, if you're OK with hardcoding information about the structures, you can dump the information with any I/O method.

For instance:

struct A
{
  int member1;
  char member2;
};

void PrintA (const A& a)
{
  std::printf ("member1: %d\n", a.member1);
  std::printf ("member2: %c\n", b.member2);
}

As you can see this is tedious because it requires intimate knowledge of the structure, and won't be updated automatically if the structure is changed. But the overhead of a reflection system to do this "properly" can be a disadvantage, especially if you're learning.

Dan Olson
Maybe usage of C++ standart I/O classes would be safer here? If types of member1 and member2 will be changed then your code will work incorrectly.
Dmitriy Matveev
I don't use C++ stream IO as a matter of preference. You're right that it would be marginally safer in this case, but the point is that the code requires careful updating to stay in lockstep with the struct definition, which is true regardless of which IO scheme you prefer.
Dan Olson
+6  A: 

No reflection in C++, so you have to do it manually, eg (using iostream, for simplicty):

dump_A(std::cout, "A", a);

void dump_A(std::iostream& os, const char* name, A const& a)
{
    os << "Structure " << name << '\n'
       << name << ".one = " << a.one << '\n'
       << name << ".two = " << a.two << '\n';
}
Simon Buchan
A: 

This applies more to C than C++, but bear in mind that if you do write a struct directly to file (or a network buffer) then the result will be machine and compiler dependent. Integers will be written in host byte order and compiler padding bytes for the struct will also be included. Attempting to read the buffer on a machine with different endianness or different compiler directives will not work

This'd happen with something like (be forgiving on syntax): write(f, &A, sizeof(A));

Crowley
+3  A: 

I've had success with the boost serialization libraries. Its easy to use and a natural choice for serialization and deserialization of C++ data structures.

Here is a link to the tutorial: http://www.boost.org/doc/libs/1_38_0/libs/serialization/doc/index.html

m-sharp
A: 

IF this is just a struct, a plain old C-compatible data type, with NO virtual methods and NO pointers, then as a short-term kludge you could just use:

struct Foo {  int a, b;  char c, d, e[50]; }  f;
write( fd, & f, sizeof(f) );

However, don't expect this to be cross-platform, or even cross-compiler revisions on the same platform.

I think I've discussed this before...

Mr.Ree
what does the fd mean in write(fd,....)? is that the file handle? it's got some pointers in it.. =/
krebstar
fd == file descriptor, as obtained from open(). If you are using a FILE* (from fopen), you could use fwrite().
Mr.Ree