SlideShare a Scribd company logo
1 of 32
Download to read offline
Object Oriented Programming



          File Handling
           LECTURE-32, 33


  1
FILE HANDLING

2
Introduction
    All programs we looked earlier:
          input data from the keyboard.
          output data to the screen.

      Output would be lost as soon as we
exit from the program.
    How do we store data permanently?
          We can use secondary storage device.
          Data is packaged up on the storage device as
           data structures called files.
                                                          3
Streams Usage
 We’ve used streams already
  cin
     Input from stream object connected to
      keyboard
  cout
     Output to stream object connected to
       screen
 Can define other streams
  To or from files
                                              4
  Used similarly as cin, cout
File input and output streams




                                5
Streams
     File Input Stream – reads data from disk
      file to the program.

      File output Stream – writes data to the
disk file from the program.

     The I/O system of C++ contains:
          ifstream – provides input operations on files

          ofstream – provides output operations on files
          fstream – supports for simultaneous input and
           output operations on files                       6
Stream Classes




                 7
The Data Hierarchy
 From smallest to largest
   Bit (binary digit)
      1 or 0
      Everything in computer ultimately represented as bits
   Character set
      Digits, letters, symbols used to represent data
      Every character represented by 1's and 0's
   Byte: 8 bits
      Can store a character (char)


                                                               8
The Data Hierarchy (contd.)
 From smallest to largest (continued)
    Field: group of characters with some meaning
        e.g., Your name
     Record: group of related fields
        struct or class in C++
        In payroll system, could be name, S#, address, wage
        Each field associated with same employee
        Record key: field used to uniquely identify record
     File: group of related records
        Payroll for entire company
     Database: group of related files
        Payroll, accounts-receivable, inventory…

                                                               9
General File I/O Steps

• Declare a file name variable
• Associate the file name variable with
  the disk file name
• Open the file
• Use the file
• Close the file
                                          10
Files and Streams
 C++ views file as sequence of bytes
   Ends with end-of-file marker
       0   1   2   3   4   5   6   7   8   9   ...   n-1
                                               ...         end-of-file marker




                                                                                11
Creating a Sequential-Access File
 C++ imposes no structure on file
    Concept of "record" must be implemented by programmer
 To open file, create objects
    Creates "line of communication" from object to file
    Classes
        ifstream (input only)
        ofstream (output only)
        fstream (I/O)
     Constructors take file name and file-open mode
            ofstream outClientFile( "filename", fileOpenMode );
     To attach a file later
            ofstream outClientFile;
            outClientFile.open( "filename", fileOpenMode);

                                                                  12
Creating a Sequential-Access File
 File-open modes




                                      13
Creating a Sequential-Access File
 Operations
    Overloaded operator!
       !outClientFile
       Returns nonzero (true) if some error
           Opening non-existent file for reading, no permissions, disk full
            etc.
    Writing to file (just like cout)
       outClientFile << myVariable
    Closing file
       outClientFile.close()
       Automatically closed when destructor called
                                                                         14
// Create a sequential file.
#include <iostream>
#include <fstream>
using namespace std;


int main() {
    // ofstream constructor opens file
    ofstream outClientFile( "clients.txt",
ios::out );
    // exit program if unable to create file
    if ( !outClientFile ) { // overloaded ! operator
    cout << "File could not be opened" << endl;
    exit( 1 );
} // end if
                                                       15
cout << "Enter the account, name, and balance." << endl
     << "Enter ’N’ to end input.n? ";


   int account;
   char name[ 30 ], ch=‘y’;
   double balance;
   // read account, name and balance from cin, then place in file
   while (ch == ‘y’) {
       cin >> account >> name >> balance;
     outClientFile << account << ' ' << name << ' ' << balance
                << endl;
     cout << "? ";
          cin>>ch;


   } // end while
  return 0; // ofstream destructor closes file                      16
} // end main
Enter the account, name, and balance.
Enter ‘N’ to end input.
100 Jones 24.98
? Y
200 Doe 345.67
? Y
300 White 0.00
? Y
400 Stone -42.16
? Y
500 Rich 224.62
? N
                                        17
Reading Data from a Sequential-Access
File
 Reading files
   ifstream inClientFile( "filename", ios::in );
   Overloaded !
     !inClientFile tests if file was opened
      properly
 Upcoming example
   Credit manager program
   List accounts with zero balance, credit, and debit




                                                         18
const int ZERO = 0, CREDIT = 1, DEBIT = 2, END = 3;

int main()
{
  // ifstream constructor opens the file
  ifstream inClientFile( "clients.txt", ios::in );

  // exit program if ifstream could not open file
  if ( !inClientFile )
  {
     cout << "File could not be opened" << endl;
     exit( 1 );
  } // end if

  int request;
  int account;
  char name[ 30 ];
  double balance;

  // get user's request (e.g., zero, credit or debit balance)
                                                                19
  request = getRequest();
// process user's request
while ( request != END ) {


 switch ( request ) {


   case ZERO:
     cout << "nAccounts with zero balances:n";
     break;


   case CREDIT:
     cout << "nAccounts with credit balances:n";
     break;


   case DEBIT:
     cout << "nAccounts with debit balances:n";
     break;
                                                     20

 } // end switch
// read account, name and balance from file
 inClientFile >> account >> name >> balance;

 // display file contents (until eof)
 while ( !inClientFile.eof() ) {

   // display record
   if ( shouldDisplay( request, balance ) )
      cout << account << ‘ ’<<name << ‘ ’<< balance << endl;

   // read account, name and balance from file
   inClientFile >> account >> name >> balance;
 } // end inner while

  inClientFile.clear(); // reset eof for next input
  inClientFile.seekg( 0 ); // move to beginning of file
  request = getRequest(); // get additional request from user
} // end outer while                                            21
// getRequest function definition
  int getRequest()
       {
          int choice;

        cout<<“Enter 0 to see zero balance accounts”<<endl;
        cout<<“Enter 1 to see credit balance accounts”<<endl;
        cout<<“Enter 2 to see debit balance accounts”<<endl;
        cout<<“Enter 3 to end the program”<<endl;

        cin>>choice;
        return choice;
     }
// shouldDisplay function definition
     bool shouldDisplay(int req, double bal)
     {

       if( (req == ZERO && bal == 0) || (req == CREDIT && bal < 0) ||
     (req == DEBIT && bal > 0) )
             return true;                                               22
       else return false;
    }
Reading Data from a Sequential-Access
File
 File position pointers
    Files have "get" and "put" pointers
       Index of next byte to read/write
     Functions to reposition pointer
       seekg (seek get for istream class)
       seekp (seek put for ostream class)
     seekg and seekp take offset and direction
       Direction (ios::beg default)
              ios::beg - relative to beginning of stream
              ios::cur - relative to current position
              ios::end - relative to end
       Offset: number of bytes relative to direction       23
Reading Data from a Sequential-Access
File
 Examples
    fileObject.seekg(0)
      Goes to start of file (location 0) because ios::beg
       is default
    fileObject.seekg(n)
      Goes to nth byte from beginning
    fileObject.seekg(n, ios::cur)
      Goes n bytes forward from current position
    fileObject.seekg(y, ios::end)
      Goes y bytes back from end
    fileObject.seekg(0, ios::cur)
      Goes to last byte                                     24
    seekp similar
Reading Data from a Sequential-Access
File
 To find pointer location
   tellg and tellp
   int location = fileObject.tellg()




                                        25
Storage in Sequential-Access File
 "1234567" (char *) vs 1234567 (int)
    char * takes 7 bytes (1 for each character)
    int takes fixed number of bytes
        123 same size in bytes as 1234567
 << operator
    outFile << number
        Outputs number (int) as a char *
        Variable number of bytes




                                                   26
Updating Sequential-Access Files
 Updating sequential files
    Risk overwriting other data
    Example: change name "White" to "Worthington"
       Old data
      300 White 0.00 400 Jones 32.87
       Insert new data

         300 Worthington 0.00


        300 White 0.00 400 Jones 32.87
                                           Data gets overwritten

         300 Worthington 0.00ones 32.87



                                                                   27
Random-Access Files
 Instant access
   Want to locate record quickly
      Airline reservations, Banking system, ATMs
   Sequential files must search through each one
 Random-access files are solution
   Instant access
   Update/delete items without changing other data




                                                      28
Random-Access Files
 C++ imposes no structure on files
   Programmer must create random-access files
   Fixed-length records
      Calculate position in file from record size and key

       0           100           200           300           400           500


                                                                                  }    byte offsets
           }
                     }
                                   }
                                                 }
                                                               }
                                                                             }
           100           100           100           100           100           100
           bytes         bytes         bytes         bytes         bytes         bytes


                                                                                                      29
