C ++删除文件中的文本行 [英] C++ deleting line of text in a file
问题描述
我的c ++程序有一个问题...我的整个程序是一个数据库的学生的名字,年级和年龄,我有一个问题的功能,当用户想删除1个学生的数据。这里是代码:
I have a problem with my c++ program... My whole program is a database for student names, grades and ages and I have a problem with function when user wants to delete data for 1 student. Here is the code:
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;
}
}
它可以工作,但问题是我进入学生数据,当我想通过此功能删除它不删除它,我首先必须关闭程序,然后重新打开程序,然后调用该函数,所以它删除学生的数据。
It works, but the problem is that I enter student data and when I want to delete it via this function it doesn't delete it, I first have to close the program and then reopen the program and then call that function so it deletes students data.
如何更改它,以便我可以在输入后立即删除学生数据,而无需先关闭程序?
How can I change it so I can delete students data right after inputing, without having to close the program first?
推荐答案
像这样。它的eaiser显示的代码比解释。
Like this. Its eaiser to show the code than to explaine.
#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 ++删除文件中的文本行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!