updating the contents of a file

So what I need to do for this program is to read from a transaction file all the data and then update the master file accordingly. Everything reads in okay but my problem is when I have to update the master file. What should happen is that after the master balance value is updated, it should replace the master balance value of the corresponding id number (the id numbers look like 1111, 2222, etc.) in the master record file. I did some debugging and everything works up until the method to change the master file. Any suggestion?
Thanks


in the main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  string var_string;
    for(int i=0;i <10; i++){
    switch (transac[i].type) {
        case 'o':
            for (int d = 0; d <5; d++){
                if (transac[i].cust_num == master[d].cust_num){
                    total = transac[i].num_ordered * transac[i].price;
                    prev_balance = master[d].bal_due;
                    master[d].bal_due += total;
                    cout << master[d].bal_due << " ^ ";
                    var_string = to_string(master[d].bal_due);
                    cout << var_string<< " ";

                 /*
                    //This is how I think it should be done but I tried this method but it didn't work. 
                   //It wasn't properly changing the file
                    char temp[10] = {};
                    strcpy(temp, var_string.c_str());
                    cout << temp;
                    fin2.seekg('$');
                    fin2.write(temp,10);
                  */
                    
                }


transaction file (ignore the lines that begin with p)
1st val = type, 2nd = cust_num, 3rd = item, 4th = num_ordered, 5th = price
1
2
3
4
5
6
7
8
9
10
11

o 1111 book 3 3.50
p 1111 apple 7 .25
p 2222 book 1 1.00
o 2222 pear 3 .35
p 3333 peach 10 .75
o 3333 phone 1 200.00
o 4444 steak 3 25.00
p 4444 stamps 50 .10
o 5555 picture 5 20.00
p 5555 apple 10 .25


master file
1
2
3
4
5
John Doe/ 1111 $341.5
Joe Dirt/ 2222 $103.11
Theresa Smith/ 3333 $1066.85
Jennifer Jones/ 4444 $97.56
Moe Simpson/ 5555 $566.98


Did I miss the part, in which you are searching masterfile for the correct row?
Presumably the master file is an ordinary text file? It certainly looks that way. Unless it has a fixed-width layout (again it doesn't look that way), you won't be able to simply overwrite individual details, as the length may need to change.

Probably the simplest approach, unless the file is extremely huge, is to read the entire file into an array, make the changes to the array then write out the entire array, thus replacing the whole file with an updated version.

Edit:
The alternative, given that both the transaction file and master file have a customer number and are already sorted in ascending order on that key field, is to read one record at a time from each of the files. Then compare the keys (customer numbers), decide what action to take, and write out the master file records one by one to a new master file. This used to be a standard approach, and an essential one if the master file was contained on reels of magnetic tape.
Last edited on
Chervil, I took your advice and cleared out the file while replacing it with the newer version. It working well now.
JockX, is it possible to search for a specific row?
I know I can search for a specific string in a file but would I be able to replace it with a new string in that specific point in a file?

Thanks guys.
If you want to replace a string in the file, the new string would need to be of precisely the same length. For example you could not replace "$97.56" with "$1066.85" since the second string is longer. But it would be ok to replace "103.11" with "566.98" as they are the same length.

in addition you'd need to take care with establishing the correct position in the file.
Last edited on
Topic archived. No new replies allowed.