回复 10楼 succubus
你好,我尝试篇写您的c程序,让其能自由读取任何输入的文档。
理想操作格式:
./cpp_program_name [input_file1_name] [input_file2_name] [output_file_name]
以下是我利用您的c程序,篇改的内容:
程序代码:
#include <stdio.h>
#include <string>
#include <map>
#include <fstream>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
map<string, string> outMap;
vector<string> keys;
char buf[4096];
FILE *fin1, *fin2, *fout;
if(argc != 4)
{
fprintf(stderr, "Usage: ./cpp_program_name [input_file1_name] [input_file2_name] [output_file_name]\n", argv[0]);
return(1);
}
fin1=fopen(argv[1], "r");
if(fin1 == NULL)
{
fprintf(stderr, "Couldn't open %s\n", argv[1]);
return(1);
}
fin2=fopen(argv[2], "r");
if(fin2 == NULL)
{
fprintf(stderr, "Couldn't open %s\n", argv[2]);
return(1);
}
fout=fopen(argv[3], "w");
if(fout == NULL)
{
fprintf(stderr, "Couldn't open %s\n", argv[3]);
return(1);
}
string key;
string word;
string line;
int delimiterPos;
map<string, string>::iterator searchRes;
while(fgets(buf, 4096, fin1))
{
while (fin1 >> key)
{
keys.push_back(key);
outMap.insert(make_pair(key, "|"));
}
}
while(fgets(buf, 4096, fin2))
{
while (fin2 >> line)
{
delimiterPos = line.find('|');
key = line.substr(0, delimiterPos);
word = line.substr(delimiterPos);
if ((searchRes = outMap.find(key)) != outMap.end())
searchRes->second = word;
}
}
for (vector<string>::iterator iter = keys.begin(); iter != keys.end(); ++iter)
{
searchRes = outMap.find(*iter);
fout << searchRes->first << searchRes->second << endl;
}
fclose(fin1);
fclose(fin2);
fclose(fout);
return 0;
}
当我试着解码以上程序时:
程序代码:
[home@cpp]g++ cpp_program.cpp
cpp_program.cpp: In function aint main(int, char**)a:
cpp_program.cpp:50: error: no match for aoperator>>a in afin1 >> keya
cpp_program.cpp:58: error: no match for aoperator>>a in afin2 >> linea
cpp_program.cpp:70: error: no match for aoperator<<a in afout << searchRes.std::_Rb_tree_iterator<_Tp>::operator-> [with _Tp = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]()->std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::firsta
您是否知道,该如何篇改,才能让您的c程序,依照理想操作形式的格式,运行及分析文档?
理想操作格式:
./cpp_program_name [input_file1_name] [input_file2_name] [output_file_name]