Escolar Documentos
Profissional Documentos
Cultura Documentos
file-stream-class stream-object;
stream-object . open (“filename”);
Open and close a file
eg:-
ofstream outfile; // create stream
outfile . open (“DATA1”); // connect stream to DATA1
……………………………..
……………………………..
outfile . Close(); //disconnect stream from DATA1
outfile . Open(“DATA2”); //connect stream to DATA2
……………………………..
……………………………..
outfile . close();
……………………………..
Example: Word File
Mode of file opening
ios::in Open for input operations.
ios::out Open for output operations.
ios::binary Open in binary mode.
Set the initial position at the end of the file.
ios::ate
If this flag is not set, the initial position is the beginning of the file.
Both ios :: app and ios :: ate take us to the end of the file
when it is opened. The difference between the two
parameters is that the ios :: app allows us to add data to
the end of file only, while ios :: ate mode permits us to add
data or to modify the existing data any where in the file.
The mode can combine two or more parameters
using the bitwise OR operator (symbol |)
eg :-
fstream file;
file . Open(“ data . txt”, ios :: out | ios :: in);
File pointer
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.
Function for manipulation
of file pointer
When we want to move file pointer to desired
position then use these function for manage the
file pointers.
file.get(ch);
file.put(ch);
read() and write() function
write ( memory_block, size );
read ( memory_block, size );
These function take two arguments. The first is the address of the
variable V , and the second is the length of that variable in bytes .
The address of variable must be cast to type char * (i.e pointer to
character type) .
Error Handling
Sometimes during file operations, errors may also creep in. For
example, a file being opened for reading might not exist. Or a file
name used for a new file may already exist. Or an attempt could be
made to read past the end-of-file. Or such as invalid operation may
be performed. There might not be enough space in the disk for
storing data.
To check for such errors and to ensure smooth processing, C++ file
streams inherit 'stream-state' members from the ios class that store
the information on the status of a file that is being currently used.
The current state of the I/O system is held in an integer, in which
the following flags are encoded :
Stream Errors
int main()
{
ifstream inFile; //Declares a file stream
object
string word;
int count = 0;
#include<fstream.h>
int main()
{
ofstream fout;
fout.open("out.txt");
char str[300]=“Welcome to the Programming World";
fout<<str;
fout.close();
return 0;
}
Program to read from text file and display it
#include<fstream.h>
#include<conio.h>
int main()
{
ifstream fin;
fin.open("out.txt");
char ch;
while(!fin.eof())
{
fin.get(ch);
cout<<ch;
}
fin.close();
getch();
return 0;
}
Count number characters in a text file
#include<fstream.h>
#include<conio.h>
int main()
{
ifstream fin;
fin.open("out.txt");
clrscr();
char ch;
int count=0;
while(!fin.eof())
{
fin.get(ch);
count++;
}
cout<<"Number of characters in file is "<<count;
fin.close();
getch();
return 0;
}
Program to count number of lines
#include<fstream.h>
#include<conio.h>
int main()
{
ifstream fin;
fin.open("out.txt");
char str[80];
int count=0;
while(!fin.eof())
{
fin.getline(str,80);
count++;
}
cout<<"Number of lines in file is "<<count;
fin.close();
getch();
return 0;
}
Program to copy contents of file to another file.
#include<fstream.h>
int main()
{
ifstream fin;
fin.open("out.txt");
ofstream fout;
fout.open("sample.txt");
char ch;
while(!fin.eof())
{
fin.get(ch);
fout<<ch;
}
fin.close();
return 0;
}