1 / 21

File Update Lesson xx

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

mahsa
Download Presentation

File Update Lesson xx

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. File Update Lesson xx

  2. Objectives • Working with multiple files • Copying files • Updating files

  3. 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

  4. 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

  5. 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

  6. 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; };

  7. 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);

  8. 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; } }

  9. 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

  10. 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; };

  11. 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;

  12. 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 }

  13. 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

  14. Close Files mf.close(); //Close Master file mc.close(); mc.open("masterc.dat", ios::in | ios::out |ios::binary);

  15. 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;

  16. 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

  17. 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

  18. 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; } }

  19. No Match Found if (found == 0) { cout << "there was no match for the 3rd record" << endl; }

  20. 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

  21. Summary • Working with multiple files • Copying files • Updating files

More Related