2013-04-14 105 views
5

我的C++程序出现问题...我的整个程序是学生姓名,成绩和年龄的数据库,当用户想要删除1学生。下面是代码:C++删除文件中的文本行

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; 
    } 
} 

它的工作原理,但问题是,我进入学生数据,当我想通过此功能,它不会删除它删除它,我首先要关闭该程序,然后重新打开该程序,然后调用该函数,以便删除学生数据。

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

+0

你可以更精确的执行它,例如:''我输入学生数据'',怎么样? – Synxis

+1

我在VS2012中运行它。没有看到任何问题。 – shivakumar

+0

假设您在程序运行期间将学生姓名列表存储在其他地方,而不仅仅是在文件中,并且您还需要从那里删除学生。 (或者清除列表,并在您复制姓名时将其填入。) –

回答

3

像这样。它的eaiser显示代码比解释。

#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; 
}