Tuesday 29 November 2011

File Handling in C++(Basics)

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::in
ofstream ios::out
fstream 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<fstream>
#include<iostream>

int main()
{
ofstream write_file("c:/ishkaran.txt"); //file opened in write mode

write_file << "hello!";

write_file.close(); //file closed
return 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 mode

read_file >> ch;
cout<<ch;
read_file.close(); //file closed
return 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....!!

11 comments:

  1. jingala huhuhu :D

    ReplyDelete
  2. jingalala ha jingalala hu...!!!!!!!!!!!!!!!!!!

    ReplyDelete
  3. You should also do something with your comments moderation section, no ?

    ReplyDelete
  4. THANKS BOHT BOHT MERA PROBLEM SOLVE HO GYA

    ReplyDelete
  5. Write a program that reads a text file specified by the user and reports how many times each word occurred in the file. The output should be formatted in two columns, with the words in the first column and the numbers in the second column aligned on their right side in a column. You may assume the input file contains no punctuation marks. An example output is shown below for the file “in.txt”. Input: In.txt Gandalf is old Gandalf is a wizard Gandalf is a friend to hobbits blah blah blah blah blah blah blah blah blah blah Output Enter file name: in.txt Gandalf 3 is 3 old 1 a 2 wizard 1 friend 1 to 1 hobbits 1 blah 10

    I'm a beginner.....need help on above statement

    ReplyDelete
  6. File Handling in C++
    A file is a collection of related data stored in a particular area on the disk. The data is stored in disk using the concept of file. In c++ we can easily handle file for store data permanently.

    ReplyDelete