Why my previous file delete???

This is a discussion on Why my previous file delete??? within the C++ Programming forums, part of the General Programming Boards category; my code is as below... i wan add student in my txt file... when i close system, restart again and ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    Kuala Lumpur, Malaysia, Malaysia
    Posts
    2

    Why my previous file delete???

    my code is as below...
    i wan add student in my txt file...
    when i close system, restart again and add student... the previous data that i entered lost...WHY???

    Code:
    int main()
    {
        int ans, loop;
        for (loop=0; loop<3;loop++)
        {
        cout<<"Enter 1 to add student or 2 to view Student: ";
        cin>>ans;
        if (ans==1)
        {    fstream outFile;
        const int QUIT = 0;
        int idNum;
        string name;
        double gpa;
        outFile.open("file2.txt", ios::out | ios::binary);
        cout<<"ENTER Student ID number or "<< QUIT << " to quit ";
        cin>>idNum;
        while (idNum!=QUIT)
        {
            cout<<"Enter Name: ";
            cin>>name;
            cout<<"Enter GPA: ";
            cin>>gpa;
            Student aStudent(idNum, name, gpa);
            outFile.seekp((idNum -1) * sizeof(aStudent));
            outFile.write(reinterpret_cast<const char*>(&aStudent), sizeof (Student));
            cout<<"Enter student ID number or "<<QUIT<<" to quit ";
            cin>>idNum;
        }
        outFile.close();
        return 0;
        }
    
        else
    
        {
        Student aStudent;
        ifstream inFile;
        const int QUIT = 0;
        int idNum;
        inFile.open("file2.txt", ios::in | ios::binary);
        inFile.read(reinterpret_cast<char*>(&aStudent), sizeof(Student));
        cout<<"ENTER Student ID number to view or "<< QUIT << " to quit ";
        cin>>idNum;
        while (idNum!=QUIT)
        {
            inFile.seekg((idNum -1) * sizeof(aStudent));
            inFile.read(reinterpret_cast<char*>(&aStudent), sizeof (Student));
            cout<<"ID : "<<aStudent.stuId<<endl;
            cout<<"Name : "<<aStudent.name<<endl;
            cout<<"GPA : "<<aStudent.gpa<<endl;
            cout<<"Enter student ID number or "<<QUIT<<" to quit ";
            cin>>idNum;
        }
        inFile.close();
        return 0;}
        }
    PLEASE HELP.....
    Last edited by macgyverwong; 07-04-2011 at 04:54 AM.

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    I haven't checked all your code, but have a look at the flags for ofstream::open, there should be one to append to your file instead of overwriting it.

    You should also check the return values of your functions. Maybe your files couldn't be opened?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Location
    Kuala Lumpur, Malaysia, Malaysia
    Posts
    2
    Quote Originally Posted by nvoigt View Post
    I haven't checked all your code, but have a look at the flags for ofstream:pen, there should be one to append to your file instead of overwriting it.

    You should also check the return values of your functions. Maybe your files couldn't be opened?
    Where should i chose to append???
    Cause i initially created 10000 space for student using this code
    Code:
    //create 10000space.
    int main()
    {
        const int maxRoom = 10000;
        Student aStudent;
        ofstream outFile;
        outFile.open("file2.txt", ios::out | ios::binary);
        for (int x = 0; x < maxRoom ; x++)
            outFile.write(reinterpret_cast <const char*> (&aStudent), sizeof(Student));
        outFile.close();
        return 0;
    }

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,710
    the previous data that i entered lost...WHY???
    Because this:
    Code:
    outFile.open("file2.txt", ios::out | ios::binary);
    erases the file when you open it. To open the file for appending you need to add ios::app to your statement.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can not delete a file
    By George2 in forum C Programming
    Replies: 7
    Last Post: 07-30-2007, 05:49 AM
  2. Delete a file that's in use
    By Queatrix in forum C++ Programming
    Replies: 4
    Last Post: 09-06-2006, 09:18 AM
  3. How to delete a file?
    By Seek n Destroy in forum C Programming
    Replies: 2
    Last Post: 10-15-2002, 11:03 PM
  4. How can I delete a file
    By pinkcheese in forum C++ Programming
    Replies: 12
    Last Post: 06-20-2002, 03:17 AM
  5. How to delete a File?
    By nag in forum C Programming
    Replies: 2
    Last Post: 05-11-2002, 04:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21