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 则输出已找到,如果没有,则输出没有匹配对象
[解决办法]
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;}