Click to See Complete Forum and Search --> : Wiriting Object Data to txt File!!
andrew_zep
December 14th, 2004, 05:05 AM
Hi,
I'm relatively new to VC++ coding.
I need help, saving contents of an object to a txt file. I would appriciate if anyone can help me with the code to writing output to a txt file.
Thanks!!
Ajay Vijay
December 14th, 2004, 05:36 AM
It is better to use binary file instead of text file to write objects. I assume that you mean class/struct 'object'. Here is sample code:
struct YourClass
{
double x;
char str[50];
};
void main()
{
ostream f("somefile",ios::append | ios::binary);
YourClass yc;
yc.x=67.405;
strcpy(yc.str,"anystring");
f.write((char*)&yc,sizeof(yc)); // Write entire object
}Similarly use read (if using fstream for file IO) to read binary contents.
andrew_zep
December 14th, 2004, 05:54 AM
I was probably interpreted as being wanting to save the object state. Instead, I want to write data members of the object (int, float etc) to a text file.
Thanks!!
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.