cs201- introduction to programming- lecture 18
DESCRIPTION
Virtual University Course CS201- Introduction to Programming Lecture No 18 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]TRANSCRIPT
![Page 1: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/1.jpg)
Introduction to ProgrammingIntroduction to Programming
Lecture 18Lecture 18
![Page 2: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/2.jpg)
FileFile
![Page 3: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/3.jpg)
Text Files Text Files
Executable ProgramsExecutable Programs
Types of Files
![Page 4: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/4.jpg)
Memory is volatileMemory is volatile
Any data that you key in by Any data that you key in by keyboard while a program is keyboard while a program is running is also volatilerunning is also volatile
![Page 5: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/5.jpg)
File HandlingFile Handling
Text files handlingText files handling
Binary files handling Binary files handling
![Page 6: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/6.jpg)
Steps to handle Steps to handle filefile
Open the FileOpen the File
Read / Write the File Read / Write the File
Close the FileClose the File
![Page 7: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/7.jpg)
StreamsStreams
![Page 8: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/8.jpg)
Header File for File Header File for File HandlingHandling
fstream.hfstream.h
![Page 9: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/9.jpg)
Header File for File Header File for File HandlingHandling
#include <fstream.h>#include <fstream.h>
![Page 10: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/10.jpg)
Input File Input File StreamStream
ifstreamifstream
![Page 11: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/11.jpg)
Output file Output file streamstream
ofstreamofstream
![Page 12: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/12.jpg)
Example 1Example 1#include <fstream.h>#include <fstream.h>
ifstream myFile ; ifstream myFile ;
myFile.open ( “payRoll.txt” ) ; myFile.open ( “payRoll.txt” ) ;
![Page 13: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/13.jpg)
Fully Qualified Path Fully Qualified Path NameName
C:\myProg\C:\myProg\payRoll.txtpayRoll.txt
![Page 14: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/14.jpg)
Access file dataAccess file datamyfile >> var1;myfile >> var1;
We can also write:We can also write:
myfile >> var1 >> var2 ;myfile >> var1 >> var2 ;
![Page 15: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/15.jpg)
Close the FileClose the File
myFile.close ( ) ;myFile.close ( ) ;
![Page 16: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/16.jpg)
Process : Open Process : Open
myfile.open ( “payRoll.txt” ) ;myfile.open ( “payRoll.txt” ) ;
myFile payRoll.txtpayRoll.txt
![Page 17: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/17.jpg)
Process: Close Process: Close myfile.close ( “payRoll.txt” ) ;myfile.close ( “payRoll.txt” ) ;
myFilepayRoll.txtpayRoll.txt
X
![Page 18: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/18.jpg)
Example 1Example 1ifstream myFile ;ifstream myFile ;myFile.open ( “myFile.txt” ) ;myFile.open ( “myFile.txt” ) ;
if ( !myFile ) // Error checkif ( !myFile ) // Error check{{
cout << “Your file could not be cout << “Your file could not be opened”;opened”;}}
------------myFile.close ( ) ;myFile.close ( ) ;
![Page 19: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/19.jpg)
Output File ModesOutput File Modes
Create a new fileCreate a new file Overwrite an existing fileOverwrite an existing file Append some textAppend some text Randomly accessing a Randomly accessing a
filefile
![Page 20: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/20.jpg)
SyntaxSyntaxfStream fileVar ( “fileName” , mode ) ; // fStream fileVar ( “fileName” , mode ) ; // Generic syntaxGeneric syntax
ifstream myfile ( “myfile.txt” , ios :: in ) ;ifstream myfile ( “myfile.txt” , ios :: in ) ;
ofstream myfile ( “myfile.txt” , ios :: out ) ;ofstream myfile ( “myfile.txt” , ios :: out ) ;
Opening Mode
Opening Mode
![Page 21: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/21.jpg)
ios :: in ios :: in open for reading (default for ifstream) open for reading (default for ifstream)
ios :: outios :: out open for writing (default for ofstream) open for writing (default for ofstream)
ios :: appios :: app start writing at end of file (APPend) start writing at end of file (APPend)
ios :: ateios :: ate start reading or writing at EOF of file (ATEnd) start reading or writing at EOF of file (ATEnd)
ios :: truncios :: trunc truncate file to zero length if it exists truncate file to zero length if it exists (TRUNCate)(TRUNCate)
ios :: nocreate error when opening if file does not already existios :: nocreate error when opening if file does not already exist
ios :: noreplace error when opening for output if file already ios :: noreplace error when opening for output if file already existsexists
ios :: binaryios :: binary open file in binary (not text) mode open file in binary (not text) mode
List of File Handling ModesList of File Handling Modes
![Page 22: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/22.jpg)
AppendAppendofstream myfile (“myfile.txt” , ios :: app ofstream myfile (“myfile.txt” , ios :: app ) ;) ;
Random AccessRandom Access ofstream myfile ( “myfile.txt” , ios :: ofstream myfile ( “myfile.txt” , ios :: ate ) ;ate ) ;
TruncateTruncate ofstream myfile ( “myfile.txt” , ofstream myfile ( “myfile.txt” , ios::trunc ) ;ios::trunc ) ;
![Page 23: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/23.jpg)
myfile.eof ( )myfile.eof ( )
while ( !myfile.eof ( ) )while ( !myfile.eof ( ) )
{{
myfile >> varName myfile >> varName ;;
}}
![Page 24: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/24.jpg)
get ( )get ( )
char ch ; char ch ;
myFile.get ( ch ) ;myFile.get ( ch ) ;
![Page 25: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/25.jpg)
Example 2Example 2while ( !myFile.eof ( ) )while ( !myFile.eof ( ) )
{{
myFile.get ( ch ) ;myFile.get ( ch ) ;
cout << ch ;cout << ch ;
}}
![Page 26: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/26.jpg)
put ( )put ( )
outputFile.put ( ch ) ;
![Page 27: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/27.jpg)
ifstream myInputFile ( “myfile.txt” , ios :: in ) ;ifstream myInputFile ( “myfile.txt” , ios :: in ) ;
ofstream myOnputFile ( “myfile.txt” , ios :: ofstream myOnputFile ( “myfile.txt” , ios :: out ) ;out ) ;
![Page 28: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/28.jpg)
int i ;int i ;
i = 0 ;i = 0 ;
int i = 0 ;int i = 0 ;
![Page 29: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/29.jpg)
Open fileOpen file
ifstream myInputFile ifstream myInputFile ( “myfile.txt” ) ;( “myfile.txt” ) ;
![Page 30: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/30.jpg)
Open fileOpen file
ofstream myOnputFile ( “myfile.txt” ) ;ofstream myOnputFile ( “myfile.txt” ) ;
![Page 31: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/31.jpg)
strtok ( )strtok ( )
![Page 32: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/32.jpg)
getline ( ) getline ( ) functionfunction
![Page 33: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/33.jpg)
Syntax of getline Syntax of getline functionfunction
myfilemyfile.getline (char *s, int n, char .getline (char *s, int n, char delim);delim);
Character array
Numbers of characters to
be read
delimiter
![Page 34: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/34.jpg)
Syntax of getline Syntax of getline functionfunction
InputInput
Hello WorldHello World
myfile.getline ( arrayString , 20 , myfile.getline ( arrayString , 20 , ’W’ ) ;’W’ ) ;
OutputOutput
HelloHello
![Page 35: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/35.jpg)
FileFile
AmirAmir 10001000
Amara 1002Amara 1002
![Page 36: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/36.jpg)
strtok ( string , delimiter )strtok ( string , delimiter )
![Page 37: CS201- Introduction to Programming- Lecture 18](https://reader036.vdocuments.net/reader036/viewer/2022081413/5492e773b4795900548b45f8/html5/thumbnails/37.jpg)
ExampleExamplechar * namePtr , *salaryPtr , arr [ 30 ] ;char * namePtr , *salaryPtr , arr [ 30 ] ;
double fSalary = 0.0 ;double fSalary = 0.0 ;
inFile.getline ( arr , 30 , ‘\n’ ) ;inFile.getline ( arr , 30 , ‘\n’ ) ;
namePtr = strtok ( arr , " " ) ;namePtr = strtok ( arr , " " ) ;
salaryPtr = strtok ( NULL , " " ) ;salaryPtr = strtok ( NULL , " " ) ;
fSalary = atof ( salaryPtr ) ;fSalary = atof ( salaryPtr ) ;
::