【发布时间】:2016-09-13 05:30:47
【问题描述】:
我正在使用此代码来查找 C++ 中括号之间的数字。
我希望从包含这些类型数据的大文件中提取该数字:
SUCCESSFUL CANDIDATES ARE INDICATED WITHIN PARANTHESIS AGAINST THEIR ROLL NUMBER AND THE EXTRA MARKS GIVEN [MAXIMUM FIVE MARKS] TO RAISE THEIR GRADES IN HARDSHIP CASES ARE INDICATED WITH PLUS[+] SIGN AND
GRACE MARKS CASES ARE INDICATED WITH CARET[+] SIGN
600023[545] 600024[554] 600031[605] 600052[560] ***********
Grade : D
Govt. Degree Boys College, Surjani Town
600060[877] *********** *********** *********** ***********
///// 在第 2 次迭代中,当找到 [554] 时。 m.size() 不会重置为零,这会导致错误。我该如何解决?我如何从整个文件中全局搜索括号中的数字 [###]
#include <iostream>
#include <fstream>
#include <string>
#include<conio.h>
#include<regex>
using namespace std;
int main () {
ifstream myfile;
myfile.open("test.txt") ;
if(!myfile)
{
cout<<"The file you entered is not present"<<endl;
}
string str;
if(myfile.is_open())
cout<<"file is open";
else
cout<<"file is close";
string output;
regex e("\[(\d+)\]");
smatch m;
while (!myfile.eof()) {
myfile >> str;
cout<<"O="<<str<<endl;
bool match=regex_search(str,m,e);
cout<<"m.size() ="<<m.size()<<endl;
int n=0;
for( n=0;n<m.size();n++)
{
cout<<"m["<<n<<"] :str() =" <<m[n].str()<<endl;
}
}
getch();
myfile.close();
return 0;
}
更新: 现在我可以读取数字了。使用字符串。
但我的文件太大,Visual Studio 崩溃了。
我想要一种在文件中全局搜索的方法。
【问题讨论】:
标签: c++ regex regex-negation regex-lookarounds regex-greedy