读取文本数据并查询关键词
RT
各位大哥,小弟新学C++,想编一个如题的程序,下面的代码编译过了,但是运行的时候便崩溃了。求各位大哥帮帮忙看看啊。。
还有,想来这个程序肯定很多不够简介,注释的理解也有错误的地方,还请各位大哥指正。。
小弟这边拜谢了。。
[code=C/C++][/code]
#include <unistd.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string.h>
using namespace std;
int main()
{
//读取文档
ifstream fin("dictionary.txt");
string line,LineByLine;
//输入关键字KeyWord;
char * KeyWord;
cout<<"请输入待查询缩写:";
cin>> KeyWord;
while (std::getline(fin,line)) //把“流fin”中的数据按行存在line里。
{
stringstream stream(line);
while (stream >> LineByLine)//把 line的数据存在LineByLine里。
{
// 检索LineByLine中是否含有输入的关键字KeyWord ;
//若有,输出整行;若无,跳过,继续。
int test=LineByLine.find(KeyWord,0);//str1= "abcdefghijklmn "; str2= "fg ";
//如果没有则返回-1,有则返回第一个匹配的位置
if (test!=-1)
{
cout << "LineByLine:" << LineByLine << endl;
break;
}
}
}
fin.close();
return 0;
}
[解决办法]
char * KeyWord; 应该要用String吧
[解决办法]
//输入关键字KeyWord;
char * KeyWord;
cout<<"请输入待查询缩写:";
cin>> KeyWord;
这一段错了,可以改为如下:
string KeyWord;
cout<<"请输入待查询缩写:";
cin>>KeyWord;
[解决办法]