Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CreateFile

Status
Not open for further replies.

simmeone

Programmer
Mar 16, 2000
29
DE
Hi,

I have a problem with the CreateFile function. The function create a file, thats OK, but, I realy don't know, how I can write any data into the file.
In the MSDN stands anything with FriteFile aso, but at the moment, I can't understand this.
Have anybody a clue, how this works ?

Thank you very much.

SiM [sig][/sig]
 
If you want to create files and write/read data, I would suggest you use the ifstream and ofstream functions. The good things with them is that you can write and read whole structures of data in one single call.

Code:
#include <fstream.h>

struct Test
{
public:
   int iVar;
   char cVar;
};

Test test_Struct;

void main()
{
   test_Struct.iVar = 45;
   test_Struct.cVar = 'H';

   // Write data to &quot;file.txt&quot;
   ofstream fout(&quot;file.txt&quot;,ios::binary);
   fout.write((char*)test_Struct, sizeof(Test));
   fout.close();

   // Read data from &quot;file.txt&quot;
   ifstream fin(&quot;file.txt&quot;,ios::binary);
   fin.read((char*)test_Struct, sizeof(Test));
   fin.close();
}
[sig][/sig]
 
Thank You very much, your source is a good help. Thanks.

SiM [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top