CS201- Introduction to Programming- Lecture 18

Preview:

DESCRIPTION

Virtual University Course CS201- Introduction to Programming Lecture No 18 Instructor's Name: Dr. Naveed A. Malik Course Email: cs201@vu.edu.pk

Citation preview

Introduction to ProgrammingIntroduction to Programming

Lecture 18Lecture 18

FileFile

Text Files Text Files

Executable ProgramsExecutable Programs

Types of Files

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

File HandlingFile Handling

Text files handlingText files handling

Binary files handling Binary files handling

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

StreamsStreams

Header File for File Header File for File HandlingHandling

fstream.hfstream.h

Header File for File Header File for File HandlingHandling

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

Input File Input File StreamStream

ifstreamifstream

Output file Output file streamstream

ofstreamofstream

Example 1Example 1#include <fstream.h>#include <fstream.h>

ifstream myFile ; ifstream myFile ;

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

Fully Qualified Path Fully Qualified Path NameName

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

Access file dataAccess file datamyfile >> var1;myfile >> var1;

We can also write:We can also write:

myfile >> var1 >> var2 ;myfile >> var1 >> var2 ;

Close the FileClose the File

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

Process : Open Process : Open

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

myFile payRoll.txtpayRoll.txt

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

myFilepayRoll.txtpayRoll.txt

X

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

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

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

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

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

myfile.eof ( )myfile.eof ( )

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

{{

myfile >> varName myfile >> varName ;;

}}

get ( )get ( )

char ch ; char ch ;

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

Example 2Example 2while ( !myFile.eof ( ) )while ( !myFile.eof ( ) )

{{

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

cout << ch ;cout << ch ;

}}

put ( )put ( )

outputFile.put ( ch ) ;

ifstream myInputFile ( “myfile.txt” , ios :: in ) ;ifstream myInputFile ( “myfile.txt” , ios :: in ) ;

ofstream myOnputFile ( “myfile.txt” , ios :: ofstream myOnputFile ( “myfile.txt” , ios :: out ) ;out ) ;

int i ;int i ;

i = 0 ;i = 0 ;

int i = 0 ;int i = 0 ;

Open fileOpen file

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

Open fileOpen file

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

strtok ( )strtok ( )

getline ( ) getline ( ) functionfunction

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

Syntax of getline Syntax of getline functionfunction

InputInput

Hello WorldHello World

myfile.getline ( arrayString , 20 , myfile.getline ( arrayString , 20 , ’W’ ) ;’W’ ) ;

OutputOutput

HelloHello

FileFile

AmirAmir 10001000

Amara 1002Amara 1002

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

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

::