2014年01月23日 星期四 15:13
此段代码是我用于分析一段文本并从中提取ed2k协议URL的实例,其中应用了文件读取、字符串分析等技术,并引用了Poco库的URI模块来进行URL Decode操作。
编写并调试完成这段代码后,我认为使用C++的开发效率也很不错,并不比PHP复杂很多。C++的标准库和Poco库的API都非常容易使用,C++11的新特性使得C++更是如虎添翼,大大提升了开发效率。
#include <iostream>
#include <string>
#include <fstream>
#include <array>
#include <Poco/URI.h>
using namespace Poco;
using namespace std;
int main(int argc,char **argv) {
if(argc != 2) {
cerr << "usage: " << argv[0] << " input_file" << endl;
return -1;
}
string input_file=argv[1];
ifstream ifs;
ifs.open(input_file);
if(!ifs.is_open()) {
cerr << "open file: " << input_file << " error." << endl;
return -2;
}
array<char,4096> line;
while(!ifs.eof()) {
line.fill(0);
ifs.getline(line.data(),4095);
//cout << line.data() << endl;
string sline(line.data());
auto p1=sline.find("ed2k://");
if(p1 == string::npos) {
continue;
}
string link=sline.substr(p1);
//cout << link << endl;
auto p2=link.find("|/'");
if(p2 == string::npos) {
continue;
}
link=link.substr(0,p2+2);
//cout << link << endl;
string declink;
URI::decode(link,declink);
cout << declink << endl;
}
ifs.close();
return 0;
}
Zeuux © 2025
京ICP备05028076号