Writing/Reading data types to/from file

Started by
8 comments, last by programering 21 years ago
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
Advertisement
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.
An example, please!

Anton Karlsson
Klingis Entertainment
Games with silly humor
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]
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 readbasic_istream & operator >>( basic_istream & s, Foo & f ){    s >> f.a >> f.b >> f.c;    return s;}// formatted writebasic_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 readbasic_istream & operator >>( basic_istream & s, Bar & b ){    s >> b.x >> b.y >> b.z;    return s;}// formatted writebasic_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 );};  


John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
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
The streams library isn''t part of C, it''s part of C++.
You forgot to close the files. And I would say you should go for char because BYTE (should be) is a typedef of char.

.lick
Can't I take reinterpret_cast< void > ?

Anton Karlsson
Klingis Entertainment
Games with silly humor

[edited by - programering on March 21, 2003 4:44:06 AM]
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]

This topic is closed to new replies.

Advertisement