Below is the code I have for doing this but something is really wrong .
Filenames is the container I am using.
Filename is a string.

void SaveFileName(){
     ofstream out;
     out.open("..\\..\\Bush Breakout 3D\\Exe\\Level\\filenames.txt",ios::out | ios::app | ios::binary);
    // out.seekp(0,ios::end);
     int size = Filenames.size();     
     for(int i = 0;i < size;++i){      
     out.write((char*)(&Filenames),sizeof(Filename));
     Filenames.pop_back();
     out.seekp(20,ios::cur); 
     }    
     out.close(); 
}

Also how would I read the file back in either to a char array or a to a list and just one of the filenames at a time.
Here is the code I have for the read operation.

int i = 20;
char IFilename[i]; 
in.open("Level\\filenames.txt",ios::binary);                       
     in.seekg(0,ios::beg);        
     in.read((char*)IFilename,i*sizeof(char));
     (*it).SetFileName(IFilename);
     in.close(); 
for(i = 0;i<20;i++){
     IFilename[i] = 0;
     }

Thanks to anyone who will help me with this I have been working on this for quite a while googled my brains several times but can't seem to find anything that will help with it. Much thanks, Jody Bush

Recommended Answers

All 3 Replies

>>Filenames is the container I am using.
What container? Post how Filenames is declared. Is it std::string, vector<string>, or something else? We need a lot more details about it in order for anyone to tell you how to serialize it.

Assuming Filenames is a vector<string>, then do it something like this:

for(size_t i = 0; i < Filenames.size(); i++)
{
     string s = Filenames[i];
     // make s constant width of 20 characters
     for(size_t j = s.length(); j < 20; j++)
          s += ' '; // add a space to end of string.
     out.write( s.c_str(), s.length() );
}

Now to read it back. Note: function you will have to code rtrim() function yourself. It removes trailing spaces.

char line[21];
while( in.read(line, 20) )
{
   line[20] = '\0'; // null terminate the string
   rtrim(line); // remove trailing spaces
   Filename.push_back(line);
}

Ok,I really did not explain things very well. The declarartion for the container is list<string>

The write function is in an entirely different program than the read function. I was using text mode but when I changed the program to set the path in the code,so that all a person has to do is type the filename in the textbox instead of the path and the filename,the program started leaving off one of the backslashes in the path from Level\\ to Level\ . The read function is in a breakout type game and the filenames are read in one at a time as needed to set the path/filename in the Block objects so the class will know which file to load in for that level. If you need any more of the code just tell me what you need and I will post it. Much Thanks again, Jody Bush

If you use std::string for the final full path/filename, and a character array to get the filename from the text box, then just do this:

std::string path;
char filename[255];
// get the text from the text box and 
// save it in the character array is not
// shown here.

path = "\\Level\\";
path += filename;
// now open  the file for reading
ifstream in(path.c_str());
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.