ofstream - unable to write onto next time line

Am i doing it right? I am still really noob at this. Everytime i run the code, only a line of text will be saved on the txt file.

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
#include <iostream>
#include <fstream>

using namespace std;

void input(){
    ofstream projectFile("projectfile.txt");

    char name[30];
    double markEng,markMath,markSci;

    cout << "Enter student name : ";
    cin.getline(name,30);
    cout << "Enter obtained of English exam : ";
    cin >> markEng;
    cout << "Enter obtained of Math exam : ";
    cin >> markMath;
    cout << "Enter obtained of Science exam : ";
    cin >> markSci;
    cin.ignore();
    projectFile << name << "  " << markEng << "  " << markMath << "  " <<markSci << endl;
    projectFile.close();};

int main()
{


input();
input();


return 0;


}
You need to put your ostream into append mode, at the moment it overwrites the previous text.
Hello hako2002,

Each time you call the function the "ofstream" is created and destroyed when the function ends. So each time you create the "ofstream" the file is wiped clear and you have a new empty file.

Choices are to open the file for append or consider this code:
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
#include <iostream>
#include <fstream>

using namespace std;

void input(std::ofstream& outFile)
{
    //std::string name;  // <--- Should be using a "std::string".
    char name[30];
    double markEng, markMath, markSci;

    cout << "Enter student name : ";
    cin.getline(name, 30);
    //std::getline(std::cin, name);

    cout << "Enter obtained of English exam : ";
    cin >> markEng;

    cout << "Enter obtained of Math exam : ";
    cin >> markMath;

    cout << "Enter obtained of Science exam : ";
    cin >> markSci;

    cin.ignore();

    outFile << name << "  " << markEng << "  " << markMath << "  " << markSci << endl;

    //projectFile.close();  // <--- Not required as the dtor will close the file when the function looses scope.
};

int main()
{
    const std::string outFileName{ "projectfile.txt" };  // <--- Put File name here.

    std::ofstream outFile(outFileName);

    if (!outFile)
    {
        //std::cout << "\n File " << std::quoted(outFileName) << " did not open" << std::endl;
        std::cout << "\n File \"" << inFileName << "\" did not open." << std::endl;

        return 1;
    }

    input(outFile);

    input(outFile);

    return 0;
}

This will keep the file open until "main" ends the program.

Untested as I did not have your input file to use.

Also when you open a file for input or output you should check that is is open. Otherwise you program would continue trying to use a file that is not open.

If you are going to use a "std::string" in the program you need the header file "<string>".
Andy
Open the file in append mode:

 
ofstream projectFile("projectfile.txt", std::ofstream::app);


All new contents will then be appended to the end of previous.
Last edited on
Topic archived. No new replies allowed.