210 likes | 303 Views
File Update Lesson xx. Objectives. Working with multiple files Copying files Updating files. Data Files. master.dat. masterc.dat. trans.dat. Helen 10 Julie 20 Annie 40 Lena 30 May 50 Alan 60. Helen 10 Julie 20 Annie 40 Lena 30 May 50 Alan 60. Julie 34 Sam 18
E N D
Objectives • Working with multiple files • Copying files • Updating files
Data Files master.dat masterc.dat trans.dat Helen 10 Julie 20 Annie 40 Lena 30 May 50 Alan 60 Helen 10 Julie 20 Annie 40 Lena 30 May 50 Alan 60 Julie 34 Sam 18 Lena 55 Lee 43 Helen 15
Program Description Open 3 files: master.dat, trans.dat, masterc.dat Make a copy of master.dat on masterc.dat Read in the 3rd record of trans.dat Find the record on masterc.dat with the same name as the 3rd transaction record. Add the amount from the transaction record to the amount in the master record Write the updated record onto masterc.dat
Updated File master.dat masterc.dat trans.dat Helen 10 Julie 20 Annie 40 Lena 30 May 50 Alan 60 Helen 10 Julie 20 Annie 40 Lena 85 May 50 Alan 60 Julie 34 Sam 18 Lena 55 Lee 43 Helen 15
Program Listing Part 1 #include<fstream> using std::fstream; using std::ios; #include <iostream> using std::cout; using std::endl; #include<string> struct record { char name[15]; float amt; };
Program Listing Part 2 int main() { fstream mf("master.dat",ios::in | ios::binary); fstream mc("masterc.dat",ios::out | ios::binary); //Copy of master fstreamtf("trans.dat",ios::in | ios::binary); record master,trans; //Copy master file onto masterc while ( mf.read((char *)&master,sizeof(master))) { cout<<master.name <<" " <<master.amt <<endl; mc.write((char *)&master,sizeof(master)); //Write to Master copy file } mf.close(); //Close Master file mc.close(); mc.open("masterc.dat",ios::in | ios::out |ios::binary);
Program Listing Part 3 //Read in 3rd transaction record long intrecsize = sizeof(record); int found = 0; tf.seekg(2*recsize,ios::beg); tf.read((char *)&trans,sizeof(trans)); cout<<"\n\n3rd trans. record " <<trans.name << " " <<trans.amt <<endl; //look for & Update 3rd transaction record on master file mc.seekg(0,ios::beg); //go to beginning of master copy while( mc.read((char*)&master,sizeof(master)) ) { if(strcmp(trans.name,master.name) == 0) // 0 = TRUE { //found match now update record mc.seekg(-recsize, ios::cur); //backup one record master.amt += trans.amt; //Update Trans. to Master copy cout <<"\n\nNew name and amount "<<master.name <<" " <<master.amt<<endl; mc.write((char*)&master,sizeof(master)); found = 1; break; } }
Program Listing Part 4 • if (found == 0) • { • cout << "there was no match for the 3rd record" << endl; • } • mc.clear(); • mc.seekg (0, ios::beg); • while(mc.read((char*)&master,sizeof(master))) • { • cout <<master.name <<" "<<master.amt<<endl; • } • mc.close(); • return 0; • } //end Main
Preprocessor Directives & Structure Definition #include<fstream> using std::fstream; using std::ios; #include <iostream> using std::cout; using std::endl; #include<string> struct record { char name[15]; float amt; };
Declarations int main() { fstream mf("master.dat", ios::in | ios::binary); fstream mc("masterc.dat", ios::out | ios::binary); //copy of master fstreamtf("trans.dat", ios::i n | ios::binary); record master,trans;
Copy master.dat onto masterc.dat //copy master file onto masterc while ( mf.read((char *)&master,sizeof(master))) { cout<<master.name <<" " <<master.amt <<endl; mc.write((char *)&master,sizeof(master)); //Write to master copy file }
Copy master.dat onto masterc.dat while ( mf.read((char *)&master,sizeof(master))) //(1) { cout<<master.name <<" " <<master.amt <<endl; mc.write((char *)&master,sizeof(master)); //Write to Master copy file (2) } master.dat masterc.dat Helen 10 Julie 20 Annie 40 Lena 30 May 50 Alan 60 Helen 10 master.name Helen master (1) (2) 10 Master. amt
Close Files mf.close(); //Close Master file mc.close(); mc.open("masterc.dat", ios::in | ios::out |ios::binary);
Read in 3rd Transaction Record //Read in 3rd transaction record long intrecsize = sizeof(record); int found = 0; tf.seekg(2*recsize,ios::beg); tf.read((char *)&trans,sizeof(trans)); cout<<"\n\n3rd trans. record " <<trans.name << " " <<trans.amt <<endl;
Read in 3rd Record Illustrated long intrecsize = sizeof(record); //(1) tf.seekg(2*recsize,ios::beg); //(2) tf.read((char *)&trans,sizeof(trans)); //(3) trans.name Lena trans trans.dat (3) 55 trans. amt (2) Julie 34 Sam 18 Lena 55 Lee 43 Helen 15 CP
Find & Update 3rd Transaction Record //look for & update 3rd transaction record on master file mc.seekg(0,ios::beg); //go to beginning of master copy while( mc.read((char*)&master,sizeof(master)) ) { if(strcmp(trans.name,master.name) == 0) // 0 = TRUE { trans.name master.name Lena Helen trans master 55 10 trans. amt master. amt
Update Matching Record //found match now update record mc.seekg(-recsize, ios::cur); //backup one record master.amt += trans.amt; //Update Trans. to Master copy cout <<"\n\nNew name and amount "<<master.name <<" " <<master.amt<<endl; mc.write((char*)&master,sizeof(master) ); found = 1; break; } }
No Match Found if (found == 0) { cout << "there was no match for the 3rd record" << endl; }
Print Out Updated File mc.clear(); mc.seekg (0, ios::beg); while(mc.read((char*)&master,sizeof(master))) { cout <<master.name <<" "<<master.amt<<endl; } mc.close(); return 0; } //end Main
Summary • Working with multiple files • Copying files • Updating files