Introduction to Programming Lecture 18 File Types of




































![Example char * name. Ptr , *salary. Ptr , arr [ 30 ] ; Example char * name. Ptr , *salary. Ptr , arr [ 30 ] ;](https://slidetodoc.com/presentation_image_h2/6afac59303158cb4416cd4a84f379b83/image-37.jpg)
- Slides: 37

Introduction to Programming Lecture 18

File

Types of Files n Text Files n Executable Programs

n Memory is volatile n Any data that you key in by keyboard while a program is running is also volatile

File Handling n Text files handling n Binary files handling

Steps to handle file n Open the File n Read / Write the File n Close the File

Streams

Header File for File Handling fstream. h

Header File for File Handling #include <fstream. h>

Input File Stream ifstream

Output file stream ofstream

Example 1 #include <fstream. h> ifstream my. File ; my. File. open ( “pay. Roll. txt” ) ;

Fully Qualified Path Name C: my. Progpay. Roll. txt

Access file data myfile >> var 1; We can also write: myfile >> var 1 >> var 2 ;

Close the File my. File. close ( ) ;

Process : Open myfile. open ( “pay. Roll. txt” ) ; my. File pay. Roll. txt

Process: Close myfile. close ( “pay. Roll. txt” ) ; my. File X pay. Roll. txt

Example 1 ifstream my. File ; my. File. open ( “my. File. txt” ) ; if ( !my. File ) // Error check { cout << “Your file could not be opened”; } -----my. File. close ( ) ;

Output File Modes Create a new file n Overwrite an existing file n Append some text n Randomly accessing a file n

Syntax f. Stream file. Var ( “file. Name” , mode ) ; // Generic syntax Opening Mode ifstream myfile ( “myfile. txt” , ios : : in ) ; Opening Mode ofstream myfile ( “myfile. txt” , ios : : out ) ;

List of File Handling Modes ios : : in open for reading (default for ifstream) ios : : out open for writing (default for ofstream) ios : : app start writing at end of file (APPend) ios : : ate start reading or writing at EOF of file (ATEnd) ios : : truncate file to zero length if it exists (TRUNCate) ios : : nocreate error when opening if file does not already exist ios : : noreplace error when opening for output if file already exists ios : : binary open file in binary (not text) mode

Append ofstream myfile (“myfile. txt” , ios : : app ) ; n Random Access ofstream myfile ( “myfile. txt” , ios : : ate ) ; n Truncate ofstream myfile ( “myfile. txt” , ios: : trunc ) ; n

myfile. eof ( ) while ( !myfile. eof ( ) ) { myfile >> var. Name ; }

get ( ) char ch ; my. File. get ( ch ) ;

Example 2 while ( !my. File. eof ( ) ) { my. File. get ( ch ) ; cout << ch ; }

put ( ) output. File. put ( ch ) ;

ifstream my. Input. File ( “myfile. txt” , ios : : in ) ; ofstream my. Onput. File ( “myfile. txt” , ios : : out ) ;

int i ; i=0; int i = 0 ;

Open file ifstream my. Input. File ( “myfile. txt” ) ;

Open file ofstream my. Onput. File ( “myfile. txt” ) ;

strtok ( )

getline ( ) function

Syntax of getline function Character array Numbers of characters to be read myfile. getline (char *s, int n, char delim); delimiter

Syntax of getline function Input Hello World myfile. getline ( array. String , 20 , ’W’ ) ; Output Hello

File Amir 1000 Amara 1002

strtok ( string , delimiter )
![Example char name Ptr salary Ptr arr 30 Example char * name. Ptr , *salary. Ptr , arr [ 30 ] ;](https://slidetodoc.com/presentation_image_h2/6afac59303158cb4416cd4a84f379b83/image-37.jpg)
Example char * name. Ptr , *salary. Ptr , arr [ 30 ] ; double f. Salary = 0. 0 ; in. File. getline ( arr , 30 , ‘n’ ) ; name. Ptr = strtok ( arr , " " ) ; salary. Ptr = strtok ( NULL , " " ) ; f. Salary = atof ( salary. Ptr ) ; :