gpt4 book ai didi

C++删除文件中的文本行

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:40:17 26 4
gpt4 key购买 nike

我的 C++ 程序有问题...我的整个程序是一个包含学生姓名、年级和年龄的数据库,当用户想要删除 1 名学生的数据时,我的功能出现问题。这是代码:

void deletestudentdata()
{
string name, grade, tname;
int age, x=0; // x - "counter" to check if user entered wrong name

system("cls");
cout << "Enter name of the student you want to erase from database" << endl;
cin >> tname;

ifstream students("students.txt");
ofstream temp("temp.txt"); // temp file for input of every student except the one user wants to delete

while(students >> name >> grade >> age)
{
if(tname!=name){ // if there are students with different name, input their data into temp file
temp << name << ' ' << grade << ' ' << age << endl;
}
if(tname==name){ // if user entered correct name, x=1 for later output message that the user data has been deleted
x=1;
}
}
students.clear(); // clear eof and fail bits
students.seekg(0, ios::beg);
students.close();
temp.close();
remove("students.txt");
rename("temp.txt","students.txt");
if(x==0){ // x was set to 0 at start, so if it didn't change, it means user entered the wrong name
cout << "There is no student with name you entered." << endl;
}
else{ // x is not 0, it means user entered the correct name, print message that students data has been deleted
cout << "Student data has been deleted." << endl;
}
}

它有效,但问题是我输入了学生数据,当我想通过这个函数删除它时它没有删除它,我首先必须关闭程序然后重新打开程序然后调用该函数所以它会删除学生数据。

如何更改它以便我可以在输入后立即删除学生数据,而无需先关闭程序?

最佳答案

当找不到学生姓名时它不起作用它只是说学生姓名已删除而不是说找不到

#include <string>
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;




void displaystudentdata()
{
string name, grade, tname;
int age, x=0; // x - "counter" to check if user entered wrong name

system("cls");

ifstream students("students.txt");


cout<<"-------------------------------------------------------------------\n\n";
while(students >> name >> grade >> age)
{

cout<<"Name= "<<name <<", Grade= "<< grade <<" , Age= " <<age<<"\n";
}
students.clear(); // clear eof and fail bits
students.seekg(0, ios::beg);
students.close();
}

void deletestudentdata()
{
string name, grade, tname;
int age, x=0; // x - "counter" to check if user entered wrong name



ifstream students("students.txt");
ofstream temp("temp.txt"); // temp file for input of every student except the one user wants to delete


cout<<"-------------------------------------------------------------------\n\n";

cout << "Enter name of the student you want to erase from database >" << endl;
cin >> tname;

//ifstream students("students.txt");
//ofstream temp("temp.txt"); // temp file for input of every student except the one user wants to delete

while(students >> name >> grade >> age)
{
if(tname!=name){ // if there are students with different name, input their data into temp file
temp << name << ' ' << grade << ' ' << age << endl;
}
if(tname==name){ // if user entered correct name, x=1 for later output message that the user data has been deleted
x=1;
}
}
students.clear(); // clear eof and fail bits
students.seekg(0, ios::beg);
students.close();
temp.close();
remove("students.txt");
rename("temp.txt","students.txt");
if(x==0){ // x was set to 0 at start, so if it didn't change, it means user entered the wrong name
cout << "There is no student with name you entered." << endl;
}
else{ // x is not 0, it means user entered the correct name, print message that students data has been deleted
cout << "Student data has been deleted." << endl;
}
}


int main(void)
{



displaystudentdata();
deletestudentdata();
displaystudentdata();
cout << "Student data has been deleted. \n\n" << endl;
cout<<" \nPress any key to continue\n";
cin.ignore();
cin.get();

return 0;
}

关于C++删除文件中的文本行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15999193/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com