I am trying to do something similar to this
[URL="http://cboard.cprogramming.com/showthread.php?t=82638&highlight=ifstream"]
where he is doing the input/output on a struct, while I am working on my packetClass
Code:
struct MusicRecord
{
string Artist;
string Album;
string Title;
string Path;
};
void FileSystem::ALP_InsertRecord(MusicRecord &Rec, int Index)
{
ofstream Stream( "playlist.dat", ios::out|ios::binary|ios::app);
Stream.seekp( ( Index ) * sizeof ( MusicRecord ) );
Stream.write( (char*) &Rec, sizeof( MusicRecord ) );
Stream.close();
}
void FileSystem::ALP_GetRecord(MusicRecord &Rec, int Index)
{
ifstream Stream( "playlist.dat", ios::binary|ios::in );
Stream.seekg( ( Index ) * sizeof( MusicRecord ) );
Stream.read( (char *) &Rec, sizeof( MusicRecord ) );
Stream.close();
}
Now, if I would like to not use at all any pass by references as a prevention for all my programs.
Now, if I do this which I don't like for the above reason:
Code:
if(strcmp( (char *)myString, "open")==0)
{
ifstream myFile("filename.txt", ios_base::binary );
myFile.read( (char *)&displayPacket.params , sizeof(displayPacket.params) );
/*Don't want to to use pointers at all, thus avoiding risk of unitialized pointers*/
myFile.read( (char *)&displayPacket.payload, sizeof(displayPacket.payload) );
/*Don't want to to use pointers at all, thus avoiding risk of unitialized pointers*/
myFile.close();
where my displayPacket is my own packet class and the params is a string class, the compiler complains that illegal use of sizeof() and illegal operation on bound member function ecpression.
Isn't there a way to do myFile >> packet? Could I derive a class improvedifstream from the ifstream and make a function that will handle a pass by value of a string and do a file.read( buff, sizeof(buf) where I guarantee that buf is inside the class of improvedifstream? Somebody must have done this before and I'd be surprised that this sort of operation in not inside standard classes.
Frank