Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My program will read from the file just fine but when it goes towrite to the fil

ID: 3613448 • Letter: M

Question

My program will read from the file just fine but when it goes towrite to the file when the program closes it just makes the fileempty and doesn't write anything to it. When I comment out thefirst part that reads the info from a file it writes to the filelike it should but I can't get it to do both and need help as towhat I'm doing wrong. Here is just the opening of the file andclosing of the file as I have determined its something wrong withone of two or both.


fstream file;
    file.open("MyFriends.dat", ios::in);

    // test to see if MyFriends.txt file openedcorrectly
    if(file.fail())
    {
        cout << "Error openingfile. ";
        return 0;
    }

    // function call to read info from the file
    readFile(Friends, file);

    file.close();
.......    //other code in between

    file.open("MyFriends.txt", ios::out);

    // test to see if MyFriends.txt file openedcorrectly
    if (file.fail())
    {
        cout << "Error openingfile. ";
        return 0;
    }

    // function call to save info to file
    saveFile(Friends, file);

    file.close();

Explanation / Answer

x.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote