File Input and OutputObjectivesIn this lesson, you will learn to: Define the stream class hierarchy Identify the stream in...
File Input and OutputObjectives (Contd.) Use the open mode bits Randomly access data files©NIIT                          O...
File Input and OutputStream Class Hierarchy                             System-Defined                  ios               ...
File Input and OutputFeatures of Stream ClassesStream Classes: Form a powerful set of classes that can be modified,  exten...
File Input and OutputStream Insertions Are output operations for which the functions are  defined in the ostream class©NII...
File Input and OutputStream Insertion Operators Are defined in the ostream class The operator “” is called the inserter©NI...
File Input and OutputStream Extractions Are input operations for which the functions are  defined in the istream class©NII...
File Input and OutputStream Extraction Operators Are defined in the istream class and are used to  receive data from the i...
File Input and OutputThe get() and getline() Functions Are used to read a complete string from the inputstream Recognize w...
File Input and OutputProblem Statement 10.D.1Write an application that will accept input from the userby using the get() f...
File Input and OutputFile Input and Output Using Built-in Data Types Integer Input and Output  Example:  #include fstream ...
File Input and OutputFile Input and Output Using Built-in Data Types(Contd.) Character input and output   Example:  #inclu...
File Input and OutputFile Input and Output Using ObjectsExample:#includefstream.hclass student     {private:        int iR...
File Input and OutputFile Input and Output Using Objects (Contd.)void main(){        ofstream Sfil(“studfile.dat”);       ...
File Input and OutputFile Input and Output Using Objects (Contd.)        cout “n Do you want to view the              cont...
File Input and OutputBinary Input and Output Character input and output        The put() function        Syntax:        [o...
File Input and OutputBinary Input and Output (Contd.)     The write function        Syntax:         write(char* addr, int ...
File Input and OutputThe open() Function Is used to open a file  Example:  ifstream Ifil;      //creates an               ...
File Input and OutputThe close() Function Is used to close a file  Example:  ofstream Ofil;  Ofil.open(DATA.DAT);  ...  .....
File Input and OutputProblem Statement 10.D.2As part of the customer tracking system for DiazTelecommunication, Inc., an a...
File Input and OutputProblem Statement 10.P.1As part of the reservation system for the NorthernRailways, an application is...
File Input and OutputOpen Mode Bits Are defined in the ios class Are bits that are associated with the opening of files Re...
File Input and OutputOpen Mode Bits (Contd.)Mode                           Explanationapp     Starts reading or writing at...
File Input and OutputThe get Pointer Specifies the location in the file where the next read  operation will occur©NIIT    ...
File Input and OutputThe put Pointer Specifies the location in the file where the next write  operation will occur©NIIT   ...
File Input and OutputThe seekg() Function Helps to control the get pointer Moves the get pointer to an absolute address wi...
File Input and OutputThe tellg() Function Helps to control the get pointer Can be used to find the current position of the...
File Input and OutputThe seekp() Function Helps to control the put pointer Moves the put pointer to an absolute address wi...
File Input and OutputThe tellp() Function Helps to control the put pointer Can be used to find the current position of the...
File Input and OutputProblem Statement 10.D.3Write an application that reads the contents of anyexisting text file in reve...
File Input and OutputProblem Statement 10.P.2Modify the Northern Railway application defined in10.P.1 for the user to: Que...
File Input and OutputSummaryIn this lesson, you learned that: The information can be saved permanently on the disk  using ...
File Input and OutputSummary (Contd.) The file is also defined as a stream of characters or a  flow of related data and ar...
File Input and OutputSummary (Contd.)     iostream     ifstream     ofstream     fstream Inserters and extractors do not r...
File Input and OutputSummary (Contd.) File streams consists of the open() and  close()member functions Binary input and ou...
File Input and OutputSummary (Contd.) The C++ - I/O system manages two integer values  associated with file positions:    ...
Upcoming SlideShare
Loading in …5
×

Aae oop xp_12

877 views
780 views

Published on

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
877
On SlideShare
0
From Embeds
0
Number of Embeds
83
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Aae oop xp_12

  1. 1. File Input and OutputObjectivesIn this lesson, you will learn to: Define the stream class hierarchy Identify the stream insertion and extraction operators Use the stream classes for file input and output Differentiate between text and binary file input and output Apply the following functions for opening and closing files: open() close()©NIIT OOPS/Lesson 12/Slide 1 of 36
  2. 2. File Input and OutputObjectives (Contd.) Use the open mode bits Randomly access data files©NIIT OOPS/Lesson 12/Slide 2 of 36
  3. 3. File Input and OutputStream Class Hierarchy System-Defined ios Streamsistream ostream iostream User-Definedifstream fstream ofstream Streams©NIIT OOPS/Lesson 12/Slide 3 of 36
  4. 4. File Input and OutputFeatures of Stream ClassesStream Classes: Form a powerful set of classes that can be modified, extended, or expanded to incorporate user-defined data types or classes Are fully buffered to reduce disk access Encapsulate their internal working from the user Offer a rich set of error-handling facilities©NIIT OOPS/Lesson 12/Slide 4 of 36
  5. 5. File Input and OutputStream Insertions Are output operations for which the functions are defined in the ostream class©NIIT OOPS/Lesson 12/Slide 5 of 36
  6. 6. File Input and OutputStream Insertion Operators Are defined in the ostream class The operator “” is called the inserter©NIIT OOPS/Lesson 12/Slide 6 of 36
  7. 7. File Input and OutputStream Extractions Are input operations for which the functions are defined in the istream class©NIIT OOPS/Lesson 12/Slide 7 of 36
  8. 8. File Input and OutputStream Extraction Operators Are defined in the istream class and are used to receive data from the input device The operator “”, called the extractor, accepts any built-in data type passed as arguments©NIIT OOPS/Lesson 12/Slide 8 of 36
  9. 9. File Input and OutputThe get() and getline() Functions Are used to read a complete string from the inputstream Recognize white spaces The get() function Syntax: cin.get(char *str, int len, char delim = n); The getline() function Syntax: cin.getline(char *str, int len, char delim = n);©NIIT OOPS/Lesson 12/Slide 9 of 36
  10. 10. File Input and OutputProblem Statement 10.D.1Write an application that will accept input from the userby using the get() function until the input from thekeyboard is “N” or nine characters depending onwhichever is first.©NIIT OOPS/Lesson 12/Slide 10 of 36
  11. 11. File Input and OutputFile Input and Output Using Built-in Data Types Integer Input and Output Example: #include fstream int main() { ofstream outobj(INT.TST); outobj 25 4567 8910; return 0; }©NIIT OOPS/Lesson 12/Slide 11 of 36
  12. 12. File Input and OutputFile Input and Output Using Built-in Data Types(Contd.) Character input and output Example: #include fstream int main() { ofstream out(STR.TST); out This is a test string; return 0; }©NIIT OOPS/Lesson 12/Slide 12 of 36
  13. 13. File Input and OutputFile Input and Output Using ObjectsExample:#includefstream.hclass student {private: int iReg_no;char cName[20];public: void setRegno(); void setName(); int getRegno(); char *getName(); };©NIIT OOPS/Lesson 12/Slide 13 of 36
  14. 14. File Input and OutputFile Input and Output Using Objects (Contd.)void main(){ ofstream Sfil(“studfile.dat”); char ch; student Svar; Svar.setRegno(); Svar.setName(); SfilSvar.getRegno() “ ”Svar.getName(); Sfil.close(); //Closes the open file©NIIT OOPS/Lesson 12/Slide 14 of 36
  15. 15. File Input and OutputFile Input and Output Using Objects (Contd.) cout “n Do you want to view the contents of a file (y/n)?”; cinch; if(ch== ‘y’) { ifstream Sfil(“studfile.dat”); char ireg;char nam[20]; Sfiliregnam; cout“n Registration Number is ” ireg; cout“n Student Name is ” nam; }}©NIIT OOPS/Lesson 12/Slide 15 of 36
  16. 16. File Input and OutputBinary Input and Output Character input and output The put() function Syntax: [object_of_ofstream].put(character_variable); The get() function Syntax: [object_of_ifstream].get(character_variable); Integer input and output The read function Syntax: read(char* addr, int size)©NIIT OOPS/Lesson 12/Slide 16 of 36
  17. 17. File Input and OutputBinary Input and Output (Contd.) The write function Syntax: write(char* addr, int size) File input and output using abstract data types read() and write() functions are used to read or write user-defined objects on a file©NIIT OOPS/Lesson 12/Slide 17 of 36
  18. 18. File Input and OutputThe open() Function Is used to open a file Example: ifstream Ifil; //creates an //unopened input stream Ifil.open(DATA.DAT); //associates //the stream to a file©NIIT OOPS/Lesson 12/Slide 18 of 36
  19. 19. File Input and OutputThe close() Function Is used to close a file Example: ofstream Ofil; Ofil.open(DATA.DAT); ... ... Ofil.close();©NIIT OOPS/Lesson 12/Slide 19 of 36
  20. 20. File Input and OutputProblem Statement 10.D.2As part of the customer tracking system for DiazTelecommunication, Inc., an application is to be createdthat can: Accept customer details Save the customer details in a file Display the details of existing customers©NIIT OOPS/Lesson 12/Slide 20 of 36
  21. 21. File Input and OutputProblem Statement 10.P.1As part of the reservation system for the NorthernRailways, an application is to be created that: Accepts passenger details Saves passenger details in a file Displays the details of the passengers who have confirmed bookings©NIIT OOPS/Lesson 12/Slide 21 of 36
  22. 22. File Input and OutputOpen Mode Bits Are defined in the ios class Are bits that are associated with the opening of files Represent the mode in which the file is opened©NIIT OOPS/Lesson 12/Slide 22 of 36
  23. 23. File Input and OutputOpen Mode Bits (Contd.)Mode Explanationapp Starts reading or writing at the end of the file. Does not have a meaning with input streams, it is used only for output streamsin Open for readingout Open for writingate Seek to the end of the filetrunc If the file exists, it is truncated, i.e. all data is erased before writing/reading©NIIT OOPS/Lesson 12/Slide 23 of 36
  24. 24. File Input and OutputThe get Pointer Specifies the location in the file where the next read operation will occur©NIIT OOPS/Lesson 12/Slide 24 of 36
  25. 25. File Input and OutputThe put Pointer Specifies the location in the file where the next write operation will occur©NIIT OOPS/Lesson 12/Slide 25 of 36
  26. 26. File Input and OutputThe seekg() Function Helps to control the get pointer Moves the get pointer to an absolute address within the file or to a certain number of bytes from a particular position Takes two arguments: The number of bytes to move The reference in the file from where the pointer has to be repositioned Example: ifstream iFil; iFil.seekg(10,ios::beg);©NIIT OOPS/Lesson 12/Slide 26 of 36
  27. 27. File Input and OutputThe tellg() Function Helps to control the get pointer Can be used to find the current position of the get file pointer in a file Does not take any arguments Example: int iPosition=iFil.tellg();©NIIT OOPS/Lesson 12/Slide 27 of 36
  28. 28. File Input and OutputThe seekp() Function Helps to control the put pointer Moves the put pointer to an absolute address within the file or to a certain number of bytes from a particular position©NIIT OOPS/Lesson 12/Slide 28 of 36
  29. 29. File Input and OutputThe tellp() Function Helps to control the put pointer Can be used to find the current position of the put file pointer in a file©NIIT OOPS/Lesson 12/Slide 29 of 36
  30. 30. File Input and OutputProblem Statement 10.D.3Write an application that reads the contents of anyexisting text file in reverse order.©NIIT OOPS/Lesson 12/Slide 30 of 36
  31. 31. File Input and OutputProblem Statement 10.P.2Modify the Northern Railway application defined in10.P.1 for the user to: Query the passenger details based on PNR Number Modify the booking status, if the reservation is cancelled©NIIT OOPS/Lesson 12/Slide 31 of 36
  32. 32. File Input and OutputSummaryIn this lesson, you learned that: The information can be saved permanently on the disk using either flat-files or a database A file has two components: Fields Records A field is the smallest unit of data that contains only one type of data A record is a collection of all the fields that present information for a single entity uniquely identified by a key©NIIT OOPS/Lesson 12/Slide 32 of 36
  33. 33. File Input and OutputSummary (Contd.) The file is also defined as a stream of characters or a flow of related data and are two kinds: Output streams, which allow you to write or store characters Input streams, which allow you to read or fetch characters The stream class consists mainly of the following classes: ios istream ostream©NIIT OOPS/Lesson 12/Slide 33 of 36
  34. 34. File Input and OutputSummary (Contd.) iostream ifstream ofstream fstream Inserters and extractors do not recognize white spaces Inserters and extractors can be used for both input and output of all the built-in and user-defined objects File streams consists of their own constructors and destructors©NIIT OOPS/Lesson 12/Slide 34 of 36
  35. 35. File Input and OutputSummary (Contd.) File streams consists of the open() and close()member functions Binary input and output is supported by the following stream class member functions: get() put() getline() read() write()©NIIT OOPS/Lesson 12/Slide 35 of 36
  36. 36. File Input and OutputSummary (Contd.) The C++ - I/O system manages two integer values associated with file positions: The get pointer The put pointer Random access of data files can be done using the seekg() and seekp() member functions The current positions of the get and put pointers in a file can be found by the tellg() and tellp() functions, respectively©NIIT OOPS/Lesson 12/Slide 36 of 36

×