How to retrieve and then add

Im trying to retrieve the txt file workers.txt and leave the information on the file that is already stored on it, then add some more information to it. However when my program goes to pull it, the information is cleared and I'm left with a blank file. Can anyone tell me why and/or how to fix it?

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
// Lab1203.cpp – Write data for 3 additional employees idnumber, hours worked, and
// hourly rate to workers.txt file
// Created by Taft Sanders on 10/31/12

#include <iostream>
#include <fstream>

using namespace std;

int main( )
{
int idNumber = 0;
double rate = 0.0;
double hours = 0.0;

ifstream workers;
workers.open("workers.txt", ios::in);

if(workers.is_open())
{
	for(int count=0; count<5;count++)
	{
	 workers>> idNumber>>rate>>hours;
	}
workers.close();
}

else
{
cout<< "The file could not be opened." << endl;
}

ofstream update;
update.open("workers.txt",ios::out);

if(update.is_open())
{
    for(int count=4;count<8;count++)
    {
        cout<<"Enter ID Number: ";
        cin>>idNumber;
        cout<<"Enter rate: ";
        cin>>rate;
        cout<<"Enter hours: ";
        cin>>hours;

        update<<idNumber<<""<<rate<<""<<hours<<""<<endl;
    }

    workers.close();
}

else
{
    cout << "The file could not be changed."<<endl;
}

system("pause");
return 0;
}


"Workers.txt":
100110.520
10021110.5
10031016
10041020
10051215


After running the program:
100611.518
100710.512
10081015.5

What it needs to be:
100110.520
10021110.5
10031016
10041020
10051215
100611.518
100710.512
10081015.5
Open the file in append mode.
update.open("workers.txt",ios::out | ios::app);
Excellent! Thanks Moschops.
Heres the code for anyone that didn't understand what changed.

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
// Lab1203.cpp – Write data for 3 additional employees idnumber, hours worked, and
// hourly rate to workers.txt file
// Created by Taft Sanders on 10/31/12

#include <iostream>
#include <fstream>

using namespace std;

int main( )
{
int idNumber = 0;
double rate = 0.0;
double hours = 0.0;

ifstream workers;
workers.open("workers.txt", ios::in);

if(workers.is_open())
{
	for(int count=0; count<5;count++)
	{
	 workers>> idNumber>>rate>>hours;
	}
workers.close();
}

else
{
cout<< "The file could not be opened." << endl;
}

ofstream update;
update.open("workers.txt",ios::out | ios::app);

if(update.is_open())
{
    for(int count=4;count<7;count++)
    {
        cout<<"Enter ID Number: ";
        cin>>idNumber;
        cout<<"Enter rate: ";
        cin>>rate;
        cout<<"Enter hours: ";
        cin>>hours;

        update<<idNumber<<""<<rate<<""<<hours<<""<<endl;
    }

    workers.close();
}

else
{
    cout << "The file could not be changed."<<endl;
}

system("pause");
return 0;
}
Last edited on
Topic archived. No new replies allowed.