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

Input Stream Confusion

Status
Not open for further replies.

Kalisto

Programmer
Feb 18, 2003
997
GB
OK, I haven't used streams in a while, and have just written this operator Overload below to Load a specific block of data from a file into my object.

[source]
std::ifstream& operator>>(std::ifstream &fs, Race &T)
{
char buffer[256];
int intbuffer[6];
std::string ip;
long liUID;
int size=0;
//Read in the Size of the 1st String
fs.read((char*)&size,sizeof(int));
//Read in the String
fs.read((char*)&buffer[0],size);
ip = buffer;
T.SetName(&ip);
//Description
fs.read((char*)&size,sizeof(int));
//Read in the String
fs.read((char*)&buffer[0],size);
ip = buffer;
T.SetDescription(&ip);
//Know The Above works due to Previous Debug code

//Next Line is causing an error
//this Line reads in 3 char, not 4 (which is size of long)
fs.get((char*)&liUID,sizeof(long));
T.SetUID(liUID);
printf("Number of Bytes Last Read in %i , should be %li\n",fs.gcount(),sizeof(long));
printf("%li\n",liUID);
return fs;
}
[/source]

OK, Each time the above function is called it reads in the size of the 2 strings (and the strings correctly).
Then it Moves on to read in the Long (Which incidentally is 999)
Instead of reading in 4 bytes, it reads in 3, which means that the next time the code is called, the file pointer is at the wrong point in the file, and so it reads in too many characters (size of next string has been left shifted a byte) and crashes my app.

I have been staring at this for hours now, and I cannot see the solution, although it is probably one of those annoiying little problems.

TIA,

K
 
If I understand your post correctly, you have a Race class that looks something like

Code:
class Race
{
 public:
 void SetName(string &);
 void SetDescription(string &);
 void SetUID(long);
 
 private:
 string name;
 string description;
 long UID;
};

and a data file that looks like..

string1 string2 999
str3 string4 123

Is this correct?

If so you it would be simple to read in each line, and use the components as inputs to a constructor.

Is this something that could help you?
 
Thanks, but I have fixed my problem. At present, all my data is contiguous in a binary file. As the description is to be displayed in a text box, and can include New lines, I cannot just read in a line at a time :-(

The problem was a combination of things.
I was reading in the long using get() not read(), and 999 in binary includes the hex value 03, which is a terminator code for get(), hence not reading enough data.

So now I have fixed that problem I have uncovered others, but most of those are (I suspect) in the output function, and in other classes that are also stored in the file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top