在C ++中的文件中的特定单词之后挤出单词 [英] Extarct a word after a particular word in a file in c++
本文介绍了在C ++中的文件中的特定单词之后挤出单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
char word[30];
string filename;
//map<char,int> mymap;
//map<char,int>::iterator it;
cout << "Name your file> ";
getline( cin, filename );
fstream file( filename.c_str() );
if (!file)
{
cout << "I could not open the file. Fooey.\n";
return EXIT_FAILURE;
}
else
{
string line;
while (getline( file, line ))
{
if ( line.find("new") != string::npos)
{
if ( line.find("int *") != string::npos)
{
//want to catch the pointer in which the memory is allocated
}
}
}
file.close();
}
return EXIT_SUCCESS;
}
我正在创建一个静态分析器以检测内存泄漏..请帮助
I am creating a static analyzer to detect memory leaks ..please help
推荐答案
我认为使用Regular Expressions
可以帮助您完成此项目:
http://linuxgazette.net/issue27/mueller.html [ Henry Spencer的Regexp引擎已重新访问 [ http://www.regular-expressions.info/tutorial.html [
I think usingRegular Expressions
will help you a lot in doing this project :
http://linuxgazette.net/issue27/mueller.html[^]
Henry Spencer''s Regexp Engine Revisited[^]
http://www.regular-expressions.info/tutorial.html[^]
There are many C++ libraries forRegular Expression
pattern matching. Use google to find more resources about it.
您还可以使用对line.find()
的调用的返回值,该返回值给出了字符串的起始位置.但是,您的代码作为分析器存在一个主要问题,因为并非每个人都使用相同的约定来编写代码.考虑以下代码行:
You could also use the return value of the call toline.find()
which gives the starting position of the string. However your code has a major problem as an analyzer in that not everyone writes using the same conventions. consider the following lines of code:
int *pOne;
int* pTwo;
int * // this is a comment
pThree;
所有这些在语法上都是正确的,但是您还需要更多才能识别它们.
All of these are syntactically correct but you need quite a lot more to be able to recognise them.
是的,您是对的...这就是为什么我这样做来提取变量名. ..
yes you are right...that is why i did thi to extract the variable name...
while (getline( file, line ))
{
if ( line.find("new") != string::npos)
{
temp = line;
string::iterator It = temp.begin();
while ( *It != '*' )
{
It++;
}
It++;
while ( *It != '=' )
{
word = *It;
It++;
if (word ==' ')
{
continue;
}
str = str + word;
}
但是如果声明是这样的话:
but again if the declaration is like this:
int* p ;
p = new int;
问题仍然存在..你能帮我一下吗
the problem still persists..can you help plzzz
这篇关于在C ++中的文件中的特定单词之后挤出单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文