c++ primer中关于单词转换的程序

来源:互联网 发布:图像压缩算法代码 编辑:程序博客网 时间:2024/05/15 13:26

这是在第四版c++ primer书中第10章10.3.9的程序。觉得很好,记下来。

#include <iostream>#include <map>#include <fstream>#include <sstream>#include <string>#include <cstdlib>using namespace std;/*in1.txt中的内容'emthemcuzbecausegratzgratefuliInahnopossupposedsezsaidtanxthankswuzwasin2.txt中的内容nah i sez tanx cuz i wuz pos tonot cuz i wuz gratz*/ifstream& open_file(ifstream &in, const string &file) {    in.close();//关闭以防之前已经打开    in.clear();//清除任何存在的错误    in.open(file.c_str());    return in;//此时in要么已经与文件绑定在了一起,要么处于错误的状态}int main(int argc, char **argv){    ifstream infile;    ofstream outfile;    map<string, string> wordChange;    string input1 = "/home/xxx/Desktop/program/c++/单词转换/in1.txt";    string input2 = "/home/xxx/Desktop/program/c++/单词转换/in2.txt";    //infile.open(input1.c_str());    if(!open_file(infile, input1)) cout << "no file" << endl;    string key, value;    while(infile >> key >> value) wordChange[key] = value;    string line, word;    //ifstream input(input2.c_str());    ifstream input;    if(!open_file(input, input2)) cout << "no file" << endl;    map<string, string>::iterator iter;    while(getline(input, line)) {        istringstream stream(line);        bool flag = true;        while(stream >> word) {            iter = wordChange.find(word);//此处不能用下标操作,因为下标操作会把原先没有的键值对加进去            if(flag) {                flag = false;            } else {                cout << " ";            }            if(iter != wordChange.end()) cout << iter->second;            else cout << word;        }        cout << endl;    }    infile.close();    return 0;}

总结:文件操作用的还不是很好。linux下文件路径的问题,要注意用各种命令检查路径和权限。如ls, pwd, stat, cat 等。