Presentation is loading. Please wait.

Presentation is loading. Please wait.

files Dr. Bhargavi Goswami Department of Computer Science

Similar presentations


Presentation on theme: "files Dr. Bhargavi Goswami Department of Computer Science"— Presentation transcript:

1 files Dr. Bhargavi Goswami Department of Computer Science
Christ University Bangalore.

2 introduction Why File Handling?
One program can store values only for that one execution. In real life, we need records to be stored and retrieved as and when required. Output generated cannot be reported. Output generated may be very large, cannot be processed immediately. Inserted data can be carried from one device to another easily. For this we need communication between Console unit and program. Input Stream: Stream that supplies data to the program is known as input stream. Reads/Extracts. Output Stream: Stream that receives data from the program is known as output stream. Writes/Inserts.

3 Type of file names

4 Classes for file stream

5 Stream classes Filebuf :- its purpose is to set the file buffer to read and write. Contain ‘openprot’ constant used in the open() of file stream classes . Also contain close() and open() as method. Fstreambase :- provides operations common to the file stream. Serves as a base for fstream, ifstream and ofsteram class. Contains open() and close() function Ifstream :- provides input operations. Contains open() with default input mode. Inherits the functions get(), getline(), read(), seekg() and tellg() function from istream. Ofstream :- provides output operations. Contains open() with default output mode. Inherits put(), seekp(), tellp() and write() function from ostream. Fstream :- provides support for simultaneous input and output operations. Contains open() with default input mode. Inherits all the function from isteram and ostream classes through iostream

6 Opening and closing a file
If we are using a disk, we need following: 1. Name of file 2. Data type and structure 3. Purpose 4. Opening Method Two ways to open a file: 1) Using constructor 2) Using open() function. File is created automatically when opened for writing, if it doesn’t exist. If exist, overwritten. Remember, stream can connect to only one file at a time. See Example Program 11.1, WorkingWithSingleFile.cpp on pg.no. 291 See Example Program 11.2, WorkingwithMultipleFiles.cpp on pg.no.293

7 Reading from two files simultaneously
At times we require to use two or more files simultaneously. Eg? Two files sorted and saved as third file. Means? First two files are open for reading and third file is open for writing. How to do that? We need two separate input streams for two files. We need one output stream for output file. See Program 11.3, on pg.no.294, ReadingFromTwoFilesSimultaneously.cpp.

8 File mode parameters ios::app Append mode. If the file already exists, its contents are preserved and all output is written to the end of the file. By default, this flag causes the file to be created if it does not exist. ios::ate If the file already exists, the program goes directly to the end of it. Output may be written anywhere in the file. ios::binary Binary mode. When a file is opened in binary mode, information is written to or read from it in pure binary format. (The default mode is text.) ios::in Input mode. Information will be read from the file. If the file does not exist, it will not be created and the open function will fail. ios::nocreate If the file does not already exist, this flag will cause the open function to fail. (The file will not be created.) ios::noreplace If the file already exists, this flag will cause the open function to fail. (The existing file will not be opened.) ios::out Output mode. Information will be written to the file. By default, the file’s contents will be deleted if it already exists. ios::trunc If the file already exists, its contents will be deleted (truncated). This is the default mode used by ios::out.

9 File pointers and manipulators
Each file have two associated pointers known as the file pointers. One of them is called the input pointer (or get pointer) and the other is called the output pointer (or put pointer). The input pointer is used for reading the contents of a given file location and the output pointer is used for writing to a given file location. When we want to move file pointer to desired position then use these function for manage the file pointers. See next slide before going for next program.

10 File pointers and manipulators
Seekg () = moves get pointer (input) to a specified location Seekp () = moves put pointer (output) to a specified location tellg () = gives the current position of the get pointer tellp () = gives the current position of the put pointer fout . seekg(0, ios :: beg) go to start fout . seekg(0, ios :: cur) stay at current position fout . seekg(0, ios :: end) go to the end of file fout . seekg(m, ios :: beg) move to m+1 byte in the file fout . seekg(m, ios :: cur) go forward by m bytes from the current position fout . seekg(-m, ios :: cur) go backward by m bytes from the current position fout . seekg(-m, ios :: end) -- go backward by m bytes from the end

11 Sequential input & output
Put() writes single characteristics to stream. Get() reads single character from stream. For loop is used to provide the access functionality. See Program 11.4 on pg.no.299 I/OOperationsOnCharacters.cpp. Fstream object can handle both the input and output simultaneously. What if the data is in binary? Write() will work as put() Read() will work as get() Advantage? Binary format is more accurate for storing numbers. No conversion, so faster storage. Two arguments are taken. 1) address of variable and 2) length of variable in bytes. file . read ((char *)&V , sizeof (V)); file . Write ((char *)&V , sizeof (V)); See Program 11.5, pg.no.301, I/OOperationOnBinaryFiles.cpp

12 Reading & writing class obj
Read() and write() functions are designed to access user defined data types directly from disks. They handle entire structure of an object as a single unit. Similarly write() will copy one by one byte without conversion from memory to disk. Remember, only data members are written to disk and not the functions. Program uses for loop to read and write objects. While loop may be used. See Program 11.6, pg.no.302, ReadingandWritingClassObjects.cpp If the number of objects are not known, it can be counted. How? Using tellg() function to determine current position of get pointer. See Program 11.7, pg.no.304, CountingObjectsinaFile.cpp. Instead, see main.cpp

13 Updating a file: random access
Updating is a routing task in maintenance of any data. Updating would include one or more of following: a) Displaying content of a file b) Modifying existing items c) Adding a new item d) Deleting an existing item This needs 1) pointers to move to particular location of files and 2) size of object if similar records exist in file. Seekg() and seekp() is used to reach to particular location in file. We can obtain file size using tellg() and tellp(). We created file STOCK.DAT during 11.6 program. Next program uses this file and does following operations. 1) Adds new item to file, 2) modifies detail of item 3) displays contents of file. Program 11.8, pg.no.306, FileUpdatingRandomAccess.cpp.

14 Thank you. Best wishes for your exam.
Syllabus is Over. Thank you. Best wishes for your exam.


Download ppt "files Dr. Bhargavi Goswami Department of Computer Science"

Similar presentations


Ads by Google