data file handling points

Upload: neena-sharma

Post on 03-Jun-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 Data File Handling Points

    1/32

    DATA FILE HANDLING

  • 8/12/2019 Data File Handling Points

    2/32

    INTRODUCTION Computer programs are associated to work with

    files as it helps in storing data & informationpermanently.

    File - itself a bunch of bytes stored on some

    storage devices.

    In C++ file I/O is achieved through a componentheader file called fstream.h

    At lowest level in C++ file is interpreted as

    sequence , or stream, of bytes.

    At user level a file consists of a sequence ofintermixed data types characters, arithmetic

    values etc.

    The I/O library manages two aspects- as interface

    and for transfer of data.

  • 8/12/2019 Data File Handling Points

    3/32

    INTRODUCTION

    The fstream library predefine a set of operations

    for handling all file related input/output throughcertain classes.

    Stream Classes required for File I/O :

    ifstream :- Stream class to read from files

    ofstream :- Stream class to write on files.

    fstream:- Stream class to both read and write from/to

    files

  • 8/12/2019 Data File Handling Points

    4/32

  • 8/12/2019 Data File Handling Points

    5/32

    STREAM I/O

    The stream that supplies data to the program is

    known as input stream

    The stream that receives data from the program

    is known as output stream

    File Program ( Input stream) - reads data from file

    Program File (Output stream)write data to file

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

  • 8/12/2019 Data File Handling Points

    6/32

    6

  • 8/12/2019 Data File Handling Points

    7/32

    IFSTREAM

    Input file stream Class

    It provide input operation for file open() is a member function of the class ifstream

    Inherited functions of ifstream class, from the class

    istream are:-

    Text File get()

    getline()

    Binary File

    read()

    For supporting random access

    seekg()

    tellg()

  • 8/12/2019 Data File Handling Points

    8/32

  • 8/12/2019 Data File Handling Points

    9/32

  • 8/12/2019 Data File Handling Points

    10/32

    DATA FILES Requirement of 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.

    1. Text file : A text file stores information inASCII characters. Each line terminated byEOL ( end of line).

    2. Binary file : It stores information in the same

    format in which the information is held inmemory. No delimiter for line. no translationoccurs. It is faster and easier for a programread and write.

  • 8/12/2019 Data File Handling Points

    11/32

    OPENING AND CLOSING FILESUsing Constructor:

    ifstream input_file(DataFile);char ch;Input_file>>ch;float amt;

    input_file>>amt;

    ofstream output_file(DataFile);

    input_file.close( );output_file.close( );

    DataFile

    Input stream

    input_fileProgram

  • 8/12/2019 Data File Handling Points

    12/32

    OPENING AND CLOSING FILESofstream output_file(DataFile);

    Opening a file for output using stream classconstructor creates a new file if there is no file of

    that name on the disk. However , if the file by that

    name exists already, the act of opening it for

    output scraps it off so that output starts a fresh line

    Closing file :-input_file.close( );output_file.close( );

    Close() does not eliminate the stream; it just

    disconnects from the file.

  • 8/12/2019 Data File Handling Points

    13/32

    OPENING FILES

    Using open() method

    STREAM-OBJECT.open(FILENAME, mode)

    Eg:-

    ofstream OFILE;

    OFILE.open(DATA1.TXT);

    ifstream IFILE;

    IFILE.open(DATA2.TXT);

  • 8/12/2019 Data File Handling Points

    14/32

    FILE MODES The File mode describes how a file is to be used:-File modeparameter

    Description Stream

    Type

    ios::in Open file to read ifstream

    ios::out Open file to write. If the file exists then, previouscontent is discarded else new file is created.

    ofstream

    ios::app All the data you write, is put at the end of the file. It

    calls ios::out

    ofstream

    ios::ate All the date you write, is put at the end of the file. Itdoes not call ios::out. I/O operations can occur

    in anywhere in the file

    ofstream/ifstream

    ios::trunc Deletes all previous content in the file. (empties the

    file)

    ofstream

    ios::nocreate If the file does not exists, opening it with the open()function gets impossible.

    ofstream

    ios::noreplace If the file exists, trying to open it with the open()

    function, returns an error.

    ofstream

    ios::binary Opens the file in binary mode. ofstream/

    ifstream

  • 8/12/2019 Data File Handling Points

    15/32

    FILE MODES

    All the flags can be combined using thebitwise operator OR (|). For example, if we

    want to open the file example.bin in

    binary mode to add data we could do it bythe following call to member function

    open():

    fstream FILE;

    FILE.open ("EXAMPLE.BIN", ios::out | ios::app | ios::binary);

  • 8/12/2019 Data File Handling Points

    16/32

  • 8/12/2019 Data File Handling Points

    17/32

    STEPS TO PROCESS A FILE IN YOUR

    PROGRAM

    Determine the type of link required.

    File to memory Input

    Memory to File OutputBoth Input/Output

    If data is to be brought in from file to memory,

    then link is of type file to-memory If data is to be sent from memory to file, then

    the link is memory-to-file

    S

  • 8/12/2019 Data File Handling Points

    18/32

    STEPS TO PROCESS A FILE IN YOUR

    PROGRAM

    Declare a stream for the desired type of link.

    In order to create file streams, include the header

    file fstream.h

    File to memory (IN TO the memory) ifstream fin

    Memory to File(OUT FROM memory) ofstream fout

    Both fstream finout

  • 8/12/2019 Data File Handling Points

    19/32

    S

  • 8/12/2019 Data File Handling Points

    20/32

    STEPS TO PROCESS A FILE IN YOUR

    PROGRAMNow process as required.

    #includevoid main( ){ofstream fout;fout.open(mark.dat, ios::out);char ans=y;

    int rollno;while(ans==y || ans==Y)

    {coutrollno;fout

  • 8/12/2019 Data File Handling Points

    21/32

  • 8/12/2019 Data File Handling Points

    22/32

    OBJECTIVE : TO INSERT SOME DATA ON A

    TEXT FILE

    Program file SCREEN

    File (.txt, .dat)#includeint main(){

    ofstream fout;fout.open("abc.txt");

    fout

  • 8/12/2019 Data File Handling Points

    23/32

    READING DATA FROM ATEXT FILE

    #include#include#includeint main()

    { ifstream fin;char str[80];fin.open("abc.txt");fin>>str; // read only first string from file// as spaces is treated as termination point

    cout

  • 8/12/2019 Data File Handling Points

    24/32

    DETECTING END OF FILEUsing EOF( ) member function Using filestream object

    Syntax

    Filestream_object.eof( );Example

    #include

    #include

    #include

    int main()

    {

    char ch;

    ifstream fin;

    fin.open("abc.txt");

    while(!fin.eof())// using eof()function{

    fin.get(ch);cout

  • 8/12/2019 Data File Handling Points

    25/32

  • 8/12/2019 Data File Handling Points

    26/32

    BASIC MODEL FOR FILE I/O

    The file stream classes are simply be viewed as astream or array of uninterpreted bytes. That is, it

    is like an array of bytes stored and indexed fromzero to len-1, where len is the total number of bytesin the entire file.

    Each open file has two positions associated with it:

    The current reading position, which is the index of thenext byte that will be read from the file. It simply pointsto the next character that the basic get method will

    return. The current writing position, which is the index of the

    byte location where the next output byte will be placed.It simply points to the location where the basic putmethod will place byte(s).

  • 8/12/2019 Data File Handling Points

    27/32

  • 8/12/2019 Data File Handling Points

    28/32

    RANDOM ACCESS

    tellg() and tellp()

    tellp( )

    It return the distance of writing pointer from the beginningin bytes

    Syntax

    Fileobject.tellp( );

    Example:long n = fout.tellp( );

    tellg( )

    It return the distance of reading pointer from the beginningin bytes

    Syntax

    Fileobject.tellg( );

    Example:

    long n = fout.tellg( );

  • 8/12/2019 Data File Handling Points

    29/32

  • 8/12/2019 Data File Handling Points

    30/32

    ERROR HANDLING IN FILES

    The fail() function returns true (non-zero) if an error

    has occurred with the current stream, false(zero)

    otherwise. This can be used for checking whether theprevious operation has failed.

    Examples of failures that cause fail to be set:

    file not found (when opening for reading).

    file cannot be created (when opening for writing).

    end of file is reached before the requested data could be

    read.

    invalid formatting of data (e.g. letters when expecting

    numbers).

  • 8/12/2019 Data File Handling Points

    31/32

    ERROR HANDLING IN FILES

    The bad() function returns true((non-zero) if afatal error with the current stream has

    occurred, false(zero) otherwise. Note: fatal errors do not normally occur. Even a

    failure to open a file is not a fatal error.

    The function eof() returns true((non-zero) if the

    end of the associated input file has beenreached, false (zero)otherwise.

    The function good() returns true((non-zero) if noerrors have occurred with the current stream,

    false (zero)otherwise.The function clear() does two things:

    it clears all io_stream_state_flags associated with thecurrent stream, and sets the flags

  • 8/12/2019 Data File Handling Points

    32/32

    DIFFERENCE BETWEEN GET()AND GETLINE() FUNCTION

    Both get() and getline() can read characters from

    the input stream into an array till the specified

    number of characters are entered or till the

    delimiting character is encountered( \n by

    default). The difference is that, in case of getline()

    this character is removed from the input stream

    but get() leaves it in the input stream.