![if !IE]> <![endif]>
FILE HANDLING CONCEPTS:
OS is the primary interface between the user and the computer. Device driver is a small program that comes with every device IO operations are performed with the help of the OS and the device driver. A C++ program does not directly request to the OS. It invokes a function from a standard library that comes with a C++ compiler, when it is installed.
Text and Binary Files:
Ø deals with information which is in ASCII form.
Ø if <enter> key is pressed, Carriage return and Line Feed characters are inserted. This is known as conversion. The text stream files can be opened by any editor. Text files are more general.
Ø binary values are inserted
Ø no conversion takes place.
The binary stream files are restricted to the application that creates the file. Binary files are not flexible.
Dealing With Text Files:
Ø ifstream < filename> - read only file
Ø ofstream <filename> - output /write only file
Ø fstream <file name > - both input/read and output /write file.
Manipulating Files Opening Files:
Files can be opened using two methods
(i) using constructors
(ii) using open functions
When a file is opened, it must be specified how it is to be opened. This means whether to create it from new or overwrite it and whether it's text or binary, read or write and if the content is to be appended to it.
In order to open a file with a stream object open() member function is used. open (filename, mode);
Write all output to the end of file (even if file position pointer is moved with seekp)
Open a file for output and move to the end of the existing data (normally used to append data to a file, but data can be written anywhere in the file
The original file (if it exists) will not be truncated
Open a file for output (default for ofstream objects)
Discard the file's contents if it exists (this is also the default action for ios::out, if ios::ate, ios::app, or ios::in are not specified)
Opens the file in binary mode (the default is text mode)
Open fails if the file does not exist
Open files if the file already exists.
Inserting data somewhere in a sequential file would require that the entire file be rewritten. It is possible, however, to add data to the end of a file without rewriting the file. Adding data to the end of an existing file is called appending.
fout.open("filename.dat", ios::app) //open file for appending
Program to append to the contents of a file
string name, dummy;
int number, i, age;
cout<<"How many names do you want to add?";
fout.open ("name_age.dat",ios::app); // open file for appending
assert (!fout.fail( ));
for(i=1, i<=number; i++)
cout<<"Enter the name: ";
cout<<"Enter age: ";
fout<<name<<endl; //send to file
fout.close( ); //close file
In C++ , there are two types of file pointers to access a file in a random manner.
ü For a file in the read mode – seekg (pointer for reading or getting)
ü For a file in the write mode – seekp(pointer for wrioting or putting)
Using these, it is possible to search any record of any file by skipping the other records inbetween.
seekg() and seekp():
Ø seekg() to move get or read pointer of file. It takes two arguments ,
§ number of bytes to skip
§ from where to skip
Ø seekp() to move put and write pointers.
tellg() and tellp():
These pointers tell us where the read and write pointers of a file are pointing to.
Ø tellg() tells us where the get pointer is pointing to.
Ø tellp() tells us where the put pointer is pointing to.
std::cout<<"unable to open file";
std::cout<<"enter character code, name and an int\n";
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.