FILE HANDLING(WORKING WITH FILES) FILE- A file is a collection of related data stored in a particular area on the disk. STREAMS- interface between the programs and the files. TYPES OF STREAMS – Input stream – Output stream
Program typically involves either or both of the following kinds of data communication. – Data transfer between the console unit and program. – Data transfer between the program and the disk file.
Input stream Supplies data to the program. Extracts(reads) data from the file. Output stream – Receives data from the program – Inserts(writes) data to the file.
CLASSES FOR FILE STREAM OPERATIONS The input/output system contains set of classes that define the file handling methods. – Ifstream – Ofstream – Fstream These classes are derived from the fstreambase and the iostream class.
OPENING AND CLOSING A FILE Suitable name for the file. Data type and structure. Purpose. Opening method. A file can be opened in two ways, 1.Using the constructor function of that class. 2.Using the member function open() of the class
OPENING FILE USING CONSTRUCTOR It is used to handle only one file in a stream class. The steps involved are, 1.Create filestream object to manage the stream using the appropriate class. 2.Initialize the file object with the desired filename.
EXAMPLE PROGRAM #include int main() { ofstream outf(“ITEM”); Cout<<“enter item name:”; Char name[30]; Cin>>name; outf<< name<<“\n”; outf.close(); ifstream inf(“ITEM”); inf>>name; Cout<<“item name:”<<name<<“\n”; inf.close(); Return 0; }
OPENING FILE USING OPEN() It is used to open multiple files that use the stream object.
EXAMPLE PROGRAM #include int main() { ofstream fout; fout.open(“country”); fout<<“USA \n”; fout<<“UK \n”; fout.close(); Fout.open(“capital”); Fout<<“newyork \n”; Fout<<“London \n”; Fout.close(); Const int N=50; Ifstream fin; Fin.open(“country”); Cout<<“contents of country file \n”; While(fin) { Fin.getline(line,N); Cout<<line; } Fin.close(); Fin.open(“capital”); Cout<<“contents of capital file \n”; While(fin) { Fin.getline(line,N); Cout<<line; } Fin.close(); Return 0; }
DETECTING END OF FILE while(fin) if(fin1.eof()!=0) { exit(1); }
FILE MODES SYNTAX: – Stream-object. open(“file name”,mode); Different modes of a file: ios::app – Append or write from end of file. ios::binary – A binary file ios::in – To open file in read mode ios::nocreate – Open fails if file doesn’t exist. ios::noreplace – Open fails if file exist. ios::out – To open file in write mode. ios::noreplace – Delete all the contents of file.
FILE POINTERS AND THEIR MANIPULATIONS File pointers: Input pointer-get pointer Output pointer-put pointer Default actions: Takes place automatically.
Functions for manipulation of file pointers: seekg( ) – Moves the pointer to a specified location to read seekp( ) – Moves the pointer to a specified location to write tellg( ) – Gets the current pointer location to read. tellp( ) – Gets the current pointer location to write.
SPECIFYING THE OFFSET: seekg(offset,refposition); seekp(offset,refposition); It takes two argument, offset and reposition. seekg(0, ios::beg) – Go to start. seekg(0, ios::cur) – Stay at current location seekg(0, ios::end) – Go to end of file.
SEQUENTIAL INPUT AND OUTPUT OPERATIONS get()-reads single character. put()-writes single character. These functions are designed to handle a single character at a time. write() read() These functions read and write in the binary form.
INPUT/OUTPUT OPERATIONS ON CHARACTERS #include int main() { Char string[5]=“Hello”; fstream file; file.open(“TEXT”,ios::in|ios:: out); for(i=0;i<5;i++) file.put(string[i]); file.seekg(0); char ch; While(file) file.get(ch); Cout<<ch; } return 0; }
UPDATING A FILE Displaying the contents of a file. Modifying an existing item. Adding a new item. Deleting an existing item.
ERROR HANDLING DURING FILE OPERATIONS A file which we are attempting to open file for reading does not exist. The file used for a new file may already exist. Attempt an invalid operation. There may not be any space in disk for storing more data. Use of an invalid file name. Attempt to perform an operation when the file is not opened for that purpose.
ERROR HANDLING FUNCTIONS: eof() fail() bad() good()