Writing in .txt mode:
#include <iostream>
#include
<fstream> //// header file for file input and output
using
namespace std;
void main()
{void main()
int i = 10;
char c =
'A';
float
f = 2.222;
ofstream infile("First.txt"); // creating file
ofstream infile("First.txt"); // creating file
// Here infile is file handle
infile<<i<<c<<f; //
writing to file using “<<”(insertion operator)
infile.close();
// closing file opened with file handle
infile
// if you open a file in your program then it
//is necessary to close that file
using
//built in function under fstream header file.
cout<<"Data
written to file"<<endl;
system("pause");
}
If you open the First.txt file it will looklike this..
output:
10A2.222
You can
see here value of i which is 10, c is A and f is 2.222 all these values are
written in Fist.txt file. There are three standards to open any file in C or
C++’s program. Header file used for all file handling purpose is #include
<fstream>
ofstream:
Open file to write data in file.If the file exist then it opens the existing
file otherwise create a new file to write in it.
Ifstream:
Open file to read data from file. If the file exists then it opens the existing file otherwise it will do nothing.
fstream:
Open file to
perform both read and write operations on file.If the file exists then it opens
the existing file for both read and write otherwise create new file to perform
operations.
common
thing in three options is :
All these functions are include in fstream .
Reading
from .txt file:
//
reading data from file
#include
<iostream>
#include
<fstream> // header file for file input and output
using
namespace std;
void main()
void main()
{
int
i;
char
c;
float
f;
ifstream infile("First.txt"); // opening existing file
ifstream infile("First.txt"); // opening existing file
infile>>i>>c>>f;
// reading data using extraction(>>) operator
infile.close();
// closing file using file handle(infile)
cout<<"Data Written in file is"<<endl;
cout<<"Data Written in file is"<<endl;
cout<<i;
cout<<c; cout<<f;
system("pause");
}
Writing character array to .txt file :
void
main()
{
char
arr[] = "Writing to file"; // char array to write in file
ofstream
infile("First.txt"); // creating file
infile<<arr;
// writing to file
infile<<"I
am in first semester of BS"<<endl;
infile<<"I
am studying at UOG"<<endl;
system("pause");
}
Data written in file of above programme: >>
output:
writing
to file
I am in
first semester of BS
I am
studying at UOG
You can
see value of arr and other two strings written in the file. Like .exe file endl
is used to move on next line which is equalent to ‘\n’ character.
Reading
array from above file:
void
main()
{
char
arr[30];
ifstream
infile("First.txt");
// creating file
while(!infile.eof())
// checking end of file
{
infile.getline(arr,30);
// getting input from file line by line
//written in file.
cout<<arr<<endl;
}
system("pause");
}
Output:
writing
to file
I am in
first semester of BS
I am
studying at UOG
Press any
key to continue......
!infile.eof(
) checks for the end of file opened by handle infile and in while condition
remains true until the end of file is not reached. Loop iterates and prints all
record on .exe file.
If we
declare file handle with this method instead of given in above programs.
Then we use open( ) then we can use different
modes to open the file.
ios::in
|
Open
file for reading (default for ifstream)
|
ios::out
|
Open
file for writing (default for ofstream)
|
ios::ate
|
Start
reading and writing at the end of file
|
ios::app
|
Start
writing at the end of file
|
ios::binary
|
Open
file to perform read and write in binay
|
ios::trunc
|
Truncate
file to zero length if exist
|
void main()
{
char arr[30] = "Writing to
file";
fstream infile; // declaring file handle
infile.open("First.txt",ios::app |
ios::in | ios::out); // creating
file
if(!infile) // !infile check file is opend or
not
{
cout<<"Unable to
openfile"<<endl;
system("pause");
exit(0); // include in process.h or
stdlib.h
}
infile<<arr<<endl;
infile<<"I am in first semester of BS"<<endl;
infile<<"I am studying at UOG"<<endl;
infile<<"Zafar Mehmood Khattak"<<endl;
infile.seekp(0); // moving file pointer at start of file.
while(!infile.eof())
{
infile.getline(arr,30); // getting input from file
cout<<arr<<endl;
}
infile.close();
system("pause");
}
Now if you run program again and again then size of file goes on
increasing and after every run output will become large and large.
cin.getline, cin.get()
To overcome the trouble with cin being word oriented, with that I mean
it reads single words from input and if words consist of more then one word,
like New York, cin buffers York, and use New to store in for example your char
array variable.
Luckily the developers of the library add some inteligent functions to
the cin object. Created from the class isstream.
cin.getline(), and cin.get(), both read a line of input at a time until
they find the '\n' character, but cin.getline() then discards the newline
character, whereas cin.get() leaves it in the input queue.
cin.getline() takes two arguments. The first argument is the name of the
array destined to hold the line of input, and the second argument is a limit on
the number of characters to be read. If this limit is, say, 20, the function
reads no more then 19 characters, leaving room to automaticaly add the null
character at the end. The getline() member function stops reading input when it
reaches this numeric limit or when it reads the newline character, whichever
comes first.
example:
cin.getline(name,20);
This reads the entire line into name array, provided that the line
consists of 19 or fewer characters.
Writing
and Reading String data to/from file:
#include
<fstream>
#include
<iostream>
using
namespace std;
int main
()
{
char
data[100];
// open a file in write mode.
ofstream
outfile;
outfile.open("afile.dat");
cout << "Writing to the
file" << endl;
cout << "Enter your name:
";
cin.getline(data, 100);
// write
inputted data into the file.
outfile << data << endl;
cout << "Enter your age:
";
cin >> data;
cin.ignore();
// again
write inputted data into the file.
outfile << data << endl;
// close
the opened file.
outfile.close();
// open a
file in read mode.
ifstream infile;
infile.open("afile.dat");
cout
<< "Reading from the file" << endl;
infile >> data;
// write
the data at the screen.
cout << data << endl;
// again
read the data from the file and display it.
infile >> data; cout << data << endl;
// close
the opened file.
infile.close();
return
0;
}
No comments:
Post a Comment