Archived

This topic is now archived and is closed to further replies.

programering

Writing/Reading data types to/from file

Recommended Posts

How can I write and read structure data to/from a file. What arguments/parameters does Fstream''s read(???); and write(???); functions have/take, and what do they return? How to find the data structure type in the file? A wad file? Using tags? Maybe a header/overview at the beginning of the file that tells the order of the structure data types, the offset, where the data begins, and the lenghts of the data. åäö Anton Karlsson Klingis Entertainment Games with silly humor

Share this post


Link to post
Share on other sites
quote:
Original post by programering
How can I write and read structure data to/from a file.

What arguments/parameters does Fstream''s read(???); and write(???); functions have/take, and what do they return?


You can use the built-in iostream operators for primitive datatypes and strings. For classes, you can make your own operator<< and operator>> functions.

quote:
How to find the data structure type in the file?

A wad file?
Using tags?
Maybe a header/overview at the beginning of the file that tells the order of the structure data types, the offset, where the data begins, and the lenghts of the data.


If you know what the fileformat is, then there''s no need to specify it in the file. Just read in fields based on where they''re SUPPOSED to be.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
Hi,

As an example, define a data stream using:

#include <stdio.h>

// Declare variables
FILE* pOutputStream;
sMyStruct Data;

Then open file:

// Open file
pOutputStream = fopen("output.txt", "wb");

You write to the file using fwrite:

// Write to file
fwrite( Data, sizeof(sMyStruct), 1, pOutputStream );

The you close the file:

// Close file
fclose(pOutputStream);

There is also equivelent read commands "fread" for getting in data. Research those commands and you'll find plenty tutorials.

[edited by - RayWilson on March 19, 2003 1:39:56 PM]

Share this post


Link to post
Share on other sites
Do it hierarchically. It is pretty elegant.

I have provided examples of both formatted and unformatted I/O, but there may be problems if you mix them. I also left out some details.


  
struct Foo
{
friend basic_istream & operator >>( basic_istream & s, Foo f );
friend basic_ostream & operator <<( basic_ostream & s, Foo const & f );
int a;
float b;
char * c;

// unformatted read

basic_istream & read( basic_istream & s )
{
s.read( &a, sizeof( a ) );
s.read( &b, sizeof( b ) );

int len;
s.read( &len, sizeof( len ) );
c = new char[ len ];
s.read( c, len );

return s;
}

// unformatted write

basic_ostream & write( basic_ostream & s )
{
s.write( &a, sizeof( a ) );
s.write( &b, sizeof( b ) );

int len = strlen( c ) + 1;
s.write( &len, sizeof( len ) );
s.write( c, len );
return s;
}
};

// formatted read

basic_istream & operator >>( basic_istream & s, Foo & f )
{
s >> f.a >> f.b >> f.c;
return s;
}

// formatted write

basic_ostream & operator <<( basic_ostream & s, Foo const & f )
{
s << f.a << f.b << f.c;
return s;
}

struct Bar
{
friend basic_istream & operator >>( basic_istream & s, Bar & b );
friend basic_ostream & operator <<( basic_ostream & s, Bar const & b );
Foo x;
int y;
float z;

// unformatted read

basic_istream & read( basic_istream & s )
{
x.read( s );
s.read( &y, sizeof( y ) );
s.read( &z, sizeof( z ) );

return s;
}

// unformatted write

basic_ostream & write( basic_ostream & s )
{
x.write( s );
s.write( &y, sizeof( y ) );
s.write( &z, sizeof( z ) );
return s;
}
};

// formatted read

basic_istream & operator >>( basic_istream & s, Bar & b )
{
s >> b.x >> b.y >> b.z;
return s;
}

// formatted write

basic_ostream & operator <<( basic_ostream & s, Bar const & b )
{
s << b.x << b.y << b.z;
return s;
}

rw_formatted()
{
ifstream in;
ofstream out;
Bar xyzzy;
...
// formatted read

in >> xyzzy;
...
// formatted write

out << xyzzy;
};


rw_unformatted()
{
ifstream in;
ofstream out;
Bar xyzzy;
...
// unformatted read

xyzzy.read( in );
...
// unformatted write

xyzzy.write( out );
};


Share this post


Link to post
Share on other sites
But in C with Fstream.

struct data
{
float w;
short y;
};

ofstream output("file_name.dat");
struct data dat = {99.32, 122};
output.write(&dat, 0, sizeof(dat));

ifstream input("file_name.dat");
input.read(&dat, 0, sizeof(dat));

Correct me if I''m wrong.
I think it shall be a reinterpret_cast, which data type shall I take? void, byte or char?

Is this right?:
output.write( /* pointer to the data to write */ ,
/* offset into the file */ ,
/* the size of the data to write */ );

input.read( /* pointer to the data to read to */ ,
/* offset into the file */ ,
/* the size of the data to read */ );



Anton Karlsson
Klingis Entertainment
Games with silly humor

Share this post


Link to post
Share on other sites
fstreams close themselves when they go out of scope. And the read/write functions take pointers to char. You rarely use void in C++.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]

Share this post


Link to post
Share on other sites