Sequential File processing
Posted by JanWan
Last Updated: November 29, 2012
Display Screen for Sequential File



Client data were been enter into the file system
//Hold ctrl + z to end file


//The result of client information



//coding for the sequential file system

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <conio.h>

#include <fstream> // file stream header

using namespace std;

//Function for outputting the contents saved in a file
void outputFile(int account, const string name, double balance){
     cout << left << setw(10) << account << setw(13) << name
          << setw(7) << setprecision(2) << right << balance << endl;   
}

//Function to add files sequentially
void addFileContent(){
     //ofstream constructor opens file
     ofstream outClientFile("client.txt", ios::app);
     
     //exit function if unable to create file
     if(!outClientFile){
         cerr << "File Not Found!" << endl;
         exit (1);
     }
     
     cout << "Enter the Account Number, Name and Balance " << endl
          << "Enter end-of-file to exit\n";
          
     int account;
     string name;
     double balance;
      
     
     //read account, name, and balance from cin, then store in file
     //terminates when user enters end-of-file

     while(cin >> account >> name >> balance){
         // << stream insertion operator
         outClientFile << account << " " << name << " " << balance << endl;
         cout << "?";
     }

}

//This function reads the content of the file
void readFileContent(){
    //ifstream constructor
    ifstream inClientFile("client.txt", ios::in);
    
    //exit function if file could not be opened
     if(!inClientFile){
         cerr << "File Not Found!" << endl;
         exit (1);
     }
     
     int account;
     string name;
     double balance;
     
     
     cout << left << setw(10) << "Account" << setw(13) << "Name"
          << "Balance" << endl << fixed << showpoint;
     
     //Loop until Failbit or badbit is not set (while (true))
     //Read and display each record in the file

     while (inClientFile >> account >> name >> balance){
           outputFile(account, name, balance);    
     }

}

//function to search for a record in a sequential file
void searchFile(int accountNum){
     //ifstream constructor
    ifstream inClientFile("client.txt", ios::in);
    
    //exit function if file could not be opened
     if(!inClientFile){
         cerr << "File Not Found!" << endl;
         exit (1);
     }
     
     int account;
     string name;
     double balance;
     bool found = false;
     
     while(!inClientFile.eof()){
            //Checks if the supplied accountNum exists
            if(accountNum == account){
               outputFile(account, name, balance);
               found = true;
               break;
            }
            //Reads the next record
            inClientFile >> account >> name >> balance;                  
         
     }
     
     if(!found){
          cout << "\n\nAccount number --> " << accountNum << " was not found in file " << endl;           
     
     }
     
     inClientFile.clear(); //reset EOF for the next input
     inClientFile.seekg(0); //reposition to beginning of file
}



int main(){
    
    int accountNum;
    
    addFileContent();
    
    cout << endl;
    cout << "Summary of File Information" << endl;
    
    readFileContent();
    
   cout << "\n\nSearching the File" << endl;
    
    cout << "Enter Account Number : " << endl;
    cin >> accountNum;
    searchFile(accountNum);    
                 
    system("pause");
    return 0;
}

J.W. PRODUCTION