File Handling in C++(Basics)
File handling is done so dat u can store any result or output to your Hard disk and read dat later on.For eg:
you write a programme to accept the marks of students of your class and store dem in some variables.But as soon d programme is over all those variables are erased from the memory thus all your data is gone.Hence you need to store them in some file from where u can read dem later.This is achieved by FILE HANDLING.
First of all u need "fstream" header file.So include it
after dat u need to open a file either in READ(ifstream) mode or in WRITE(ofstream) mode or in READ-WRITE(fstream) mode.For eg:
ifstream read;
read.open("filename",mode);
There are different modes in which you can open files.
Deafult modes
Ifstream ios::inofstream ios::outfstream ios::in | ios::out
All modes
ios::app If FileName is a new file, data is written to it.
If FileName already exists and contains data, then it is opened, the compiler
goes to the end of the file and adds the new data to it.
ios::ate If FileName is a new file, data is written to it and subsequently added to the
end of the file. If FileName already exists and contains data, then it is opened and data is
written in the current position.
ios::in If FileName is a new file, then it gets created fine as an empty file.
If FileName already exists, then it is opened and its content is made available
for processing.
ios::out If FileName is a new file, then it gets created fine as an empty file. Once/Since it gets created empty, you can write data to it.
If FileName already exists, then it is opened, its content is destroyed, and the file becomes as new.
Therefore you can create new data to write to it. Then,if you save the file, which is the main purpose of this mode, the new content
is saved it.This operation is typically used when you want to save a file
ios::trunc If FileName already exists, its content is destroyed and the file becomes as new.
ios::nocreate If FileName is a new file, the operation fails because it cannot create a new file.
If FileName already exists, then it is opened and its content is made available for processing.
ios::noreplace If FileName is a new file, then it gets created fine.
If FileName already exists and you try to open it, this operation would fail because it cannot create a file of the same name in the same location.
//////////////WRITING///////////////
Now lets try to open a file, write to it, and save it to hard disk
#include#includeint main(){ofstream write_file("c:/ishkaran.txt"); //file opened in write modewrite_file << "hello!";write_file.close(); //file closedreturn 0;}It will create a txt file in your c:\ drive of name ishkaran.txt then it vil write to dat file the string provided and den it vil close d file.
You can also check if the file has been properly opened before readin or writing to it by
if(write_file.is_open())
//Do something with the file
///////////////READING/////////////////////
Now lets read the file we just wrote to.
int main(){char ch[256];ifstream read_file("c:/ishkaran.txt"); //file opened in write moderead_file >> ch;cout<read_file.close(); //file closedreturn 0;}this will print hello but deres a problem here it will read until a space is encountered. So to overcome that uoy should use
read_file.getline( char*,int count,char delimiter);
it reads the number of characters specified in count and passes it to 1st argument either this or it will read until delimiter character is encountered whichever happems first.
u can read like this
while(!read_file.eof()){//Read here}
this loop will work until whole of the file is read. eof() returns true if End of File is encountered.
I have explained only the basics, reading and writing simple data types
Theres still a lot more to file handling ....Will explain more in further posts....!!