回复 10楼 succubus
非常感谢您的无私分享 :)我之前对awk,sed,perl及基本的unix command,比较熟悉。
我多数都是用综合以上的语言,来处理文档。
但最近发觉,在处理某些较大的文档(>1Gb),我会遇到该程序需要花费很长的时间处理文档及没有足够记忆(RAM)分析文档。
因此,才希望了解c语言,是如何分析相同的文档。
我会再详读您的每行code,希望能理解您所用的逻辑。
谢谢
./cpp_program_name [input_file1_name] [input_file2_name] [output_file_name]
程序代码:
#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
./cpp_program_name [input_file1_name] [input_file2_name] [output_file_name]

程序代码:
#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;
}