37
Introduction to Programmi Introduction to Programmi Lecture 18 Lecture 18

CS201- Introduction to Programming- Lecture 18

Embed Size (px)

DESCRIPTION

Virtual University Course CS201- Introduction to Programming Lecture No 18 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]

Citation preview

Page 1: CS201- Introduction to Programming- Lecture 18

Introduction to ProgrammingIntroduction to Programming

Lecture 18Lecture 18

Page 2: CS201- Introduction to Programming- Lecture 18

FileFile

Page 3: CS201- Introduction to Programming- Lecture 18

Text Files Text Files

Executable ProgramsExecutable Programs

Types of Files

Page 4: CS201- Introduction to Programming- Lecture 18

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

File HandlingFile Handling

Text files handlingText files handling

Binary files handling Binary files handling

Page 6: CS201- Introduction to Programming- Lecture 18

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

StreamsStreams

Page 8: CS201- Introduction to Programming- Lecture 18

Header File for File Header File for File HandlingHandling

fstream.hfstream.h

Page 9: CS201- Introduction to Programming- Lecture 18

Header File for File Header File for File HandlingHandling

#include <fstream.h>#include <fstream.h>

Page 10: CS201- Introduction to Programming- Lecture 18

Input File Input File StreamStream

ifstreamifstream

Page 11: CS201- Introduction to Programming- Lecture 18

Output file Output file streamstream

ofstreamofstream

Page 12: CS201- Introduction to Programming- Lecture 18

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

Fully Qualified Path Fully Qualified Path NameName

C:\myProg\C:\myProg\payRoll.txtpayRoll.txt

Page 14: CS201- Introduction to Programming- Lecture 18

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

Close the FileClose the File

myFile.close ( ) ;myFile.close ( ) ;

Page 16: CS201- Introduction to Programming- Lecture 18

Process : Open Process : Open

myfile.open ( “payRoll.txt” ) ;myfile.open ( “payRoll.txt” ) ;

myFile payRoll.txtpayRoll.txt

Page 17: CS201- Introduction to Programming- Lecture 18

Process: Close Process: Close myfile.close ( “payRoll.txt” ) ;myfile.close ( “payRoll.txt” ) ;

myFilepayRoll.txtpayRoll.txt

X

Page 18: CS201- Introduction to Programming- Lecture 18

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

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

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

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

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

myfile.eof ( )myfile.eof ( )

while ( !myfile.eof ( ) )while ( !myfile.eof ( ) )

{{

myfile >> varName myfile >> varName ;;

}}

Page 24: CS201- Introduction to Programming- Lecture 18

get ( )get ( )

char ch ; char ch ;

myFile.get ( ch ) ;myFile.get ( ch ) ;

Page 25: CS201- Introduction to Programming- Lecture 18

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

put ( )put ( )

outputFile.put ( ch ) ;

Page 27: CS201- Introduction to Programming- Lecture 18

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

int i ;int i ;

i = 0 ;i = 0 ;

int i = 0 ;int i = 0 ;

Page 29: CS201- Introduction to Programming- Lecture 18

Open fileOpen file

ifstream myInputFile ifstream myInputFile ( “myfile.txt” ) ;( “myfile.txt” ) ;

Page 30: CS201- Introduction to Programming- Lecture 18

Open fileOpen file

ofstream myOnputFile ( “myfile.txt” ) ;ofstream myOnputFile ( “myfile.txt” ) ;

Page 31: CS201- Introduction to Programming- Lecture 18

strtok ( )strtok ( )

Page 32: CS201- Introduction to Programming- Lecture 18

getline ( ) getline ( ) functionfunction

Page 33: CS201- Introduction to Programming- Lecture 18

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

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

FileFile

AmirAmir 10001000

Amara 1002Amara 1002

Page 36: CS201- Introduction to Programming- Lecture 18

strtok ( string , delimiter )strtok ( string , delimiter )

Page 37: CS201- Introduction to Programming- Lecture 18

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 ) ;

::