jeudi 22 avril 2021

How to delete stored data in a text file?

The code below was modified based on my original one to fix an error thanks to @RemyLebeau. It woks great but a new issue have showed up where when I want to delete a student data using option 4 from the menu, after entering their ID, it says their data is deleted but its not. And at the end of the console the message "error reading data from file", under the Standard Error Stream (cerr), keeps showing up. And all my tries to fix it failed. error_display

//Project on Student Management
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

struct Course
{
    string courseName;
    string grade;
    string status;
};

class Student
{
    int id;
    int year;
    float cgpa;
    vector<Course> courses;
    string academic_advisor;
    
public:
    Student();

    void getData();
    void showData() const;
    int getID() const { return id; }

    friend ostream& operator<<(ostream &out, const Student &s);
    friend istream& operator>>(istream &in, Student &s);
};

void writeStr(ostream &out, const string &str)
{
    size_t size = str.size();
    out.write((char*)&size, sizeof(size));
    if (size)
        out.write(str.c_str(), size);
}

void readStr(istream &in, string &str)
{
    str.clear();

    size_t size;
    if (in.read((char*)&size, sizeof(size)))
    {
        if (size > 0)
        {
            str.resize(size);
            in.read(&str[0], size);
        }
    }
}

ostream& operator<<(ostream &out, const Course &c)
{
    writeStr(out, c.courseName);
    writeStr(out, c.grade);
    writeStr(out, c.status);
    return out;
}

istream& operator>>(istream &in, Course &c)
{
    readStr(in, c.courseName);
    readStr(in, c.grade);
    readStr(in, c.status);
    return in;
}

ostream& operator<<(ostream &out, const Student &s)
{
    out.write((char*)&s.id, sizeof(s.id));
    out.write((char*)&s.year, sizeof(s.year));
    out.write((char*)&s.cgpa, sizeof(s.cgpa));
    writeStr(out, s.academic_advisor);

    size_t NumberOfcourses = s.courses.size();
    out.write((char*)&NumberOfcourses, sizeof(NumberOfcourses));

    for(int i = 0; i < NumberOfcourses; ++i)
    {
        if (!(out << s.courses[i]))
            break;
    }

    return out;
}

istream& operator>>(istream &in, Student &s)
{
    s.courses.clear();

    in.read((char*)&s.id, sizeof(s.id));
    in.read((char*)&s.year, sizeof(s.year));
    in.read((char*)&s.cgpa, sizeof(s.cgpa));
    readStr(in, s.academic_advisor);

    size_t NumberOfcourses;
    if (in.read((char*)&NumberOfcourses, sizeof(NumberOfcourses)))
    {
        s.courses.reserve(NumberOfcourses);
        Course c;

        for(size_t i = 0; i < NumberOfcourses; ++i)
        {
            if (in >> c)
                s.courses.push_back(c);
            else
                break;
        }
    }

    return in;
}

Student::Student()
{
    id = 0;
    year = 0;
    cgpa = 0.0f;
}

void Student::getData()
{
    courses.clear();

    cout << "\n\nEnter Student Details......\n";
    cout << "Enter ID No.     : "; cin >> id;
    cout << "Enter Intake Year of the Student: "; cin >> year;
    cout << "Enter number of Taken courses: ";

    size_t NumberOfcourses;
    cin >> NumberOfcourses;
    cin.ignore();

    courses.reserve(NumberOfcourses);
    Course c;

    for(int a = 0; a < NumberOfcourses; ++a)
    {
        cout << "\nEnter Subject Name: ";
        getline(cin, c.courseName);
            
        cout << "Enter Grade of Subject: ";
        getline(cin, c.grade);
            
        cout << "Enter Subject Status: ";
        getline(cin, c.status);

        courses.push_back(c);
    }

    cout << "Enter student CGPA: ";
    cin >> cgpa;
    cin.ignore();

    cout << endl;
    cout << "Enter Name of Academic Advisor of Student: ";
    getline(cin, academic_advisor);
}

void Student::showData() const
{
    cout << "\n\n.......Student Details......\n";
    cout << "ID No.                                   : " << id << endl;
    cout << "Intake Year                              : " << year << endl;
    cout << "Subjects Taken in Previous Semester      : " << endl;
    for(size_t t = 0; t < courses.size(); ++t)
    {
        cout << "\t" << courses[t].courseName << ": " << courses[t].grade << " (" << courses[t].status << ") ";
        cout << endl;
    }
    cout << "CGPA                                     : " << cgpa << endl;
    cout << "Name of academic advisor of Student      : " << academic_advisor << endl;
    cout << endl;
}

