【发布时间】:2013-11-11 03:43:20
【问题描述】:
此代码应该在文件中搜索每个客户端的 iterNo,然后显示该信息。我的问题是,无论我搜索什么数字,它总是显示文件中的第一项。任何帮助将不胜感激。现在真的很绝望......提前感谢您的回复。
void View()
{
char ans; //answer to store 'y' or 'n'
Intervention inte;
int interNo;
ifstream InfoFile("info.dat", ios::in |ios::binary|ios::app);
if (!InfoFile)
{
cout <<"Error - random files could not be opened.";
}
else
{
while (true)
{
cout << "\nEnter client's interNo number to display ";
cin >> interNo;
inte.getClient();
inte.getAddress();
inte.getTelNo();
inte.getTime();
inte.getDate();
inte.getAnimal();
//InfoFile.seekg(sizeof(Intervention)*(interNo - 1));
InfoFile.read(reinterpret_cast<char *> (&inte), sizeof(Intervention));
//if not eof show data
if (!InfoFile.eof())
{
//Display the processed
// info for the employee
inte.display();
}
cout << "Enter another employee's information to display ? [y / n] ";
fflush(stdin);
ans = ' ';
while (ans != 'y' && ans != 'Y' &&
ans != 'n' && ans != 'N')
{
ans = _getch();
} cout << endl;
if (ans == 'n' || ans == 'N')
{
exit(1);
}
}//end while
}//end else
}//end function view
//**********************************************
【问题讨论】:
-
你没有做任何搜索;您只是使用
read从文件中读取第一个值。 (你注释掉了上面的行,它确实移动了文件指针。)如果你想定位一个特定的客户端,你必须从文件的开头实际移动文件指针。 -
真相是我不知道把那条线放在哪里......如果我把它放在 (if (!InfoFile.eof())) 之外的任何地方......那么它就直接到问我是否要显示另一个而不显示任何内容
标签: c++ filestream random-access