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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I read and write integers to a file from an array?

Status
Not open for further replies.

lordhuh

Programmer
Apr 25, 2000
96
US
me again with my c++ final project. i figured out the rand number thing (thank you planet source code). now im stumed again.

i have a file that store info for an arrayed structure. some is int some is chararcter strings. my c++ class only covered fileio with strings not with integers. how do i go about reading and writing the integers to files. thanks
Karl Pietri
 
In most cases you would write an int to a file the same way. What method are you using to write the strings to the file?
 
Here is just some code I'm going to throw at ya, just to give you an idea of what you could do.


#include <fstream.h>

fstream *File;

struct foobar {
char character;
int x, y;
};

int main(void)
{

foobar *fubar;
fubar->character = 'A';
fubar->x = 1;
fubar->y = 2;

File->open(&quot;test.dat&quot;, ios::eek:ut);
File->write((char *)&fubar, sizeof(fubar));
File->close();

return 0;

}


This might be what your looking for, to write the entire structure to a file at once.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top