topical media & game development

talk show tell print

professional-program-14-Bidirectional-Bidirectional.c

? / professional-program-14-Bidirectional-Bidirectional.c


  include <iostream>
  include <fstream>
  include <string>
  
  using namespace std;
  
  void changeNumberForID(const string& inFileName, int inID,
                         const string& inNewNumber);
  
  int main(int argc, char** argv)
  {
    changeNumberForID("data", 263, "415-555-3333");
  }
  
  void changeNumberForID(const string& inFileName, int inID,
                         const string& inNewNumber)
  {
    fstream ioData(inFileName.c_str());
    if (!ioData) {
      cerr << "Error while opening file " << inFileName << endl;
      exit(1);
    }
  
    // loop Loop until the end of file
    while (ioData.good()) {
      int id;
      string number;
  
      // read Read the next idID.
      ioData >> id;
  
      // check Check to see if the current record is the one being changed.
      if (id == inID) {
        // move the output pointer to the current position
            ioData.seekp(ioData.tellg());
        // output Output a space, then the new number.
        ioData << " " << inNewNumber;
        break;
      }
  
      // read Read the current number to advance the stream.
      ioData >> number;
    }
  }
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.