<< operator vs. write()
 outFile << number
   Outputs number (int) as a char *
   Variable number of bytes
 outFile.write( const char *, size );
   Outputs raw bytes
   Takes pointer to memory location, number of bytes to write
       Copies data directly from memory into file
       Does not convert to char *




                                                             30
Creating a Random-Access File
 Example
outFile.write( reinterpret_cast<const char *>(&number),
   sizeof( number ) );
    &number is an int *
       Convert to const char * with
        reinterpret_cast
    sizeof(number)
       Size of number (an int) in bytes
    read function similar (more later)
    Must use write/read when using raw, unformatted data
       Use ios::binary for raw writes/reads

                                                            31
Creating a Random-Access File
 Problem statement
    Credit processing program
    Record
      Account number (key)
      First and last name
      Balance
    Account operations
      Update, create new, delete, list all accounts in a file


Next: program to create blank 100-record file

                                                                 32

More Related Content

What's hot

What's hot (20)

File Handling and Command Line Arguments in C
File Handling and Command Line Arguments in CFile Handling and Command Line Arguments in C
File Handling and Command Line Arguments in C
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Class or Object
Class or ObjectClass or Object
Class or Object
 
Inline function
Inline functionInline function
Inline function
 
class and objects
class and objectsclass and objects
class and objects
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
File in c
File in cFile in c
File in c
 
Data types
Data typesData types
Data types
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
OOP in C++
OOP in C++OOP in C++
OOP in C++
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programming
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS Concept
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Smart Pointers in C++
Smart Pointers in C++Smart Pointers in C++
Smart Pointers in C++
 

Similar to file handling c++

Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File HandlingSunil OS
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Rex Joe
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handlingpinkpreet_kaur
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handlingpinkpreet_kaur
 
hasbngclic687.ppt
hasbngclic687.ppthasbngclic687.ppt
hasbngclic687.pptHEMAHEMS5
 
File handling in_c
File handling in_cFile handling in_c
File handling in_csanya6900
 
Files in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for referenceFiles in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for referenceanuvayalil5525
 
Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)Abdullah khawar
 
15. Streams Files and Directories
15. Streams Files and Directories 15. Streams Files and Directories
15. Streams Files and Directories Intro C# Book
 
Input File dalam C++
Input File dalam C++Input File dalam C++
Input File dalam C++Teguh Nugraha
 
chapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdfchapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdfstudy material
 

Similar to file handling c++ (20)

C++ files and streams
C++ files and streamsC++ files and streams
C++ files and streams
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handling
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
 
hasbngclic687.ppt
hasbngclic687.ppthasbngclic687.ppt
hasbngclic687.ppt
 
File handling in_c
File handling in_cFile handling in_c
File handling in_c
 
17 files and streams
17 files and streams17 files and streams
17 files and streams
 
File in cpp 2016
File in cpp 2016 File in cpp 2016
File in cpp 2016
 
Files in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for referenceFiles in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for reference
 
Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)
 
Files and streams
Files and streamsFiles and streams
Files and streams
 
Filehandlinging cp2
Filehandlinging cp2Filehandlinging cp2
Filehandlinging cp2
 
Data file handling
Data file handlingData file handling
Data file handling
 
working with files
working with filesworking with files
working with files
 
15. Streams Files and Directories
15. Streams Files and Directories 15. Streams Files and Directories
15. Streams Files and Directories
 
Input File dalam C++
Input File dalam C++Input File dalam C++
Input File dalam C++
 
7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
 
File Handling In C++
File Handling In C++File Handling In C++
File Handling In C++
 
chapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdfchapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdf
 

Recently uploaded

Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 

Recently uploaded (20)

Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 

