fstream逐行讀取內容並寫入

#include <string>
#include <vector>
#include <map>

int _tmain(int argc, _TCHAR* argv[])
{
    std::fstream File;
    File.open("E:\\茂名石化錯誤碼.txt");
    std::vector<std::string> vecErrorCode;
    std::map<std::string, int> mapErrStatistics;
    while (!File.eof())
    {
        std::string strErrCode = "";
        char szErrCode[256];
        // 以下兩種寫法都可
        //inFile.getline(szErrCode, 256, '\n');
        std::getline(File, strErrCode);
        vecErrorCode.push_back(strErrCode);
    }
    File.close();
    // 保存統計信息
    File.open("E:\\錯誤統計表.csv", std::ios::binary | std::ios::out);
    for (auto itrMap:mapErrStatistics)
    {
        File << itrMap.first << ',' << itrMap.second << std::endl;
    }
    File.close();
	return 0;
}

      ios::in              input         以輸入方式打開文件。

      ios::out           output        以輸出方式打開文件。

      ios::app          append     以追加方式打開文件。

      ios::ate           at end        打開文件時,定位到文件尾。

      ios::binary      binary        以二進制方式打開文件,默認是文本方式。

      ios::trunc        truncate    打開文件時,把文件長度截斷爲0。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章