void addData()
{
    Student s;
    s.getData();

    ofstream fout("Students.txt", ios::binary|ios::app);
    if (fout << s)
        cout << "\n\nData Successfully Saved to File....\n";
    else
        cerr << "\n\nError Saving Data to File!\n";
}

void displayData()
{
    ifstream fin("Students.txt", ios::binary);

    Student s;
    while (fin >> s)
    {
        s.showData();
    }

    if (fin)
        cout << "\n\nData Reading from File Successfully Done....\n";
    else
        cerr << "\n\nError Reading Data from File!\n";
}

void searchData()
{
    int n;
    cout << "Enter ID Number you want to search for : ";
    cin >> n;    

    ifstream fin("Students.txt", ios::binary);

    Student s;
    bool flag = false;

    while (fin >> s)
    {
        if (n == s.getID())
        {
            cout << "The Details of ID No. " << n << " are: \n";
            s.showData();
            flag = true;
            break;
        }
    }

    if (fin)
    {
        if (!flag)
            cout << "The ID No. " << n << " not found....\n\n";
        cout << "\n\nData Reading from File Successfully Done....\n";
    }
    else
        cerr << "\n\nError Reading Data from File!\n";
}

void deleteData()
{
    int n;
    cout << "Enter ID Number you want to delete : ";
    cin >> n;
    
    ifstream fin("Students.txt", ios::binary);
    ofstream fout("TempStud.txt", ios::binary);
    ofstream tout("TrashStud.txt", ios::binary|ios::app);

    Student s;
    bool flag = false;

    while (fin >> s)
    {
        if (n == s.getID())
        {
            cout << "The Following ID No. " << n << " has been deleted:\n";
            s.showData();
            tout << s;
            flag = true;
        }
        else
        {
            fout << s;
        }
    }

    if (fin)
    {
        if (fout)
        {
            fin.close();
            fout.close();

            if (!flag)
            {
                cout << "The ID No. " << n << " not found....\n\n";
                remove("tempStud.txt");
            }
            else
            {
                remove("Students.txt");
                rename("tempStud.txt", "Students.txt");

                cout << "\n\nData Successfully Deleted from File....\n";
            }
        }
        else
        {
            cerr << "\n\nError Saving Data to File!\n";
        }
    }
    else
    {
        cerr << "\n\nError Reading Data from File!\n";
    }
}


void modifyData()
{
    int n;    
    cout << "Enter ID Number you want to Modify : ";
    cin >> n;
    
    ifstream fin("Studentstxt", ios::binary);
    ofstream fout("TempStud.txt", ios::binary);

    Student s;
    bool flag = false;

    while (fin >> s)
    {
        if (n == s.getID())
        {
            cout << "The Following ID No. " << n << " will be modified with new data:\n";
            s.showData();
            cout << "\n\nNow Enter the New Details....\n";
            s.getData();
            flag = true;
        }

        fout << s;
    }
    
    if (fin)
    {
        if (fout)
        {
            fin.close();
            fout.close();

            if (!flag)
            {
                cout << "The ID No. " << n << " not found....\n\n";
                remove("TempStud.txt");
            }
            else
            {
                remove("Students.txt");
                rename("TempStud.txt", "Students.txt");

                cout << "\n\nData Successfully Updated in File....\n";
            }
        }
        else
        {
            cerr << "\n\nError Saving Data to File!\n";
        }
    }
    else
    {
        cerr << "\n\nError Reading Data from File!\n";
    }
}

void project()
{
    int ch;
    do
    {
        system("cls");
        cout << "...............STUDENT MANAGEMENT SYSTEM..............\n";
        cout << "====================SEGI University=================\n";
        cout << "0. Exit from Program\n";
        cout << "1. Write Data to File\n";
        cout << "2. Read Data From File\n";
        cout << "3. Search Data From File\n";
        cout << "4. Delete Data From File\n";
        cout << "5. Modify Data in File\n";
        cout << "Enter your choice  : ";
        cin >> ch;
        system("cls");
        switch (ch)
        {
            case 1: addData(); break;
            case 2: displayData(); break;
            case 3: searchData(); break;
            case 4: deleteData(); break;
            case 5: modifyData(); break;
        }
        system("pause");
    }
    while (ch != 0);
}

int main()
{
    project();
}

Aucun commentaire:

Enregistrer un commentaire