7

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..

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
krebstar
  • 3,956
  • 8
  • 46
  • 64
  • Related: * [http://stackoverflow.com/questions/234724/how-to-serialize-in-c](http://stackoverflow.com/questions/234724/how-to-serialize-in-c) * [http://stackoverflow.com/questions/523872/how-do-you-serialize-an-object-in-c](http://stackoverflow.com/questions/523872/how-do-you-serialize-an-object-in-c) – dmckee --- ex-moderator kitten Feb 27 '09 at 02:13

5 Answers5

8

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
  • 12,707
  • 2
  • 48
  • 55
5

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
  • 22,849
  • 4
  • 42
  • 56
  • 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. – okutane Feb 27 '09 at 02:17
  • 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 Feb 27 '09 at 02:20
3

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
  • 16,443
  • 1
  • 26
  • 26
0

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
  • 51
  • 4
0

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...

Community
  • 1
  • 1
Mr.Ree
  • 8,320
  • 27
  • 30
  • what does the fd mean in write(fd,....)? is that the file handle? it's got some pointers in it.. =/ – krebstar Feb 27 '09 at 13:56
  • fd == file descriptor, as obtained from open(). If you are using a FILE* (from fopen), you could use fwrite(). – Mr.Ree Feb 28 '09 at 12:48