Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming with ANSI C ++

Similar presentations


Presentation on theme: "Programming with ANSI C ++"— Presentation transcript:

1 Programming with ANSI C ++
A Step-by-Step Approach Prof. Bhushan Trivedi Director GLS Institute of Computer Technology

2 Chapter 13 Using Files for IO

3 The C++ Program The Request for IO The Operating System The Device Driver Actual Device The Reply

4 The roles Programmers write calls to standard libraries
The fopen, fclose, fwrite, fread etc The cin.read() and cin.getline() The stream based IO

5 The Stream Advantage The Windows OS The Disk Same C++ Program
Stream based IO to printer Same C++ Program The Disk The Windows OS Stream based IO to disk The Linux OS output redirected to printer Printer Standard library calls of VC++ Standard Library calls of GNU C++ (Linux C++)

6 Steps for File IO Ask for hard disk space to store file.
OS responds positively in most of the cases OS knows the file by some other name (physical) then programmer’s name (logical). Asking for association of physical file with the logical file. This step also involves specifying type of linking; the file mode

7 The file mode Specify the stream We need to specify file mode.
Just reading from physical file Just writing to it Doing both read and write together etc. Specify the stream The default stream is text Files based on binary stream are known as binary files

8 Buffers and file handle
buffers to store intermediate data unique identifier usually known as file handle to represent that file. entire set of requests for the manipulation of the same file will bear the same identifier.

9 File IO in C++ If file is opened in read or read-write mode, we can read from it. If file is opened in write mode; we cannot read from it. At times, we would like to reach to specific location in the file, known as random access The last operation involved with files is closing a file.

10 Text and binary streams
Text stream deals with information, which is ASCII form. Binary streams are pure binary streams Text files are made up of ASCII characters In the case of binary file, the numbers are stored as binary numbers no of characters read from screen may differ when written to or read from a text file

11 The Difference Text file records are separated by CR-LF sequence (in windows). Text files records can be varied size while a binary file record usually of the same size Text files are more general

12 Defining files Files can be defined with three possible types.
ifstream <filename> (input file stream) ofstream <filename> (output file stream) fstream <filename> (IO file stream) Files can be opened using two methods. Using constructors Using open function

13 Providing Random Access
For any given file opened in read-write mode seekg is a pointer for reading (or getting) and seekp is a pointer for writing (or putting). Both functions takes two arguments, No of bytes to skip and From where to skip We can count or skip no of bytes from Beginning of the file OR End of the file

14 Jumping to a location ios::end End ios::beg Beginning
ios::cur Current position

15 The tellg and tellp tellg and tellp are functions which tells us where read and write pointers of file are tellg tells us where get pointer is pointing to tellp tells us where put pointer is pointing to

16 IO Modes; ios::ate ios::ate puts the file pointer at the end of the file ios: app opens the file in append mode put the file pointer at the end we can only write at the end of a file Both ate and app work only with existing files. If we use ifstream constructor, ios::in is default and if we use ofstream ios::out is default.

17 IO Mode Effect ios::in File opens in input mode ios::out File opens in output mode ios::trunc When the file is opened, the contents are erased. ios::noreplace Checks if the file exists, if file does not exist, the call to open fails. ios::nocreate Checks if the file exists, if file exists, the call to open fails. ios::binary The file would be opened in binary rather then default text mode


Download ppt "Programming with ANSI C ++"

Similar presentations


Ads by Google