Click here to Skip to main content
15,922,650 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralIPersistStorage::Load() for Embeded object of Word Creates .doc file in Temp Pin
anshumanshinde5-Apr-04 2:47
anshumanshinde5-Apr-04 2:47 
GeneralRe: IPersistStorage::Load() for Embeded object of Word Creates .doc file in Temp Pin
Mike Dimmick5-Apr-04 6:37
Mike Dimmick5-Apr-04 6:37 
Generalread a certain line from a text file Pin
dairiseky5-Apr-04 2:44
dairiseky5-Apr-04 2:44 
GeneralRe: read a certain line from a text file Pin
avenger_sb255-Apr-04 3:42
avenger_sb255-Apr-04 3:42 
GeneralRe: read a certain line from a text file Pin
dairiseky5-Apr-04 4:01
dairiseky5-Apr-04 4:01 
GeneralRe: read a certain line from a text file Pin
avenger_sb255-Apr-04 4:46
avenger_sb255-Apr-04 4:46 
GeneralRe: read a certain line from a text file Pin
dairiseky5-Apr-04 5:18
dairiseky5-Apr-04 5:18 
GeneralRe: read a certain line from a text file Pin
avenger_sb255-Apr-04 7:10
avenger_sb255-Apr-04 7:10 
See, the best way (that i feel) of reading and writing srtuctured data to a file is using the following functions:
istream::read
ostream::write

Function prototypes are: read( const unsigned char* ptr, int n)
Description: The read function reads n characters starting form the location pointed to by ptr. In other words, read function reads n bytes from the address pointed to by ptr.
The write has similar prototype and similar description (but used to write data)

The above functions work best for reading/writing classes and structures from/to files.
EXAMPLE:I include all your data in a class, say "A"
class A<br />
{<br />
double x;<br />
double y;<br />
double speed;<br />
double z;<br />
int something1;<br />
char something2;<br />
};

Then i write all these this to a file using the code:
main()<br />
{<br />
A objectA;<br />
//take input from the user<br />
cin>>objectA.x;<br />
cin>>objectA.y;<br />
cin>>objectA.z; //and so on<br />
<br />
ofstream outfile ("c:\\test.txt", ios::binary);<br />
outfile.write ((char*)objectA, sizeof(objectA)); //this line writes the complete objectA(field by field) into the file test.txt. Remember, the file contents are now binary. So u may see many strange character when you open test.txt in a notepad.<br />
}

Once the object has been written into a file, you can read it anytime using using the code:
main()<br />
{<br />
A objectB;<br />
ifstream infile ("c:\\test.txt", ios::binary);<br />
infile.read ((char*)objectB, sizeof(objectB)); //after reading from the file objectB now contains the complete info of one class.<br />
//DISPLAY the read results<br />
cout<<objectA.x;<br />
cout<<objectA.y;<br />
cout<<objectA.z;//and so on<br />
}

YOU CAN CALL WRITE AGAIN AND AGAIN TO APPEND MORE TAHN ONE RECORDS INTO THE FILE
SIMILAR IS THE READ FUNCTION. WHEN U CALL READ AGAIN, IT WILL READ THE NEXT OBJECT WRITTEN INTO THE FILE.

All this may appear a bit fuzzy and complicated to you, BUT TRUST ME, this is the best way to handle structured data in files in C++.

Ask if u have any doubts;)


Remember... testing & debugging are always part of programming ...so exterminate those stinking bugs
GeneralRe: read a certain line from a text file Pin
dairiseky5-Apr-04 8:02
dairiseky5-Apr-04 8:02 
GeneralRe: read a certain line from a text file Pin
avenger_sb255-Apr-04 8:51
avenger_sb255-Apr-04 8:51 
GeneralRe: read a certain line from a text file Pin
David Crow5-Apr-04 4:50
David Crow5-Apr-04 4:50 
GeneralRe: read a certain line from a text file Pin
dairiseky5-Apr-04 5:22
dairiseky5-Apr-04 5:22 
GeneralRe: read a certain line from a text file Pin
David Crow5-Apr-04 7:33
David Crow5-Apr-04 7:33 
GeneralRe: read a certain line from a text file Pin
Anonymous5-Apr-04 7:44
Anonymous5-Apr-04 7:44 
GeneralRe: read a certain line from a text file Pin
dairiseky5-Apr-04 7:48
dairiseky5-Apr-04 7:48 
GeneralRe: read a certain line from a text file Pin
dairiseky6-Apr-04 2:46
dairiseky6-Apr-04 2:46 
GeneralRe: read a certain line from a text file Pin
David Crow6-Apr-04 3:24
David Crow6-Apr-04 3:24 
GeneralDialog related question Pin
Imtiaz Murtaza5-Apr-04 2:16
Imtiaz Murtaza5-Apr-04 2:16 
GeneralRe: Dialog related question Pin
avenger_sb255-Apr-04 2:48
avenger_sb255-Apr-04 2:48 
GeneralRe: Dialog related question Pin
David Crow5-Apr-04 4:52
David Crow5-Apr-04 4:52 
GeneralC++ Pin
cberam5-Apr-04 2:05
cberam5-Apr-04 2:05 
GeneralRe: C++ Pin
Steve S5-Apr-04 2:24
Steve S5-Apr-04 2:24 
GeneralRe: C++ Pin
RichardGrimmer19-Apr-04 5:36
RichardGrimmer19-Apr-04 5:36 
GeneralRe: C++ Pin
avenger_sb255-Apr-04 2:33
avenger_sb255-Apr-04 2:33 
GeneralDifference b/w HTTP PROXY ans SOCKS Pin
Anonymous5-Apr-04 1:15
Anonymous5-Apr-04 1:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.