Sabira Paper Work

download Sabira Paper Work

of 24

Transcript of Sabira Paper Work

  • 7/31/2019 Sabira Paper Work

    1/24

    Seminar on:File Handling

    SUBMITTED TO:

    SUBMMITED BY:

    Mr. Chetan Mali

    Sabira Hita

  • 7/31/2019 Sabira Paper Work

    2/24

    (B.C.A 4th

    Sem.)

    Files are used to store data in a relatively permanent form, onfloppy disk, hard disk, tape or other form of secondary storage.

    Files can hold huge amounts of data if need be. Ordinary variables (even records and arrays) are kept in main

    memory which is temporary and rather limited in size.

    Difficult to handle large amount of input data.Output would also be lost as soon as we exit

    from the program.How do we store data permanently?.

    We can use secondary storage device. Data is packaged up on the storage device as data

    structures called files.

  • 7/31/2019 Sabira Paper Work

    3/24

    Text fileBinary filesSequential filesRandom access files

    Text Files:

    A text file consists of readable characters separated intolines by newline characters.

    (On most PCs, the newline character is actuallyrepresented by the two-character sequence of carriagereturn (ASCII 13), line feed (ASCII 10). (\n)

    To insert data into text files:Program file SCR

    Abc.txt

  • 7/31/2019 Sabira Paper Work

    4/24

    Program for writing into a text file:

    Program ABC.txt file contents

    #include

    int main()

    {

    ofstream fout;

    fout.open("abc.txt");

    fout

  • 7/31/2019 Sabira Paper Work

    5/24

    Program for reading from a file:

    #include

    #include#includeint main(){

    ifstream fin;char str[80];fin.open("abc.txt");fin>>str; // read only first //string from file

    cout

  • 7/31/2019 Sabira Paper Work

    6/24

    Binary streamsconsist of data values such as integers,floats or complex data types, using their memoryrepresentation.

    Not having to translate numbers into a readable form makes

    binary files somewhat more efficient.

    Sequential file. With this type of file access one must read thedata in order, much like with a tape, whether the data is reallystored on tape or not.

    Random file(or direct access). This type of file access lets you

    jump to any location in the file, then to any other, etc., all in areasonable amount of time.

    Text Files Binary Files When using a text file,

    we write out separatelyeach of the pieces of

    data about a givenrecord. The text file will be

    readable by an editor

    for the text file we willuse the usual output

    operator(

  • 7/31/2019 Sabira Paper Work

    7/24

    read each of thepieces of record fromthe file separately,using the usual input

    operator(>>)

    With the binary file

    we will use the read

    function to read a

    whole record,

    To access file handling routines:#include

    2: To declare variables that can be used to access

    file:ifstream in_stream;ofstream out_stream;

    3: To connect your program's variable:in_stream.open("infile.txt");out_stream.open("outfile.txt");

    4: To see if the file opened successfully:if (in_stream.fail()){ cout

  • 7/31/2019 Sabira Paper Work

    8/24

    5: To get data from a file (one option), must declarea variable to hold the data and then read it usingthe extraction operator:

    int num;in_stream >> num;

    [Compare: cin >> num;]

    6: To put data into a file, use insertion operator:out_stream

  • 7/31/2019 Sabira Paper Work

    9/24

    I/O in C++:

    Different streams are used to represent different kinds ofdata flow.

    Each stream is associated with a particular class, whichcontains member functions and definitions for dealing with that particular kind of data

    flow.

    The following classes in C++ have access to file input and outputfunctions:

    ifstream

    ofstream fstream

  • 7/31/2019 Sabira Paper Work

    10/24

    some other very important member functionMember function

    name

    Explanation

    seekg( ) Used to move reading pointer forward and backward

    Syntax

    fileobject.seekg( no_of_bytes,mode);Example:

    (a) fout.seekg(50,ios::cur); // move 50 bytes forward from current position

    (b) fout.seekg(50,ios::beg); // move 50 bytes forward from current beginnin(c) fout.seekg(50,ios::end); // move 50 bytes forward from end .

    seekp( ) Used to move writing pointer forward and backward

    Syntax

    fileobject.seekp(no_of_bytes,mode);Example:

    (a) fout.seekp(50,ios::cur); // move 50 bytes forward from current position

    (b) fout.seekp(50,ios::beg); // move 50 bytes forward from current beginnin(c) fout.seekp(50,ios::end); // move 50 bytes forward from end .

    tellp( ) It return the distance of writing pointer from the beginning in bytesSyntax

    Fileobject.tellp( );

    Example:

    long n = fout.tellp( );

    tellg( ) It return the distance of reading pointer from the beginning in bytesSyntax

    Fileobject.tellg( );

    Example:

  • 7/31/2019 Sabira Paper Work

    11/24

    long n = fout.tellg( );

    PARAMETER MEANING ios::app Append to end-of file ios::ate goto end of file on opening ios::binary open binary file ios::in Open existing file for reading ios::nocreate open fails if file doesnt exist

    ios::noreplace open fails if file already exists ios::out creates new file for writing on ios::trunc Deletes contents if it exists

    How to open FileUsing member function Open( ) Using Constructor

    Syntax

    Filestream object;

    Object.open(filename,mode);Example

    ifstream fin;

    fin.open(abc.txt)

    Syntax

    Filestream object(filename,mode)

    Example

    ifstream fin(abc.txt);

    NOTE: (a) Mode are optional and given at the end .

    (a) Filename must follow the convention of 8.3 and its

    extension can be anyone

    How to close fileAll types of files can be closed using close( ) member function

    Syntax

    fileobject.close( );

    Example

  • 7/31/2019 Sabira Paper Work

    12/24

    fin.close( ); // here fin is an object of istream class

    Note: The mode can combine two or more modes using

    bit wise or ( | ).

  • 7/31/2019 Sabira Paper Work

    13/24

  • 7/31/2019 Sabira Paper Work

    14/24

    Example : To read the contents of a text file and display them on the screen.

    Program ( using getline member function) Program ( using get( ) member function)#include

    #include

    #include

    int main()

    {

    char str[100];

    ifstream fin;

    fin.open("c:\\abc.txt");

    while(!fin.eof()){

    fin.getline(str,99);

    cout

  • 7/31/2019 Sabira Paper Work

    15/24

    char name[30];char address[60];};

    int main()

    {

    student s;

    ofstream fout;

    fout.open("student.dat");

    couts.roll;

    couts.name;

    couts.address;

    fout.write((char *)&s,sizeof(student));

    fout.close();

    return 0;

    }

    To make class using filehandling

  • 7/31/2019 Sabira Paper Work

    16/24

  • 7/31/2019 Sabira Paper Work

    17/24

    //***************************************************************// HEADER FILE USED IN PROJECT//****************************************************************

    #include#include#include#include#include

    //***************************************************************// CLASS USED IN PROJECT//****************************************************************

    class account{

    int acno;char name[50];int deposit;char type;

    public:void create_account(); //function to get data from user

  • 7/31/2019 Sabira Paper Work

    18/24

    void show_account(); //function to show data on screenvoid modify(); //function to get new data from uservoid dep(int); //function to accept amount and add to balance amountvoid draw(int); //function to accept amount and subtract from balance amountvoid report(); //function to show data in tabular formatint retacno(); //function to return account numberint retdeposit(); //function to return balance amountchar rettype(); //function to return type of account

    }; //class ends here

    void account::create_account(){

    coutacno;coutdeposit;cout

  • 7/31/2019 Sabira Paper Work

    19/24

    }

    void account::report(){

    cout

  • 7/31/2019 Sabira Paper Work

    20/24

    coutch;clrscr();switch(ch){case '1':

    write_account();break;

    case '2':coutnum;deposit_withdraw(num, 1);break;

    case '3':coutnum;deposit_withdraw(num, 2);break;

    case '4':coutnum;display_sp(num);break;

    case '5':display_all();break;

    case '6':coutnum;delete_account(num);break;

    case '7':coutnum;modify_account(num);break;

    case '8':textattr(128+10);

    gotoxy(23,23);

    cprintf("\n\n\tThanks for using bank managemnt system");break;

    default :cout

  • 7/31/2019 Sabira Paper Work

    21/24

    outFile.close();}

    //***************************************************************// function to read specific record from file//****************************************************************

    void display_sp(int n){

    account ac;int flag=0;ifstream inFile;

    inFile.open("account.dat",ios::binary);if(!inFile){

    cout

  • 7/31/2019 Sabira Paper Work

    22/24

    File.seekp(pos,ios::cur);File.write((char *) &ac, sizeof(account));cout

  • 7/31/2019 Sabira Paper Work

    23/24

    }cout

  • 7/31/2019 Sabira Paper Work

    24/24

    }}

    File.close();if(found==0)

    cout