Data File Handlingg

Embed Size (px)

Citation preview

  • 7/29/2019 Data File Handlingg

    1/45

    Data File Handlingin C++

  • 7/29/2019 Data File Handlingg

    2/45

    Introduction Computer programs are associated to work

    with files as it helps in storing data &information permanently. File - itself a bunch of bytes stored on

    some storage devices.

    In C++ this is achieved through acomponent header file calledfstream.h The I/O library manages two aspects- as

    interface and for transfer of data. The library predefine a set of operations

    for all file related handling through certainclasses.

  • 7/29/2019 Data File Handlingg

    3/45

    C++ provides a new technique for handlingI/O operations through mechanism known as

    streams.

    A stream refers to a flow of data.

    Classified in 2 categories:

    1. Output stream

    2. Input stream

    In output stream flow of data is from program to

    the output device.In input stream the flow of data is from input

    device to a program in main memory.

  • 7/29/2019 Data File Handlingg

    4/45

    Thefstream.hheader file

    Streams act as an interface between files andprograms.

    They represent as a sequence of bytes and deals withthe flow of data.

    Every stream is associated with a class having memberfunctions and operations for a particular kind of data

    flow.

    File Program ( Input stream) - readsProgram File (Output stream) write

    All designed into fstream.h and hence needs to beincluded in all file handling programs.

    Diagrammatically as shown in next slide

  • 7/29/2019 Data File Handlingg

    5/45

  • 7/29/2019 Data File Handlingg

    6/45

    Stream Class Hierarchy

    ios

    ostreamistream

    fstream

    iostream

    ofstreamifstream

    User-Defined

    Streams

    System-Defined

    Streams

  • 7/29/2019 Data File Handlingg

    7/45

    Why to use Files:Convenient way to deal large quantities of data.

    Store data permanently (until file is deleted).

    Avoid typing data into program multiple times.

    Share data between programs.

    We need to know:

    how to "connect" file to program

    how to tell the program to read datahow to tell the program to write data

    error checking and handling EOF

  • 7/29/2019 Data File Handlingg

    8/45

    Stream Insertion Operators (data write) Are defined in the ostream class

    The operator , called the extractor, accepts any built-

    in data type passed as arguments

  • 7/29/2019 Data File Handlingg

    9/45

    1: 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 (its internal name)

    to an external file (i.e., on the Unix file system):in_stream.open("infile.dat");

    out_stream.open("outfile.dat");

    4: To see if the file opened successfully:

    if (in_stream.fail())

    { cout

  • 7/29/2019 Data File Handlingg

    10/45

    5: To get data from a file (one option), must declare

    a variable to hold the data and then read it using

    the extractionoperator:

    int num;

    in_stream >> num;

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

  • 7/29/2019 Data File Handlingg

    11/45

    Example (creating file)

    #include

    #include

    #include

    int main()

    {

    ofstream input;

    input.open ("stu.txt");

    input

  • 7/29/2019 Data File Handlingg

    12/45

    Example (creating file)

    #include

    #include

    Void main()

    {

    int rollno

    char name[10];

    ofstream ofil(stud.txt);

    coutrollno>>name;

    cout

  • 7/29/2019 Data File Handlingg

    13/45

    Reading a file

    #include

    #include

    Void main()

    {

    int rollnochar name[10];

    Ifstream ifil(stud.txt);

    If(!ifil) //if( ifil.fail() )

    {

    Cout

  • 7/29/2019 Data File Handlingg

    14/45

    coutrollno>>name;

    cout

  • 7/29/2019 Data File Handlingg

    15/45

    ios:: app - (append) write all output to the end of file

    ios:: ate - data can be written anywhere in the file ios:: binary - read/write data in binary format

    ios:: in - (input) open a file for input

    ios::out - (output) open a file for output

    ios:: trunc -(truncate) discard the files contents if itexists

    File Open Modes

  • 7/29/2019 Data File Handlingg

    16/45

    Stream state member functions

    In C++, file stream classes inherit a stream statemember from the ios class, which gives out the

    information regarding the status of the stream.

    For e.g.:

    eof()used to check the end of file character

    fail()- used to check the status of file at openingfor I/O

    bad()- used to check whether invalid fileoperations or unrecoverable error .

    good()- used to check whether the previous fileoperation has been successful

  • 7/29/2019 Data File Handlingg

    17/45

    File Input and Output Using Objects

    Example:

    #include

    class student

    {

    private:

    int iReg_no;

    char name[20];

    public:

    void setRegno()

    {CoutiReg_no;

    }

  • 7/29/2019 Data File Handlingg

    18/45

    void setName()

    {

    Coutname;

    }

    int getRegno()

    {

    Cout

  • 7/29/2019 Data File Handlingg

    19/45

    void main()

    {

    ofstream Sfil(studfile.dat);char ch;

    student Svar;

    Svar.setRegno();

    Svar.setName();

    Sfil

  • 7/29/2019 Data File Handlingg

    20/45

    cout>ch;

    if(ch== y)

    {

    ifstream Sfil(studfile.dat);

    char ireg;char nam[20];

    Sfil>>ireg>>nam;

    cout

  • 7/29/2019 Data File Handlingg

    21/45

    // Initial experience reading and writing files#include #include #include

    int main(){ ifstream in_stream;ofstream out_stream;int num;in_stream.open("numbers.dat");if (in_stream.fail()) { cout

  • 7/29/2019 Data File Handlingg

    22/45

  • 7/29/2019 Data File Handlingg

    23/45

    INPUT AND OUTPUT OPERATION

    put() and get() functionthe function put() writes a single character to theassociated stream. Similarly, the function get() reads asingle character form the associated stream.example :file.get(ch);file.put(ch);

    write() and read() functionwrite() and read() functions write and read blocks of data.example:file.read((char *)&obj, sizeof(obj));file.write((char *)&obj, sizeof(obj));

  • 7/29/2019 Data File Handlingg

    24/45

    int main()

    {

    ifstream mys;

    char c;

    mys.open("new.txt");

    if(mys.fail())

    {

    cout

  • 7/29/2019 Data File Handlingg

    25/45

    while (!mys.eof())

    {

    cout

  • 7/29/2019 Data File Handlingg

    26/45

    class Person

    {

    private:char name[40];

    int age;

    public:

    void getData()

    {

    cout > name;

    cout > age;

    }} ;

  • 7/29/2019 Data File Handlingg

    27/45

    int main()

    {

    Person per ;

    per.getData();

    ofstream outfile("Person.txt");

    outfile.write((char*)&per, sizeof(per));

    return 0;

    getch();}

  • 7/29/2019 Data File Handlingg

    28/45

    struct record

    {

    char player[20];int age;

    int runs;

    }emp;

    void add()

    {

    ofstream fp("jk.txt",ios::out);

  • 7/29/2019 Data File Handlingg

    29/45

    if(!fp)

    {

    coutemp.age>>emp.runs;

    fp.write((char *)&emp,sizeof(emp));fp.close();

    }

    void display()

  • 7/29/2019 Data File Handlingg

    30/45

    void display()

    {

    ifstream fp("jk.txt",ios::in);

    if(!fp)

    {

    printf("file cannot be opened");

    exit(1);

    }

    printf("records entered");

    fp.read((char *)&emp,sizeof(emp));

    cout

  • 7/29/2019 Data File Handlingg

    31/45

    deletionvoid del()

    { int r;

    student obj;

    ifstream fp1;

    ofstream fp2,fp3;fp1.open("student.dat",ios::in);

    fp2.open("temp1.txt",ios::app);

    fp3.open("temp2.dat",ios::app);

    puts("Enter the deleting roll no.");

    cin>>r;

  • 7/29/2019 Data File Handlingg

    32/45

    while(fp1.read((char *)&obj,sizeof(obj)))

    {

    if(r==obj.roll())

    {

    fp2.write((char *)&obj,sizeof(obj));

    }

    else

    {

    fp3.write((char *)&obj,sizeof(obj));

    }

    }remove("student.txt");

    rename("temp2.txt","student.txt");

    getch();}

    updating

  • 7/29/2019 Data File Handlingg

    33/45

    updatingvoid update()

    {

    student obj;

    int r;

    ifstream fp1;fp1.open("student.txt");

    ofstream fp2;

    fp2.open("temp.txt",ios::app);

    puts("Enter the rollno. to be modified");

    cin>>r;

  • 7/29/2019 Data File Handlingg

    34/45

    while(fp1.read((char*)&obj,sizeof(obj)))

    {

    if(obj.roll()==r)

    {puts("Enter new roll no. and name");

    obj.getdata();

    fp2.write((char*)&obj,sizeof(obj));

    }

    else

    {

    fp1.write((char *)&obj,sizeof(obj));

    }

    }remove("student.txt");

    rename("temp.txt","student.txt");

    getch();

    }

  • 7/29/2019 Data File Handlingg

    35/45

    File pointers

    The get Pointer

    Specifies the location in the file where thenext read operation will occur

  • 7/29/2019 Data File Handlingg

    36/45

    The put Pointer

    Specifies the location in the file where the

    next write operation will occur

  • 7/29/2019 Data File Handlingg

    37/45

    The seekg() Function

    Helps to control the get pointer

    Moves the get pointer to an absoluteaddress within the file or to a certain number

    of bytes from a particular position

    Takes two arguments:The number of bytes to move

    The reference in the file from where the pointer

    has to be repositioned

    Example:

    ifstream iFil;

    iFil.seekg(10,ios::beg);

  • 7/29/2019 Data File Handlingg

    38/45

    The tellg() Function

    Helps to control the get pointer

    Can be used to find the current position of

    the get file pointer in a file

    Does not take any arguments

    Example:

    int iPosition=iFil.tellg();

  • 7/29/2019 Data File Handlingg

    39/45

    The seekp() Function

    Helps to control the put pointer

    Moves the put pointer to an absolute

    address within the file or to a certain number

    of bytes from a particular position

  • 7/29/2019 Data File Handlingg

    40/45

    The tellp() Function

    Helps to control the put pointer

    Can be used to find the current position of

    the put file pointer in a file

  • 7/29/2019 Data File Handlingg

    41/45

    ExampleTo find the size of the file

    #include

    #include

    Void main()

    {

    Ifstream ifil(abc.txt);

    If(!ifill)

    {

    Cout

  • 7/29/2019 Data File Handlingg

    42/45

    ifill.seekg(0,ios::end);

    int x=ifil.tellg();

    Cout

  • 7/29/2019 Data File Handlingg

    43/45

    void student::getdata()

    {

    coutname;

    coutrollno;

    }

    void student::date::getdate()

    {

    cout

  • 7/29/2019 Data File Handlingg

    44/45

    void student::display()

    {

    cout

  • 7/29/2019 Data File Handlingg

    45/45

    cout