Click here to Skip to main content
15,921,028 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: avenger_sb25//read, write data files Pin
Steve S6-Apr-04 1:21
Steve S6-Apr-04 1:21 
GeneralRe: avenger_sb25//read, write data files Pin
avenger_sb256-Apr-04 11:15
avenger_sb256-Apr-04 11:15 
GeneralRe: avenger_sb25//read, write data files Pin
David Crow6-Apr-04 2:11
David Crow6-Apr-04 2:11 
GeneralRe: why binary? Pin
dairiseky6-Apr-04 2:32
dairiseky6-Apr-04 2:32 
GeneralRe: why binary? Pin
David Crow6-Apr-04 3:27
David Crow6-Apr-04 3:27 
GeneralRe: avenger_sb25//read, write data files Pin
avenger_sb256-Apr-04 11:12
avenger_sb256-Apr-04 11:12 
GeneralRe: avenger_sb25//read, write data files Pin
dairiseky6-Apr-04 23:48
dairiseky6-Apr-04 23:48 
GeneralRe: avenger_sb25//read, write data files Pin
Anonymous7-Apr-04 3:27
Anonymous7-Apr-04 3:27 
D'Oh! | :doh: Ooops! a small mistake on my part again. I was actually mixing the read function of C with read member of C++.

In C++, the prototype for write is:
istream& read(signed char*, int);
So whatever we write, we have to cast its pointer to (char*)

So if we have to write some object AAA to the file, we will use the following syntax:
ofstream outfile("test.txt", ios::binary);
outfile.write((char *) &AAA, sizeof(AAA));


Now let me tell you a few things:
Dont try to retrieve data from a file using read where you have manually entered data. What i mean is, if you have created a file from notepad and have manually typed in the data, you cannot retrieve those data using istream::read()

So the point is:- retrieve only those data from the file using istream::read that have been written to it using ostream::write.

The following code is tested on my system and will help you in understanding all this better. After running the program, open the "123.txt" file in c:\ drive to view how data has been written in binary.
#include <iostream.h>
#include <fstream.h>
int main()
{
class A
{
public:
double x;
double y;
double z;
};

A objectA,objectB;

objectA.x=123.345;
objectA.y=345.003;
objectA.z=456.2;
cout<<"Following data will be written to 123.txt"<<endl;
cout<<"A.x="<<objectA.x<<endl;
cout<<"A.y="<<objectA.y<<endl;
cout<<"A.z="<<objectA.z<<endl;

ofstream outfile("c:\\123.txt",ios::binary);//creare ofstream object
outfile.write((char*)&objectA,sizeof(objectA));//write object
outfile.close(); //close the stream-NEVER FORGET TO DO THIS
cout<<"object A has been written to the file"<<endl<<endl;
cout<<"reading data from 123.txt"<<endl;
ifstream infile("c:\\123.txt",ios::binary); //create stream
infile.read((char*)&objectB,sizeof(objectB)); //read object from stream
outfile.close(); //close stream
cout<<"B.x="<<objectB.x<<endl;
cout<<"B.y="<<objectB.y<<endl;
cout<<"B.z="<<objectB.z<<endl;

return 0;
}


Avoid that seek stuff. Dead | X|
Suppose you have performed 5 writes to a file(i.e. you have written 5 objects one after the other) and you want to read the 3rd object later, then use the following technique.
ifstream infile("test.txt", ios::binary);<br />
A objectA;<br />
for(i=0;i<3;i++)<br />
{<br />
infile.read((char *) &objectA, sizeof(objectA));<br />
}<br />
infile.close();
After the for loop, objectA will contain the data from the 3rd record;
Ask if u have any problems.
GeneralRe: avenger_sb25//read, write data files Pin
avenger_sb257-Apr-04 3:35
avenger_sb257-Apr-04 3:35 
GeneralRe: avenger_sb25//read, write data files Pin
dairiseky7-Apr-04 5:32
dairiseky7-Apr-04 5:32 
GeneralOracle Stored Procedure Problem in .NET Pin
Anonymous5-Apr-04 23:20
Anonymous5-Apr-04 23:20 
QuestionGDI+ question: Bitmap:FromFile, how to detect non-existent image file? Pin
Indrawati5-Apr-04 22:39
Indrawati5-Apr-04 22:39 
AnswerRe: GDI+ question: Bitmap:FromFile, how to detect non-existent image file? Pin
Mike Dimmick5-Apr-04 23:43
Mike Dimmick5-Apr-04 23:43 
AnswerRe: GDI+ question: Bitmap:FromFile, how to detect non-existent image file? Pin
JonCage6-Apr-04 1:37
JonCage6-Apr-04 1:37 
GeneralWindows idle time Pin
Florin Ochiana5-Apr-04 22:28
Florin Ochiana5-Apr-04 22:28 
GeneralRe: Windows idle time Pin
Rage5-Apr-04 22:35
professionalRage5-Apr-04 22:35 
GeneralRe: Windows idle time Pin
Florin Ochiana5-Apr-04 22:43
Florin Ochiana5-Apr-04 22:43 
GeneralRe: Windows idle time Pin
Rage5-Apr-04 22:48
professionalRage5-Apr-04 22:48 
GeneralRe: Windows idle time Pin
Florin Ochiana5-Apr-04 22:52
Florin Ochiana5-Apr-04 22:52 
GeneralRe: Windows idle time Pin
avenger_sb255-Apr-04 23:25
avenger_sb255-Apr-04 23:25 
GeneralRe: Windows idle time Pin
Rage6-Apr-04 0:46
professionalRage6-Apr-04 0:46 
GeneralRe: Windows idle time Pin
Antony M Kancidrowski6-Apr-04 1:37
Antony M Kancidrowski6-Apr-04 1:37 
GeneralRe: Windows idle time Pin
David Crow6-Apr-04 2:15
David Crow6-Apr-04 2:15 
GeneralRe: Windows idle time - GetLastInputInfo error Pin
Anonymous7-Apr-04 3:18
Anonymous7-Apr-04 3:18 
GeneralRe: Windows idle time - GetLastInputInfo error Pin
avenger_sb257-Apr-04 3:40
avenger_sb257-Apr-04 3:40 

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.