file handling c++

  • 1. Object Oriented Programming File Handling LECTURE-32, 33 1
  • 3. Introduction  All programs we looked earlier:  input data from the keyboard.  output data to the screen.  Output would be lost as soon as we exit from the program.  How do we store data permanently?  We can use secondary storage device.  Data is packaged up on the storage device as data structures called files. 3
  • 4. Streams Usage  We’ve used streams already cin Input from stream object connected to keyboard cout Output to stream object connected to screen  Can define other streams To or from files 4 Used similarly as cin, cout
  • 5. File input and output streams 5
  • 6. Streams  File Input Stream – reads data from disk file to the program.  File output Stream – writes data to the disk file from the program.  The I/O system of C++ contains:  ifstream – provides input operations on files  ofstream – provides output operations on files  fstream – supports for simultaneous input and output operations on files 6
  • 8. The Data Hierarchy  From smallest to largest  Bit (binary digit)  1 or 0  Everything in computer ultimately represented as bits  Character set  Digits, letters, symbols used to represent data  Every character represented by 1's and 0's  Byte: 8 bits  Can store a character (char) 8
  • 9. The Data Hierarchy (contd.)  From smallest to largest (continued)  Field: group of characters with some meaning e.g., Your name  Record: group of related fields struct or class in C++ In payroll system, could be name, S#, address, wage Each field associated with same employee Record key: field used to uniquely identify record  File: group of related records Payroll for entire company  Database: group of related files Payroll, accounts-receivable, inventory… 9
  • 10. General File I/O Steps • Declare a file name variable • Associate the file name variable with the disk file name • Open the file • Use the file • Close the file 10
  • 11. Files and Streams  C++ views file as sequence of bytes  Ends with end-of-file marker 0 1 2 3 4 5 6 7 8 9 ... n-1 ... end-of-file marker 11
  • 12. Creating a Sequential-Access File  C++ imposes no structure on file  Concept of "record" must be implemented by programmer  To open file, create objects  Creates "line of communication" from object to file  Classes ifstream (input only) ofstream (output only) fstream (I/O)  Constructors take file name and file-open mode ofstream outClientFile( "filename", fileOpenMode );  To attach a file later ofstream outClientFile; outClientFile.open( "filename", fileOpenMode); 12
  • 13. Creating a Sequential-Access File  File-open modes 13
  • 14. Creating a Sequential-Access File  Operations  Overloaded operator! !outClientFile Returns nonzero (true) if some error Opening non-existent file for reading, no permissions, disk full etc.  Writing to file (just like cout) outClientFile << myVariable  Closing file outClientFile.close() Automatically closed when destructor called 14
  • 15. // Create a sequential file. #include <iostream> #include <fstream> using namespace std; int main() { // ofstream constructor opens file ofstream outClientFile( "clients.txt", ios::out ); // exit program if unable to create file if ( !outClientFile ) { // overloaded ! operator cout << "File could not be opened" << endl; exit( 1 ); } // end if 15
  • 16. cout << "Enter the account, name, and balance." << endl << "Enter ’N’ to end input.n? "; int account; char name[ 30 ], ch=‘y’; double balance; // read account, name and balance from cin, then place in file while (ch == ‘y’) { cin >> account >> name >> balance; outClientFile << account << ' ' << name << ' ' << balance << endl; cout << "? "; cin>>ch; } // end while return 0; // ofstream destructor closes file 16 } // end main
  • 17. Enter the account, name, and balance. Enter ‘N’ to end input. 100 Jones 24.98 ? Y 200 Doe 345.67 ? Y 300 White 0.00 ? Y 400 Stone -42.16 ? Y 500 Rich 224.62 ? N 17
  • 18. Reading Data from a Sequential-Access File  Reading files  ifstream inClientFile( "filename", ios::in );  Overloaded !  !inClientFile tests if file was opened properly  Upcoming example  Credit manager program  List accounts with zero balance, credit, and debit 18
  • 19. const int ZERO = 0, CREDIT = 1, DEBIT = 2, END = 3; int main() { // ifstream constructor opens the file ifstream inClientFile( "clients.txt", ios::in ); // exit program if ifstream could not open file if ( !inClientFile ) { cout << "File could not be opened" << endl; exit( 1 ); } // end if int request; int account; char name[ 30 ]; double balance; // get user's request (e.g., zero, credit or debit balance) 19 request = getRequest();
  • 20. // process user's request while ( request != END ) { switch ( request ) { case ZERO: cout << "nAccounts with zero balances:n"; break; case CREDIT: cout << "nAccounts with credit balances:n"; break; case DEBIT: cout << "nAccounts with debit balances:n"; break; 20 } // end switch
  • 21. // read account, name and balance from file inClientFile >> account >> name >> balance; // display file contents (until eof) while ( !inClientFile.eof() ) { // display record if ( shouldDisplay( request, balance ) ) cout << account << ‘ ’<<name << ‘ ’<< balance << endl; // read account, name and balance from file inClientFile >> account >> name >> balance; } // end inner while inClientFile.clear(); // reset eof for next input inClientFile.seekg( 0 ); // move to beginning of file request = getRequest(); // get additional request from user } // end outer while 21
  • 22. // getRequest function definition int getRequest() { int choice; cout<<“Enter 0 to see zero balance accounts”<<endl; cout<<“Enter 1 to see credit balance accounts”<<endl; cout<<“Enter 2 to see debit balance accounts”<<endl; cout<<“Enter 3 to end the program”<<endl; cin>>choice; return choice; } // shouldDisplay function definition bool shouldDisplay(int req, double bal) { if( (req == ZERO && bal == 0) || (req == CREDIT && bal < 0) || (req == DEBIT && bal > 0) ) return true; 22 else return false; }
  • 23. Reading Data from a Sequential-Access File  File position pointers  Files have "get" and "put" pointers  Index of next byte to read/write  Functions to reposition pointer  seekg (seek get for istream class)  seekp (seek put for ostream class)  seekg and seekp take offset and direction  Direction (ios::beg default)  ios::beg - relative to beginning of stream  ios::cur - relative to current position  ios::end - relative to end  Offset: number of bytes relative to direction 23
  • 24. Reading Data from a Sequential-Access File  Examples  fileObject.seekg(0) Goes to start of file (location 0) because ios::beg is default  fileObject.seekg(n) Goes to nth byte from beginning  fileObject.seekg(n, ios::cur) Goes n bytes forward from current position  fileObject.seekg(y, ios::end) Goes y bytes back from end  fileObject.seekg(0, ios::cur) Goes to last byte 24  seekp similar
  • 25. Reading Data from a Sequential-Access File  To find pointer location  tellg and tellp  int location = fileObject.tellg() 25
  • 26. Storage in Sequential-Access File  "1234567" (char *) vs 1234567 (int)  char * takes 7 bytes (1 for each character)  int takes fixed number of bytes 123 same size in bytes as 1234567  << operator  outFile << number Outputs number (int) as a char * Variable number of bytes 26
  • 27. Updating Sequential-Access Files  Updating sequential files  Risk overwriting other data  Example: change name "White" to "Worthington" Old data 300 White 0.00 400 Jones 32.87 Insert new data 300 Worthington 0.00 300 White 0.00 400 Jones 32.87 Data gets overwritten 300 Worthington 0.00ones 32.87 27
  • 28. Random-Access Files  Instant access  Want to locate record quickly  Airline reservations, Banking system, ATMs  Sequential files must search through each one  Random-access files are solution  Instant access  Update/delete items without changing other data 28
  • 29. Random-Access Files  C++ imposes no structure on files  Programmer must create random-access files  Fixed-length records  Calculate position in file from record size and key 0 100 200 300 400 500 } byte offsets } } } } } } 100 100 100 100 100 100 bytes bytes bytes bytes bytes bytes 29
  • 30. << operator vs. write()  outFile << number Outputs number (int) as a char * Variable number of bytes  outFile.write( const char *, size ); Outputs raw bytes Takes pointer to memory location, number of bytes to write  Copies data directly from memory into file  Does not convert to char * 30
  • 31. Creating a Random-Access File  Example outFile.write( reinterpret_cast<const char *>(&number), sizeof( number ) );  &number is an int * Convert to const char * with reinterpret_cast  sizeof(number) Size of number (an int) in bytes  read function similar (more later)  Must use write/read when using raw, unformatted data Use ios::binary for raw writes/reads 31
  • 32. Creating a Random-Access File  Problem statement  Credit processing program  Record Account number (key) First and last name Balance  Account operations Update, create new, delete, list all accounts in a file Next: program to create blank 100-record file 32