KeeperMustDie

Hello, I am new on c++ forms prgraming. I just faced a problem with writing structure or class to file and cant fnd answer. Can anyone plz tell me ho to do that

Re: Visual C++ General Writing data to file

JamesSexton

KeeperMustDie,

The functions of Microsoft that you'll probably use the most are:

- CreateFile()
- SetFilePointer()
- ReadFile()
- SetFilePointer()
- WriteFile()
- CloseHandle()

Now, be careful...! You are getting into the "real beans" of programming. Stuff like:

- NULL pointers
- INVALID_HANDLE_VALUE

Best read up a little on what files are all about, the do(s) and don't(s) and what kind of trouble you can get into before trying to write or read your data.

Now, take your time. Be patient. Don't be discouraged.  The KeeperMustLive...!

JamesSexton

 






Re: Visual C++ General Writing data to file

KeeperMustDie

Yes I am new in c++ forms and programing with files. But I just want to write structure to file thats all! later I will load it with <stdio.h>. Can someone plz say how to do that



Re: Visual C++ General Writing data to file

TilakGopi

Hi,

u can just use 'fread' and 'fwrite' for reading & writing structures.

Thanx,

CH.T.Gopi Kumar.






Re: Visual C++ General Writing data to file

yxrkt

If you insist on using stdio.h, you will have to use fread/fwrite. But if you want to use C++ methods, you should include fstream and use std::ifstream and std::ofstream to read and write.



Re: Visual C++ General Writing data to file

Simple Samples

One term used to refer to writing structures and classes to files is serialize, so you might want to search for that.

Your question is not clear, though. Do you need to write just one instance of one structure or class or multiple instances of one structure or class or more complicated combinations Something that is important is whetehr you need to have dynamic creation of objects. Another important question is whether you will be writing all data from a structure or class in the order they appear in the definition or do you need to write the data in a pre-determined format different from the C++ definitions.






Re: Visual C++ General Writing data to file

KeeperMustDie

Well I just want to know how to write structure like this:

struct Object{

char name[50];

int x;

int y;

float value;

};

to file using system::io. Writing with <stdio.h> it should be look like this:

FILE *ofile;

Object obj;

ofile = fopen( "test.dat", "wb");

fwrite(&obj, sizeof(Object), 1, ofile);

fclose(ofile);





Re: Visual C++ General Writing data to file

TilakGopi

Hi,

This will work, (i did in the same way , long time back ,by writing arrays of structures using fwrite).Be sure that structure object filled with valid data.

 

Thanx,

Ch.T.Gopi kumar.






Re: Visual C++ General Writing data to file

johnmarco

Also be sure to precede your struc declaration with a

#pragma pack( 1 )

and after the struct defintion close the pragma with

#pragma pack( )

This ensures your fields are laid out sequentially in memory with no gaps between them, so you can just write the whole thing out in one step. If you leave off the pragma pack and then print it out in one shot, you'll get unexpected gaps in the data and it won't be read in correctly.





Re: Visual C++ General Writing data to file

JamesSexton

The Keeper may die and few of you other fellows

When writing structures to files in C or C++ there are a couple of things to keep in mind besides #pragma pack (1)

1) the size of a char, int, long, etc...

2) then memset the structure before filling it up

3) then memset the structure before reading into it

4) then pay attention to the return values of low level file functions like:

fopen();

fread();

fseek();

fwrite();

fclose();

5) and lastly, if the program is a simple one and not complicated then #pragma pack (1) the whole blessed program instead of trying to pack and un-pack structures, which is just asking for trouble elsewhere. eg. the 1st line of your header.h should be #pragma pack (1) and that's all

May the Keeper Live, and read chapter 6 of "The C Programming Language" by Kernighan and Ritchie

JamesSexton