我想搜索学生ID,班级,部分,性别,电子邮件和电话号码的记录。这里是代码
void searching()
{
cout << "\t\t\t\tSearching the Record" << endl;
int offset;
std::string se_id, se_name, se_email, se_home, se_clas, se_tele, se_cell, se_sec, se_gender, line;
ifstream filee;
filee.open("Student.txt");
cout << "\nType id of student you want to search:";
cin >> se_id;
if (filee.is_open())
{
while (!filee.eof())
{
getline(filee, line);
if (((offset = line.find(se_id, 0))) != string::npos)
{
cout << "\nId ::" << se_id;
cout << "\nSearch found";
break;
}
}
filee.close();
}
else
{
cout << "search not found";
}
}
答案 0 :(得分:0)
这应该可以得到你想要的东西(见下文)。我留给你检查以确保文件正确打开。此外,让用户知道是否找不到记录可能会有所帮助。
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main() {
ifstream in("Student.txt");
int target_id, id;
cin >> target_id;
string record, field;
// while there are records in the file
while(getline(in, record)) {
istringstream ss(record);
ss >> id;
// Check to see if target id equals record id
if(target_id == id) {
cout << id;
// It does, so let's print the rest of the fields
while(getline(ss, field, ',')) {
cout << field << " ";
}
cout << endl;
}
}
in.close();
return 0;
}