Files Class 12 C++

download Files Class 12 C++

of 26

Transcript of Files Class 12 C++

  • 8/10/2019 Files Class 12 C++

    1/26

  • 8/10/2019 Files Class 12 C++

    2/26

  • 8/10/2019 Files Class 12 C++

    3/26

    When a file is opened in output mode: If the file does not exists, then a new file is created If the file exists, then new set of data overwrites the existing data stored in the file (loss of data)

    So if we want to add more data in an existing file, we need to open the file in append mode. Appendmode is special type of output mode. When a file is opened in output mode: If the file does not exists, then a new file is created If the file exists, then new set of data is added at the end of the file, without overwriting existing data

    stored in the file (no loss of data)

    Reading from a file: Data from the backing storage is transferred to buffer an Operating System Data from the buffer is transferred to work area (part of the RAM containing data) by a C++

    program Reading data from a file is input mode

    Opening a text file in output mode using an instance of of st r eam :1. of s t r eam f out ;

    f out . open( " REPORT. TXT") ; 2. of st r eam f out ( " REPORT. TXT" ) ;

    Opening a text file in output mode using an instance of f s t r eam :1. f s t r e a m f o u t ;

    f out . open( "REPORT. TXT", i os: : out ) ; 2. f st r eam f out ( "REPORT. TXT", i os: : out ) ;

    Opening a text file in append mode using an instance of of st r eam :

    1. of s t r eam f out ;f out . open( " REPORT. TXT" , i os: : app) ; 2. of st r eam f out ( "REPORT. TXT", i os: : app) ;

    Opening a text file in append mode using an instance of f s t r eam :1. f s t r e a m f o u t ;

    f out . open( " REPORT. TXT" , i os: : app) ; 2. f st r eam f out ( "REPORT. TXT", i os: : app) ;

    Opening a text file in input mode using an instance of i f s t r eam :1. i f st r eam f i n;

    f i n. open( " REPORT. TXT" ) ; 2. i f st r eam f i n( "REPORT. TXT") ;

    Opening a text file in input mode using an instance of f s t r eam :1. f st r eam f i n;

    f i n. open( "REPORT. TXT", i os: : i n) ;2. f s t r eam f i n( "REPORT. TXT", i os : : i n) ;

    RAM HDD (Backing storage)Operating System Work Area C++ Program

    Data Buffer REPORT.TXT

  • 8/10/2019 Files Class 12 C++

    4/26

    Writing data into a text file in output mode using file object and insertion operator (output operator

  • 8/10/2019 Files Class 12 C++

    5/26

    cout eno;cout name;cout sal ;f out

  • 8/10/2019 Files Class 12 C++

    6/26

    Running of the program produces following output:FAI PS conduct ed i t s Annual Schol ar Badge Cer emony f el i ci t at i ngt he st udent s who excel l ed academi cal l y i n t he pr evi ous academi cyear . Keepi ng t he DPS t r adi t i on, f or br i l l i ant and consi st entacademi c per f or mance f or 5 year s, st udent s wer e awar ded a Schol arBl ue Ti e. For t hei r met i cul ous consecut i ve achi evement s f or3 year s, a Schol ar Gr een Ti e. The hi gh achi ever s of t hepr evi ous academi c year wer e r ecogni zed wi t h a Schol ar Badge,and t he mer i t or i ous s t udent s wer e pr esent ed a Mer i t Cer t i f i cat e.

    2. To read a text file character by character using stream class member function get () and using fileobject ( f i n ) to check for end of file.#i ncl udevoid mai n( ){

    i f st r eam f i n( "REPORT. TXT") ;char ch;f i n. get ( ch) ;while ( f i n){

    cout wor d;while ( ! f i n. eof ( ) ){

    cout wor d;

    }f i n. cl ose( ) ;

    }

  • 8/10/2019 Files Class 12 C++

    7/26

    Running of the program produces following output:FAI PSconduct edi t sAnnual Schol ar BadgeCer emonyf el i ci t at i ngt hest udent swhoexcel l edacademi cal l yi nt hepr evi ousacademi cyear . Keepi ngt heDPSt r adi ti on, f or br i l l i ant andconsi st ent academi cper f or mancef or 5year s, st udent swer eawar dedaSchol ar Bl ueTi e. For t hei r met i cul ousconsecut i veachi evement sf or 3year s, aSchol ar Gr eenTi e. Thehi ghachi ever sof t hepr evi ousacademi cyear wer er ecogni zedwi t haSchol ar Badge, andt hemer i t or i ouss t udent swer epr esent edaMer i t Cer t i f i cat e.

    The extraction operator (>>) treats white space characters (space / tab / new line) as separator, hencethe file is displayed without any white space characters. The output is not readable and the format ofthe file is lost. So to display the file in a readable manner, every word is displayed on the screenseparated by space but new line characters are lost. Edited program is given below:#i ncl udevoid mai n( ){

    i f st r eam f i n( "REPORT. TXT") ;char wor d[ 15] ;f i n>>wor d;while ( ! f i n. eof ( ) ){

    cout ) and using file object(f i n ) to check for end of file.#i ncl udevoid mai n( ){

    i f st r eam f i n( "REPORT. TXT") ;char wor d[ 15] ;f i n>>wor d;while ( f i n){

    cout

  • 8/10/2019 Files Class 12 C++

    8/26

    6. To read a text file word by word using extraction operator (input operator >>) and using the value ofexpression f i n>>wor d to check for end of file.#i ncl udevoid mai n( ){

    i f st r eam f i n( "REPORT. TXT") ;char wor d[ 15] ;while ( f i n>>wor d)

    cout ch;while ( ! f i n. eof ( ) ){

    cout ch;

    }f i n. cl ose( ) ;

    }Running of the program produces following output:

    FAI PSconduct edi t sAnnual Schol ar BadgeCer emonyf el i ci t at i ngt hest udent swhoexcel l edacademi cal l yi nt hepr evi ousacademi cyear . Keepi ngt heDPSt r adi ti on, f or br i l l i ant andconsi st ent academi cper f or mancef or 5year s, st udent swer eawar dedaSchol ar Bl ueTi e. For t hei r met i cul ousconsecut i veachi evement sf or 3year s, aSchol ar Gr eenTi e. Thehi ghachi ever sof t hepr evi ousacademi cyear wer er ecogni zedwi t haSchol ar Badge, andt hemer i t or i ouss t udent swer epr esent edaMer i t Cer t i f i cat e.

    The extraction operator (>>) treats white space characters as separator, hence the file is displayedwithout any white space characters. Nothing can be done to improve the display on the screen.

    8.

    To read a text file character by character using extraction operator (input operator >>) and using fileobject function to check for end of file.#i ncl udevoid mai n( ){

    i f st r eam f i n( "REPORT. TXT") ;char ch;f i n>>ch;while ( f i n){

    cout ch;}f i n. cl ose( ) ;

    }

  • 8/10/2019 Files Class 12 C++

    9/26

    9. To read a text file character by character using extraction operator (input operator >>) and using thevalue of expression f i n>>ch to check for end of file.#i ncl udevoid mai n( ){

    i f st r eam f i n( "REPORT. TXT") ;char ch;while ( f i n>>ch)

    cout

  • 8/10/2019 Files Class 12 C++

    10/26

    while ( f i n){

    cout

  • 8/10/2019 Files Class 12 C++

    11/26

    3. Write a C++ function to display the text file STORY.TXT on the screen. At end display number ofnon-white space characters present in the file.void r eadt ext f i l e( ){

    i f st r eam f i n( "STORY. TXT") ;char ch;int c=0;while ( f i n. get ( ch) ){

    cout

  • 8/10/2019 Files Class 12 C++

    12/26

    while ( f i n>>wor d){

    cout

  • 8/10/2019 Files Class 12 C++

    13/26

    cout

  • 8/10/2019 Files Class 12 C++

    14/26

    while ( f i n. get l i ne( st r i ng, 80) ){

    cout

  • 8/10/2019 Files Class 12 C++

    15/26

  • 8/10/2019 Files Class 12 C++

    16/26

    Binary Data FileData stored in file can either be in text mode or in binary mode. We have already learned few thingsabout text file how to add, how to read and how to edit. Now it is time to learn how to work with

    binary data file. Basic concepts regarding files in C++ are same for both text file and binary file.However there are couple of differences Generally a binary data file has an extension .DAT and extra

    parameter i os : : bi nar y is needed when opening file a binary data file.

    Opening a binary file in output mode using an instance of of st r eam :1. of s t r eam f out ;

    f out . open( "EMP. DAT" , i os: : bi nar y) ; 2. of st r eam f out ( "EMP. DAT", i os: : bi nar y) ;

    Opening a binary file in output mode using an instance of f s t r eam :1. f s t r e a m f o u t ;

    f out . open( "EMP. DAT", i os: : bi nar y | i os: : out ) ; 2. f s t r eam f out ( "EMP. DAT", i os : : bi nar y | i os : : out ) ;

    Opening a binary file in append mode using an instance of of st r eam :1. of s t r eam f out ;

    f out . open( "EMP. DAT", i os: : bi nar y | i os: : app) ; 2. of st r eam f out ( "EMP. DAT", i os: : bi nar y | i os: : app) ;

    Opening a binary file in append mode using an instance of f s t r eam :1. f s t r e a m f o u t ;

    f out . open( "EMP. DAT", i os: : bi nar y | i os: : app) ; 2. f st r eam f out ( "EMP. DAT", i os: : bi nar y | i os: : app) ;

    Opening a binary file in input mode using an instance of i f st r eam :1. i f st r eam f i n;f i n. open( "EMP. DAT", i os: : bi nar y) ;

    2. i f s t r eam f i n( "EMP. DAT", i os : : bi nar y) ;

    Opening a binary file in input mode using an instance of f s t r eam :1. f st r eam f i n;

    f i n. open( "EMP. DAT", i os : : bi nar y | i os : : i n) ;2. f s t r eam f i n( "EMP. DAT" , i os : : bi nar y | i os : : i n) ;

    To begin with, when working with binary data file in C++, we need either a class or a structure. So westart with class declaration empl oyee .

    class empl oyee{

    int eno;char name[ 20] ;double sal ;

    public :void ei nput ( ){

    cout eno;cout

  • 8/10/2019 Files Class 12 C++

    17/26

    void edi spl ay( ){

    cout

  • 8/10/2019 Files Class 12 C++

    18/26

    Function to add n (n is a local variable) records / objects into a binary file in output mode.void addr ec( ){

    of st r eam f out ( "EMP. DAT", i os: : bi nar y) ;empl oyee obj ; int n;cout n;for ( int k=0; k

  • 8/10/2019 Files Class 12 C++

    19/26

    Suppose the binary data file EMP.DAT contains following records:ENO NAME SAL1001 ATUL KRI SHNA 350001009 AARTI SHARMA 450001002 CHETAN J OSHI 370001005 DEEPALI NI GAM 430001004 NI TI N YADAV 480001007 SHI LPI GUPTA 320001003 PRI TAM J AI N 41000

    Function to read and display all the records stored in the binary data file EMP.DAT and checking forend of file using eof ():void di spl ayr ec( ){

    i f s t r eam f i n( "EMP. DAT" , i os : : bi nar y) ;empl oyee obj ;f i n. r ead( char *) &obj , sizeof ( obj ) ) ;while ( ! f i n. eof ( ) ){

    obj . edi spl ay( ) ;f i n. r ead( char *) &obj , sizeof ( obj ) ) ;

    }f i n. cl ose( ) ;

    }

    Stream class member function r ead (), transfers fixed amount of data from the buffer to the work area(values to be stored in the object obj ). Two parameters of the function r ead () are: (char*)&obj address of the data sizeof(obj) size of data in terms of byte

    Calling the function di spl ayr ec () produces following output:1001 ATUL KRI SHNA 350001009 AARTI SHARMA 450001002 CHETAN J OSHI 370001005 DEEPALI NI GAM 430001004 NI TI N YADAV 480001007 SHI LPI GUPTA 320001003 PRI TAM J AI N 41000

    Function to read and display all the records stored in the binary data file EMP.DAT and checking forend of file using file object ( f i n ):void di spl ayr ec( ){

    i f s t r eam f i n( "EMP. DAT" , i os : : bi nar y) ;empl oyee obj ;f i n. r ead( char *) &obj , sizeof ( obj ) ) ;while ( f i n){

    obj . edi spl ay( ) ;f i n. r ead( char *) &obj , sizeof ( obj ) ) ;

    }f i n. cl ose( ) ;

    }

  • 8/10/2019 Files Class 12 C++

    20/26

    Function to read and display all the records stored in the binary data file EMP.DAT and checking forend of file using return value of the stream class member function r ead ():void di spl ayr ec( ){

    i f s t r eam f i n( "EMP. DAT" , i os : : bi nar y) ;empl oyee obj ;while ( f i n. r ead( char *) &obj , sizeof ( obj ) ) )

    obj . edi spl ay( ) ;f i n. cl ose( ) ;

    }

    Function to search for an employee number in the binary data file EMP.DAT. Employee number to besearched is local variable to the function.void sear cheno( ){

    i f s t r eam f i n( "EMP. DAT" , i os : : bi nar y) ;empl oyee obj ;int eno, f ound=0;cout eno;while ( f i n. r ead( char *) &obj , sizeof ( obj ) ) )

    if ( eno==obj . r et eno( ) ){

    obj . edi spl ay( ) ;f ound=1;

    }f i n. cl ose( ) ;if ( f ound==0)

    cout

  • 8/10/2019 Files Class 12 C++

    21/26

    int f ound=0; char name[ 20] ;cout

  • 8/10/2019 Files Class 12 C++

    22/26

    while ( f i n. r ead( char *) &obj , sizeof ( obj ) ) )if ( obj . r et sal ( )

  • 8/10/2019 Files Class 12 C++

    23/26

  • 8/10/2019 Files Class 12 C++

    24/26

    f i n. cl ose( ) ;f out . cl ose( ) ;r emove( " EMP. DAT") ;r ename( " TEMP. DAT", " EMP. DAT") ;

    }

    Function to increase the salary by 5000 of an employee whose record is stored in the binary data fileEMP.DAT by using employee number. Employee number is a local variable to the function.void edi t r eceno( ){

    i f s t r eam f i n( "EMP. DAT" , i os : : bi nar y) ;of st r eam f out ( "TEMP. DAT", i os: : bi nar y) ;empl oyee obj ;int eno, f ound=0;cout eno;while ( f i n. r ead( char *) &obj , sizeof ( obj ) ) ){

    if ( eno==obj . r et eno( ) ){

    double sal =5000+obj . r et sal ( ) ;obj . updat esal ( sal ) ;f ound=1;

    }f out . wr i t e( ( char *) &obj , sizeof ( obj ) )

    }f i n. cl ose( ) ;f out . cl ose( ) ;r emove( " EMP. DAT") ;r ename( " TEMP. DAT", " EMP. DAT") ;if ( f ound==0)

    cout

  • 8/10/2019 Files Class 12 C++

    25/26

    r ename( " TEMP. DAT", " EMP. DAT") ;if ( f ound==0)

    cout

  • 8/10/2019 Files Class 12 C++

    26/26

    File Pointer : it is an indicator inside a file, which gets updated whenever a data is either written into afile or data is read from a file. The concept of a file pointer is similar to a cursor on the screen. Whendata is inputted, the cursor moves from left to right. Also when data is displayed on the screen, then alsocursor moves from left to right. A cursor is visible on the screen but file pointer is not visible. Wheneverwe open a file (in any mode output/append/input mode), file pointer is positioned in the beginning ofthe file. Position of the file pointer in the beginning of the file is 0 (zero). There are 4 stream classmember function is available, related to file pointer. t el l p () returns he position of file pointer in output mode and append mode. Position is returned

    from the beginning of the file and in terms bytes. Function t el l p () is a member function ofof st r eam and f s t r eam . Return value of the function t el l g () is i nt .

    t el l g () returns he position of file pointer in input mode. Position is returned from the beginningof the file and in terms bytes. Function t el l g () is a member function of i f st r eam andf s t r eam . Return value of the function t el l p () is i nt .

    seekp () shifts the file pointer to the desired location inside the file in output mode and appendmode. Function seekp () is a member function of of st r eam and f s t r eam . Return value of thefunction seekp () is voi d . Function seekp () is overloaded. There are two definitions of seekp ():

    seekp( pos) ;shifts the file pointer to pos position from the beginning of the file

    seekp( pos, of f set ) ; shifts the file pointer to pos position from the beginning / end / current position of the file

    seekg () shifts the file pointer to the desired location inside the file in input mode. Functionseekg () is a member function of i f st r eam and f s t r eam . Return value of the function seekg ()is voi d . Function seekg () is overloaded. There are two definitions of seekp ():

    seekg( pos) ;shifts the file pointer to pos position from the beginning of the file

    seekg( pos, of f set ) ; shifts the file pointer to pos position from the beginning / end / current position of the file