我想删除文件中的一行,这是我的代码,它不起作用,但经过长时间的仔细检查,我无法弄清楚为什么。
/* A function to delete the pointed record. */
void S_O::delete_record (const string &id) {
/* Read all records in a vector. */
Temp_Info();
/* Find the excat record. */
vector<string>::iterator iter = std::find (temp_info.begin(), temp_info.end(), id);
/* If find, then delete it. */
if (iter != temp_info.end())
temp_info.erase(iter);
/* Re-input the records in file. */
ofstream file;
file.open ("StudentInfo");
if (!file) {
cerr << "error: unable to open input file: "
<< "file" <<endl;
}
for (size_t i = 0; i != temp_info.size(); i++)
file << temp_info[i] << endl;
/* Clear the vector. */
temp_info.erase (temp_info.begin(), temp_info.end());
}
这是示例文件:
姓名(Name) 学号(Id) 性别(Sex) 成绩(Score)
黄佳敏 1 女 100
李佳惠 2 女 100
这是函数: inline void S_O::Temp_Info() {
/* Create a stream and a file. */
ifstream afile ("StudentInfo");
/* Test if the file is opened successfully. */
if (afile.is_open()) {
while (afile.good()) {
string line;
/* To read file line to line. */
while (getline(afile, line)) {
/* To put lines into a vector. */
temp_info.push_back(line);
}
}
/* Close the stream and save the file. */
afile.close();
}
}
这里有什么问题?