C++11 从文本文件C+中提取IP+;
我是C++的新程序员,我想创建一个从文本文件中提取IP的代码C++11 从文本文件C+中提取IP+;,c++11,C++11,我是C++的新程序员,我想创建一个从文本文件中提取IP的代码 我试图将txt文件转换为向量(字符串),以便进行简单的筛选,但我无法获得像XXX.XXX.XXX.XXX这样的所有格式鉴于ip可以嵌入某些文本中,我们将解析字符串并获取它 #include<iostream> #include<fstream> using namespace std; int main() { ifstream myReadFile; myReadFile.open("tex
我试图将txt文件转换为向量(字符串),以便进行简单的筛选,但我无法获得像XXX.XXX.XXX.XXX这样的所有格式鉴于ip可以嵌入某些文本中,我们将解析字符串并获取它
#include<iostream>
#include<fstream>
using namespace std;
int main() {
ifstream myReadFile;
myReadFile.open("text.txt");
char output[100];
if (myReadFile.is_open()) {
while (!myReadFile.eof()) {
myReadFile >> output;
cout<<output;
}
}
myReadFile.close();
return 0;
}
#include <iostream>
#include <string>
#include <iterator>
#include <vector>
#include <sstream>
using namespace std;
string ip(string str)
{
//middle portion
auto firstDot = str.find_first_of('.');
auto lastDot = str.find_last_of('.');
int dotCount = 0;
for(auto i = firstDot; i <= lastDot; i++)
{
if(!isdigit(str.at(i)) && str.at(i) != '.') //e.g 127.ss.0y.1
return string("");
if(str.at(i) == '.')
dotCount++;
}
if(dotCount != 3) //eg. 127.0.1 (not sure if this is wrong though)
return string("");
string result = str.substr(firstDot,lastDot-firstDot + 1);
//left portion
size_t x = 0; //number consegative digits from the first dot
for(auto i = firstDot-1; i >= 0; i--)
{
if(!isdigit(str.at(i)))
break;
else if(x == 3) //take first 3
break;
x++;
}
if(x == 0)
return string("");
result.insert(0,str.substr(firstDot-x,x));
//right portion
size_t y = 0;
for(auto i = lastDot + 1; i < str.length(); i++)
{
if(isdigit(str.at(i)))
{
if(y == 3)
break;
result.push_back(str.at(i));
y++;
}
else
break;
}
if(y == 0)
result.push_back('0');
return result;
}
int main()
{
string test = "1111127.0.0.11111 xx23.45.12.# xxxx.34.0.13 124.sd.2.1 sd.45.56.1";
string x,y;
vector<string> ips;
stringstream stream;
stream<<test;
while(stream>>x)
if(!(y = ip(x)).empty())
ips.push_back(y);
for(auto z : ips)
cout<<z<<"\t";
cout<<endl;
system("pause");
return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
字符串ip(字符串str)
{
//中段
auto firstDot=str.find_first_of('.');
auto lastDot=str.find_last_of('.');
int dotCount=0;
对于(自动i=firstDot;i=0;i--)
{
如果(!isdigit(str.at(i)))
打破
如果(x==3)//取前3个
打破
x++;
}
如果(x==0)
返回字符串(“”);
结果:插入(0,str.substr(firstDot-x,x));
//右部分
尺寸y=0;
对于(自动i=lastDot+1;i cout我是C++11新手。我为您做了以下简单示例。它基于正则表达式库。希望它对您有用
#include <iostream>
#include <string>
#include <regex>
int main ()
{
std::string s ("There's no place like 127.0.0.1\n");
std::smatch m;
std::regex e ("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
while (std::regex_search (s,m,e)) {
for (auto x:m) std::cout << x << " ";
std::cout << std::endl;
s = m.suffix().str();
}
return 0;
}
#包括
#包括
#包括
int main()
{
std::string s(“没有像127.0.0.1\n那样的地方”);
std::smatch m;
std::regex e(“\\d{1,3}\\\\\\\\d{1,3}\\\\\\\\\\d{1,3}\\\\\\\\\\\d{1,3}”);
while(std::regex_搜索(s,m,e)){
对于(auto x:m)std::cout第12行:语法错误:缺少“,”before“:”我不理解“regex”的意思它应该做什么?谢谢你的回答(y)伟大的算法!但是所有的自动类型都是错误[error]“firstDot”没有命名类型……。@MED-ELOMARI,如果在auto
中出现错误,请将auto
更改为size\t
。
#include <iostream>
#include <string>
#include <regex>
int main ()
{
std::string s ("There's no place like 127.0.0.1\n");
std::smatch m;
std::regex e ("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
while (std::regex_search (s,m,e)) {
for (auto x:m) std::cout << x << " ";
std::cout << std::endl;
s = m.suffix().str();
}
return 0;
}