首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++输入英文名字怎么与txt中名字匹配(求修正)

2012-05-09 
C++输入英文名字如何与txt中名字匹配(求修正)int main() {int count 0int buf_size 100char *namen

C++输入英文名字如何与txt中名字匹配(求修正)
int main() {


int count = 0;
int buf_size = 100;
char *name;

name = new char [buf_size];

  cout<<"Enter a name to search for:";
cin.getline(name, buf_size);
cout<<name<<endl;

//string line;
char ch;
char nu[100]=" \t\n";
ifstream myfile("TSA.txt",ios::in);
if(myfile.is_open())
{
while((ch=myfile.get())!=EOF)
{

//if (strcmp(name,ch)==0)
cout.put(ch);
}
myfile.close();
}
else cout<<"Unalbe to open file";

return 0;
}

目的:输入一英文名字如jenni erice,与txt文件中名字进行匹配,如果文件中也有jenni erice 则输出已找到,如果没有,则输出没有匹配对象

[解决办法]

C/C++ code
const int filesize = 1024*1024*4;static char filedata[filesize ];int main() { int count = 0; int buf_size = 100; char *name; name = new char [buf_size];   cout<<"Enter a name to search for:"; cin.getline(name, buf_size); cout<<name<<endl; //string line; char ch; char nu[100]=" \t\n"; ifstream myfile("TSA.txt",ios::in); if(myfile.is_open()) { myfile.read(&filedata[0],filesize); if(strstr(filedata,name)) {  cout<<"find"; } else   cout<<"not find"; myfile.close(); } else cout<<"Unalbe to open file"; return 0;} 

热点排行