2013-04-14 69 views
5

Tôi gặp sự cố với chương trình C++ ... Toàn bộ chương trình của tôi là cơ sở dữ liệu cho tên, cấp độ và tuổi của học sinh và tôi gặp vấn đề với chức năng khi người dùng muốn xóa dữ liệu cho 1 sinh viên. Đây là mã số:C++ xóa dòng văn bản trong một tệp

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

Nó hoạt động, nhưng vấn đề là tôi nhập dữ liệu sinh viên và khi tôi muốn xóa nó thông qua chức năng này nó không xóa nó, trước tiên tôi phải đóng chương trình và sau đó mở lại chương trình và sau đó gọi hàm đó để nó xóa dữ liệu của sinh viên.

Làm cách nào để thay đổi nó để tôi có thể xóa dữ liệu sinh viên ngay sau khi nhập, mà không phải đóng chương trình trước?

+0

Bạn có thể chính xác hơn với cách bạn thực hiện nó, ví dụ: '" Tôi nhập dữ liệu sinh viên "', làm cách nào? – Synxis

+1

Tôi đã chạy nó trong VS2012. Không thấy bất kỳ vấn đề nào. – shivakumar

+0

Có lẽ bạn lưu trữ danh sách tên sinh viên ở một nơi khác trong suốt quá trình chạy chương trình của bạn, không chỉ trong tệp và bạn cũng cần phải xóa sinh viên khỏi đó. (Hoặc xóa danh sách và điền lại vào khi bạn sao chép các tên.) –

Trả lời

3

Như thế này. Của nó háo hức để hiển thị mã hơn để 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ác vấn đề liên quan