I’m trying to write a program that will save the contents of a linked list (each individual list is called a locality and includes a mix of data types). The code compiles but terminates unexpectedly; it refers me to the lines in the ifstream library (even though I only want to be using writing)
*_Str = _Elem(); // add terminating null character
Does anyone have an idea whats gone wrong?
//saves a single locality onto a file
void fSave_locality(Clinked *current, fstream *fout)
{
fout->write(current->site_name,100);
fout->write((char*) ¤t->cood_x,sizeof(double));
fout->write((char*) ¤t->cood_y,sizeof(double));
fout->write((char *) ¤t->dip,sizeof(double));
fout->write((char *) ¤t->strike,sizeof(double));
if (current->next!=NULL) fSave_locality(current->next,fout);
}
void fSave_list(char* fname)
{
fstream *fout;
do
{
cout<<"Would you like to save as a (b)inary or (t)ext file? ";
test = getch();
cout<<"Enter file name (make sure its unique!): ";
cin.getline(fname,100);
if(toupper(test)=='T') fout->open(fname, fstream::out);
if(toupper(test)=='B') fout->open(fname, fstream::out| fstream::binary);
}
while(toupper(test)!='T' || toupper(test)!='B');
if(fout->fail())
{
cout<<"unable to open file.\n";
exit(0);
} //it gets to here without any problems.
current = start;
while(current->next!=NULL)
{
fSave_locality(current, fout);
current=current->next; //repeat for the next object in the list
}
fout->close();
}
I don’t understand why you are iterating recursively and sequentially in the same time ?
Just choose one or the other, I changed the code, you also need to set a correct limit to your while loop and make sure you do not use a null object and try to access elements on a null object.
and change the below part :