當前位置:成語大全網 - 書法字典 - c++如何在指定的TXT文件中實現數據存儲?

c++如何在指定的TXT文件中實現數據存儲?

1,類似於以下代碼:

2.需要

3.主要是對ofstream類對象的操作。

虛空?CNews::save(字符串?文件名)

{

ofstream?outfile(filename . c _ str();

弦樂?ar;

如果(!輸出文件)

{

cerr?& lt& lt?“錯誤:無法?去哪?打開嗎?輸入?文件:“?& lt& lt?outfile?& lt& lt?endl

返回;

}

//格式_保存

for(mapIter?it =新聞組。begin();它!=新聞組。end();it++)

{

outfile?& lt& lt?它-& gt;第壹

& lt& lt?恩德爾

& lt& lt?它-& gt;第二。第壹

& lt& lt?endl

for(vector & lt;字符串& gt*叠代器?vIt = it-& gt;second . second . begin();維特。= it-& gt;second . second . end();vIt++)

outfile?& lt& lt?* vIt

outfile?& lt& lt?endl

}

outfile . close();

} 4當然可以簡化成下面的代碼,上面是我之前寫的類的文件保存功能。

# include & ltfstream & gt

使用?名稱空間?std

int?主()

{

ofstream?outfile(“filename . txt“);

如果(!輸出文件)

{

cerr?& lt& lt?“錯誤:無法?去哪?打開嗎?輸入?文件:“?& lt& lt?endl

返回;

}

outfile?& lt& lt?“C++?程序?。文件?版本。”?& lt& lt?endl

outfile . close();

}