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。

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