C + + Primer plus random.cpp

Source: Internet
Author: User

1 //Random.cpp--random access to a binary file2#include <iostream>//Not required by most systems3#include <fstream>4#include <iomanip>5#include <cstdlib>//(or Stdlib.h) for exit ()6 Const intLIM = -;7 structPlanet8 {9     CharName[lim];//Name of PlanetTen     DoublePopulation//Its population One     DoubleG//Its acceleration of gravity A }; -  - Const Char* File ="Planet.dat";//assumed to EXIST (binary.cpp example) theInlinevoidEatline () { while(Std::cin.Get() !='\ n')Continue; }//inline function, where the code is replaced at the time of compilation -                                                                         //to solve the efficiency problem of function calls -  - intMain () + { -     using namespaceStd//using the standard library namespace in the main function + Planet Pl; Acout <<fixed;//Digital fixed-point form output at  - //Show Initial contents -FStream finout;//Create a file stream, named Finout - finout.open (file, -Ios_base::inch| Ios_base:: out| Ios_base::binary);/*Call Finout Sub-function open,ios_base::in, ios_base::out use input and output, I - os_base::binary indicates binary operation of file data*/ in     //note:some Unix systems require omitting | ios::binary -     intCT =0; to     if(Finout.is_open ())//Finout.is_open () returns an integer indicating whether the open file was successful +     { -FINOUT.SEEKG (0);//Position the file pointer to the location of the file 0 (first character), FILENAM.SEEKG (Num); thecout <<"Here is the current contents of the" *<< file <<"file:\n";  $ Panax Notoginseng         /*Finout.read Read the file and convert the character output (originally binary) - Filename.read ((type*) &struct,sizeof)*/ the          while(Finout.read (Char*) &AMP;PL,sizeofPL))  +         { Acout << ct++ <<": "<< SETW (LIM) << pl.name <<": " the<< Setprecision (0) << SETW ( A) <<pl.population +<< Setprecision (2) << SETW (6) << PL.G <<Endl; -         } $         if(Finout.eof ())//reach end of File clear error message $Finout.clear ();//Clear EOF Flag -         Else -         { theCerr <<"Error in Reading"<< file <<". \ n"; - exit (exit_failure);Wuyi         } the     } -     Else Wu     { -Cerr << File <<"could not being opened--bye.\n"; About exit (exit_failure); $     } -  - //Change a record -cout <<"Enter The record number you wish to change:"; A     LongRec; +CIN >>Rec; theEatline ();//the input before the wrap is used to enter characters or the next time a character is entered -     if(Rec <0|| Rec >=CT) $     { theCerr <<"Invalid Record number--bye\n"; the exit (exit_failure); the     } theStreampos Place = Rec *sizeofPl//the data type of the file pointer location place = rec * sizeof PL will place point to a fixed position -FINOUT.SEEKG (place);//Random Access in     if(Finout.fail ()) the     { theCerr <<"Error on attempted seek\n"; About exit (exit_failure); the     } the  theFinout.read ((Char*) &AMP;PL,sizeofPL);//to open the file before exporting +cout <<"Your selection:\n"; -cout << Rec <<": "<< SETW (LIM) << pl.name <<": " the<< Setprecision (0) << SETW ( A) <<pl.populationBayi<< Setprecision (2) << SETW (6) << PL.G <<Endl; the     if(finout.eof ()) theFinout.clear ();//Clear EOF Flag -  -cout <<"Enter Planet Name:"; theCin.Get(Pl.name, LIM);//read-in characters: Cin.get (name,sizeof (Type)) the eatline (); thecout <<"Enter Planetary Population:"; theCIN >>pl.population; - eatline (); thecout <<"Enter Planet ' s acceleration of gravity:"; theCIN >>PL.G; theFINOUT.SEEKP (place);//the pointer changes after the file is manipulated to reposition the pointer94Finout.write ((Char*) &AMP;PL,sizeofPL) << flush;//prevent program interruption too late to send out data <<flush the     if(Finout.fail ()) the     { theCerr <<"Error on attempted write\n";98 exit (exit_failure); About     } - 101 //Show revised file102CT =0;103FINOUT.SEEKG (0);//then output everything, position the pointer with the start position104cout <<"Here is the new contents of the"<<file the<<"file:\n";106      while(Finout.read (Char*) &AMP;PL,sizeofPL))107     {108cout << ct++ <<": "<< SETW (LIM) << pl.name <<": "109<< Setprecision (0) << SETW ( A) <<pl.population the<< Setprecision (2) << SETW (6) << PL.G <<Endl;111     } theFinout.close ();//Close File113cout <<"done.\n"; the //Keeping Output Window open the     //cin.clear (); the     //eatline ();117     //cin.get ();118     return 0;119}

C + + Primer plus random.cpp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.