跟随贺老师:2011年C++第16周-任务4-文档的自动处理

来源:互联网 发布:国资委待遇怎么样知乎 编辑:程序博客网 时间:2024/06/05 17:57

贺利坚老师2011年16周任务4文档的自动处理(去除;注释行)


下面是我自己查了一些资料写出来的具有相同的功能的程序,但是我觉得这个程序有点“投机取巧”。


里面涉及的知识有:迭代器,还有string的一些操作。(这些老师暂时没有教)


#include <iostream>  #include <fstream>#include <string>  using namespace std;  int main()   {      ifstream sourceFile;      ofstream targetFile;          sourceFile.open("WolfSheep.txt",ios::in); targetFile.open("ws.txt", ios::out);        string line;    int pos = -1;    string::iterator it;     while ( ! sourceFile.eof() )   {   getline (sourceFile,line);   cout<<line<<endl;   pos = line.find(";");    if(pos<0)    targetFile<<line<<'\n';        if(pos>2){        line.erase(line.begin()+pos, line.end());    targetFile<<line<<'\n';    }    }    sourceFile.close(); targetFile.close();    }    


0 0
原创粉丝点击