• 文件处理


    在做软件度量作业的过程中,遇到了文件中上千条记录的处理。程序如下:

    C++的读文件

    字符串取子串等

    #include <iostream>
    #include <map>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main()
    {
        fstream fin;
        string filename = "E:\002Validate Binary Search Tree\Debug\bugs-2013-11-30.csv";
        fin.open(filename);
        if(fin.bad())
        {
            cout<<"文件打开失败"<<endl;
            return 0;
        }
        string line;
        int first,last;
        string changeddata;
        map<string,int> count;
         
        while(!fin.eof())
        {
            std::getline(fin,line);
            int comma = line.find_last_of(',');
            first = line.find_first_of('//',comma+1);
            last = line.find_first_of('//',first+1);
            if(first!=-1 && last!= -1)
            {
                if(first<5||last-first<1)
                    continue;
                changeddata = line.substr(first-4,last-first+4);
    
                if(count.find(changeddata)==count.end())
                    count.insert(pair<string,int>(changeddata,1));
                else
                    count[changeddata] +=1;
    
            }
        }
        for(map<string,int>::iterator iter = count.begin();iter!=count.end();iter++)
        {
            //cout<< iter->first <<"  "<< iter->second<<endl;
            //cout<< iter->first<<endl;
            cout<<iter->second<<endl;
        }
        return 0;
    }
  • 相关阅读:
    mysql安装脚本
    vim常用命令
    CentOS 6.5使用国内网易163的源
    053(七十五)
    053(七十四)
    053(七十三)
    053(七十二)
    053(七十一)
    053(七十)
    053(六十九)
  • 原文地址:https://www.cnblogs.com/qingcheng/p/3452507.html
Copyright © 2020-2023  润新知