#include <cstdlib> #include <iostream> #include <map> #include <fstream> #include <sstream> using namespace std; int main(int argc, char *argv[]) { string dicFileName("dicFile.txt"); string input("input.txt"); string output("output.txt"); string eachLine; map<string, string> dicMap; ifstream dicFile(dicFileName.c_str()); if(!dicFile) { cerr << "Error: Cannot open file: " << dicFile << endl; system("PAUSE"); return -1; } while(!dicFile.eof()) { getline(dicFile, eachLine); istringstream is(eachLine); string key; string value; is >> key; is >> value; dicMap.insert(make_pair(key, value)); } dicFile.close(); ifstream inputFile(input.c_str()); if(!inputFile) { cerr << "Error: Cannot open file: " << inputFile << endl; system("PAUSE"); return -1; } ofstream outputFile(output.c_str()); string eachKey; while(!inputFile.eof()) { inputFile >> eachKey; map<string, string>::iterator it = dicMap.find(eachKey); if(it != dicMap.end()) outputFile << it->second << " "; else outputFile << eachKey << " "; } inputFile.close(); outputFile.close(); system("PAUSE"); return EXIT_SUCCESS; } C++ primer P317