INT32 const CFile::Load(const string *Filename, string *Buffer)
{
FullFilePath = GetPhysicalPath(Filename);
ifstream inFile( FullFilePath.c_str(), ios::in / ios::out );
Buffer += inFile.rdbuf();
inFile.close();
//Add error checking here
return 0;
}
INT32 const CFile::Save(const string *Filename, const string *Buffer)
{
FullFilePath = GetPhysicalPath(Filename);
ifstream outFile( FullFilePath.c_str(), ios::in / ios::out );
outFile.setbuf(Buffer,sizeof(*Buffer));
outFile.close();
//Add error checking here
return 0;
}
Many thanks
Chris Brodie
iostream help needed
Thanks for reading...
I cant get the following source code to work. I''m just starting out with C++ IO and STL and -hope- that this is along the lines of how it''s meant to work.
Chris Brodie
Hrrm yes,... I wasn''t very specific was I, must have been in one of those coding dazes I get...
I''m trying to write a generic file handler. The idea is to memory map the file.
1) First of all I''m not sure if I should be using a string as the return buffer for the file. In the sence that I don''t want any special string formatting to interfere with the data. A vector perhaps?
2) Second I gave the syntax a shot as it seems it should be done but the following is wrong:
Buffer = inFile.rdbuf();
Buffer << inFile.rdbuf();
Buffer += inFile.rdbuf();
I''ve read the rather unhelpful doco in MSDN on rdbuf() but am looking to see rdbuf in action.(I''v seen it used to output to cout...in the manner I''m describing...
3) With setbuf it looks like I missed the mark again...
outFile.setbuf(Buffer,*Buffer.Size();
This was basically a lame attempt at dumping the contents of a string to disk.
Any help would be appreciated.
gimp
I''m trying to write a generic file handler. The idea is to memory map the file.
1) First of all I''m not sure if I should be using a string as the return buffer for the file. In the sence that I don''t want any special string formatting to interfere with the data. A vector perhaps?
2) Second I gave the syntax a shot as it seems it should be done but the following is wrong:
Buffer = inFile.rdbuf();
Buffer << inFile.rdbuf();
Buffer += inFile.rdbuf();
I''ve read the rather unhelpful doco in MSDN on rdbuf() but am looking to see rdbuf in action.(I''v seen it used to output to cout...in the manner I''m describing...
3) With setbuf it looks like I missed the mark again...
outFile.setbuf(Buffer,*Buffer.Size();
This was basically a lame attempt at dumping the contents of a string to disk.
Any help would be appreciated.
gimp
Chris Brodie
quote: Original post by gimp
Buffer = inFile.rdbuf();
Buffer << inFile.rdbuf();
Buffer += inFile.rdbuf();
I''ve read the rather unhelpful doco in MSDN on rdbuf() but am looking to see rdbuf in action.(I''v seen it used to output to cout...in the manner I''m describing...
You are slightly misinterpreting the way these things work. When you want to do something like that, you need the relevant operator to apply to that combination of objects, or for there to be some sort of convertion already in place. You can''t just throw an .rdbuf() at something and hope it knows what you want to do with it. You can do this:
aStringStream << aStream.rdbuf();
because std::stringstream, on the left hand side, has its ''<<'' operator defined for rdbuf(), which is of type streambuf.
Now, you can''t do this:
aString << aStream.rdbuf();
as std::string has no ''<<'' operator defined that takes a streambuf.
Just like you can''t really do:
int a = 1;
a += "sdflk";
As an ''int'' doesn''t have ''+='' defined for ''const char*''.
Off the top of my head, you could use an intermediary, like so:
// Load in the stream to a memory-based stream
tempStringStream << aStream.rdbuf();
// Now add that stream''s character representation to my buffer
buffer += tempStringStream.str();
There may even be a better way, but that would work. Whether it kills your performance dead is another matter, so test it and see.
alright, i think what you are doing wrong (i don''t know what kind of error you are getting is this:
you can''t do "ios::in / ios::out", you have to do "ios::binary", i think.
when i use binary to save/load data, this is what i do:
{source}
#include
// main, and other code...
bool SaveStruct (SOMESTRUCT ss)
{
ofstream outfile (g_lpszFileName, ios::binary);
outfile.write ((char*) & ss, sizeof (ss));
return (true);
}
bool LoadStruct (SOMESTRUCT ss)
{
ifstream infile (g_lpszFileName, ios::binary);
infile.read ((char*) & ss, sizeof (ss));
return (true);
}
{/source}
i may be wrong, but you can probably use just an fstream instead of ofstream or ifstream if fstream is an object. it should support the write method. otherwise, you''ll need to create one object for output and one for input. the format of read/write is pretty simple, but there are some things you have to know. you pass it the address of the data you want to save/load, but you must cast it to a char* because the class needs to save/load the number of bytes, instead of the number of structs or ints or floats or whatever. you tell it how many bytes by using sizeof. if you are passing an array of something, do this:
size = sizeof (arraytype) * num_elements;
hope that helped.
farmersckn
p.s. The other post''s may have had the answer you were looking for, but i wrote this a while before, and i got the boot from the ''net, so i couldn''t post it until now...
Sometimes even chickens need to eat... Don't bang your head against a wall just to enjoy the good feeling when you stop.
you can''t do "ios::in / ios::out", you have to do "ios::binary", i think.
when i use binary to save/load data, this is what i do:
{source}
#include
// main, and other code...
bool SaveStruct (SOMESTRUCT ss)
{
ofstream outfile (g_lpszFileName, ios::binary);
outfile.write ((char*) & ss, sizeof (ss));
return (true);
}
bool LoadStruct (SOMESTRUCT ss)
{
ifstream infile (g_lpszFileName, ios::binary);
infile.read ((char*) & ss, sizeof (ss));
return (true);
}
{/source}
i may be wrong, but you can probably use just an fstream instead of ofstream or ifstream if fstream is an object. it should support the write method. otherwise, you''ll need to create one object for output and one for input. the format of read/write is pretty simple, but there are some things you have to know. you pass it the address of the data you want to save/load, but you must cast it to a char* because the class needs to save/load the number of bytes, instead of the number of structs or ints or floats or whatever. you tell it how many bytes by using sizeof. if you are passing an array of something, do this:
size = sizeof (arraytype) * num_elements;
hope that helped.
farmersckn
p.s. The other post''s may have had the answer you were looking for, but i wrote this a while before, and i got the boot from the ''net, so i couldn''t post it until now...
Sometimes even chickens need to eat... Don't bang your head against a wall just to enjoy the good feeling when you stop.
Yesterday is the past, tomorrow is the future. Today is a gift, that is why we call it the present.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement