Editing certain line in File C++

Hi guys nid some helps over here.

Well,i hav a question requiring me to edit sales achieved by an employee for a particular month.

I'm having a dat.file which following format
Employee ID
Name
Sales(for a year)

For example
A00001
Harlem Shake
10.00 20.00 30.00 40.00 50.00 60.00 70.00 80.00 90.00 100.00 110.00 120.00


Wad I wanna is TO EDIT only the sales for July which is 70.00 to a new amount with other months remain intact.

I need to search particular id which will then link to their respective sales.

1
2
3
4
5
6
7
8
9
10
11
12
13
    ofstream outFile;
    string empID;
    cout<<"Please Enter Employee ID:\n";
    cin>>empID;
    cout<<"Kindly input the month you wish to edit:\n";
    cout<<"1)January     2)February     3)March      4)April\n";
    cout<<"5)May         6)June         7)July       8)August\n";
    cout<<"9)September  10)October     11)November  12)December\n";
    cin>>month;
    outFile.open("employee.dat",ios::out|ios::binary|ios::app);
    cout.precision(2);
    cout<<"ID Entered:"<<empID<<endl;
    


I tried to run myself before.But I could only create a new line instead of replacing.

To my understanding with ios::app ..will use the same file for editting..

Tq
Last edited on
with ios::app you will append lines to the file opened without touching anything already written.

However, with your code, if you remove ios::app you would delete the content of the file EXCEPT what you are modifying (if other employes present in your .dat, they will deleted).

You must find a way how to modify your line without deleting the other parts of the file...
But removing ios::app
Wont it create a new file?

Tq
Creating a new file is not a bad idea anyway.
1
2
3
read(A) modify write(B)
move(A -> A.bak)
move(B -> A)

another side question;
any ideas on how to convert every first letter to capital if small letters were entered?

i mean when it outFile it.

i shall see the new format in my dat.file.

Tq
Topic archived. No new replies allowed.