Having Issues With Writing to a File

In the following code I am trying to write book information into a .txt file so that I can then read from it later to display all of it. However, I noticed that upon execution of the loop it constantly overwrites the 4 values that are written.
Rather then continuing to write more then 4 entries.

I have searched quite a few places over the internet and have found nothing that specifically relates to this problem. And even if it did, it was usually written in another language or used syntax that I have no experience with and had trouble understanding. If someone could help me out, then that would be awesome and much appreciated.

I have also asked my friends and my lab TA who had issues helping me as well, he also didn't have much time to explain everything as it was the end of class but he did say that writing to a file is really the only way that will accomplish the storing of an unlimited amount of book data. I will talk to him again on Tuesday, and yes I have been working on this for quite some time as I was assigned this on Monday. I figured I might get some pointers leading me in the right direction on this site. I like to finish things early.

Also I will admit that this is a question for homework, I have no interest in receiving help on completing the assignment in its entirety as I feel certain that I know how to read from the file and display it. I just need help with this specific issue, it's been kind of a roadblock for me.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <fstream>
using namespace std;


//Function to take values from main, and write them in the .txt file
void getbookInfo(int &prospectCE, int &numBooks, double &bookCode, double &single_CC, ofstream &bookInfoFile)
{

        bookInfoFile << bookCode << endl;

        bookInfoFile << single_CC << endl;

        bookInfoFile << prospectCE << endl;

        bookInfoFile << numBooks << endl;

        bookInfoFile.close();
}


int main()
{
    
    //Declaration of file, and all needed variables.
    ofstream bookInfoFile;
    int ceProspect, booksNum;
    double codeBook, cc_Single;
    char enterBook;
    
    //Explanation statement, and the starting point of the loop.
    cout << "This program is going to display the total profit after selling all of the books that you enter." << endl;
    cout << "Do you wish to enter a book and it's information? Type Y for yes or N for no." << endl;
    cin >> enterBook;


    //The loop which collects the book information, then sends it to the function which writes the information for further use. 
    while (enterBook == 'Y' || enterBook == 'y')
    {
        bookInfoFile.open("LABPROA3");
        cout << "Please enter the unique code for the book." << endl;
        cin >> codeBook;

        cout << "Please enter the single copy cost for the book." << endl;
        cin >> cc_Single;

        cout << "Now enter the total number of people participating in the class that this book is needed for." << endl;
        cin >> ceProspect;

        cout << "Now enter the total number of copies that you have for this book." << endl;
        cin >> booksNum;

        getbookInfo(ceProspect, booksNum, codeBook, cc_Single, bookInfoFile);

        bookInfoFile.close();

        cout << "Would you like to enter another book?" << endl;
        cin >> enterBook;

    }



       return 0;

   }

    
Last edited on
You need to open the file in append if you are going to close() it like you currently have it in your functions.

Otherwise just open it once and close it later in your program after you are done collecting user input.
Last edited on
So do I need to open the file outside of the while loop in main? Or open it in the function? I'm sorry but I'm not quite sure what you mean by append.
You are always writing at the beginning of output file instead of appending text to the end of it.
Try to initialize bookIntoFile with ios::app parameter: ofstream bookInfoFile("LABPROA3", ios::app); and close it after you write all data to it (after that while loop)
Last edited on
open the file once (before while loop) and close if once (after while loop) and you dont have to worry about append mode.
if you want to keep your code as it is, do what mobotus says.
Append mode means, the data to be written will be written below the data already in the file.
So its like adding items to a list.
My code now looks like this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <fstream>
using namespace std;


//Function to take values from main, and write them in the .txt file
void getbookInfo(int &prospectCE, int &numBooks, double &bookCode, double &single_CC, ofstream &bookInfoFile)
{

        bookInfoFile << bookCode << endl;

        bookInfoFile << single_CC << endl;

        bookInfoFile << prospectCE << endl;

        bookInfoFile << numBooks << endl;

        bookInfoFile.close();
}


int main()
{

    //Declaration of file, and all needed variables.
    ofstream bookInfoFile;
    int ceProspect, booksNum;
    double codeBook, cc_Single;
    char enterBook;

    //Explanation statement, and the starting point of the loop.
    cout << "This program is going to display the total profit after selling all of the books that you enter." << endl;
    cout << "Do you wish to enter a book and it's information? Type Y for yes or N for no." << endl;
    cin >> enterBook;

    bookInfoFile.open("LABPROA3");
    //The loop which collects the book information, then sends it to the function which writes the information for further use.
    while (enterBook == 'Y' || enterBook == 'y')
    {

        cout << "Please enter the unique code for the book." << endl;
        cin >> codeBook;

        cout << "Please enter the single copy cost for the book." << endl;
        cin >> cc_Single;

        cout << "Now enter the total number of people participating in the class that this book is needed for." << endl;
        cin >> ceProspect;

        cout << "Now enter the total number of copies that you have for this book." << endl;
        cin >> booksNum;

        getbookInfo(ceProspect, booksNum, codeBook, cc_Single, bookInfoFile);



        cout << "Would you like to enter another book?" << endl;
        cin >> enterBook;

    }

    bookInfoFile.close();


    return 0;
}


But now it doesn't overwrite the information, but it only keeps the first 4 entries. When I enter more then one book
Also rich1 after changing the initialization to ios::app it actually didn't write anything into the file at all.
The code is now this, rich1 was correct but he had the ios::app in the wrong spot. The mode is not used after the initialization but when you open the function with:
bookInfoFile.open("LABPROA3", ios::app); on line 36

To see more modes one can look at
http://www.cplusplus.com/doc/tutorial/files/

The answer was in front of me all along. And I failed to see it. haha

Now it works perfectly, thanks for the advice guys.

:D
Last edited on
Topic archived. No new replies allowed.