我正在嘗試編寫一個程序來保存鏈接列表的內容(每個單獨的列表稱爲一個地方幷包含數據類型的混合)。代碼編譯但意外終止;它指向我ifstream庫中的行(儘管我只想使用寫入)爲fstream添加NULL終止符
*_Str = _Elem(); // add terminating null character
有沒有人有一個想法什麼是錯誤?
//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();
}
你使用調試器來確定哪些錯誤發生的具體行? (如果你使用'gdb',你應該使用'b(ack)t(race)') – Zeta
fname設置爲什麼?我猜這可能是罪魁禍首。在另一個說明中,你確定最後一個循環不應該是「while(current!= NULL)」嗎? – DrC
如果回答這個問題,我正在使用Visual Studios 2010? (抱歉,如果沒有;計算不是我的強項) –