Lab Report 06
Lab Report 06
Lab Report 02
Title:
File Handling in C++
1ST SEMESTER
Theory:
Stream :
Stream is a general term used for the flow of data from source to a destination. The
process of input data from source is known as reading. The process of outputting data is
known as writing.
Stream classes are defined in the header files. The objects stream classes are
used to control flow of data.
If stream :
This is used to perform input operations on data files on the disk. The objects of
this stream is used to read file on the disk.
Of stream :
This stream is used to perform the output operations on data files on the disk. The
objects of this stream are used to write data from memory on the disk.
Fstream :
The stream is used to both input & output operations on files on the disk.
File operation modes :
The data files can be opened in different modes. These modes are
defined in “ios” class. The resolution operator (::) is used between the “ios” and the mode
code.
Some of them required are :
ios :: in Open files in input mode to read data from the file. The data can only be read but
cant be written in the file.
ios :: out Opens files in output mode to write data on the file. If the file of the same name
already exists on the disk, all data in the file is erased and a new file is opened.
return 0;
}
Q4 )
#include<iostream> //Required for cerr
#include<fstream> //Required for ifstream, ofstream
using namespace std;
int main()
{
//Define file streams for input and output.
ifstream fin("sensor.txt");
ofstream fout("checkedSensor.txt");
//Check for possible errors.
if(fin. fail())
{
cerr << "could not open input file sensor.dat\n";
exit(1);
}
if(fout. fail())
{
cerr << "could not open output file checkedSensor.dat\n";
exit(1);
}
//All files are open.
double t, motion;
int count(0);
fin >> t >> motion;
while(!fin.eof())
{
++count;
//Write valid data to output file.
if(t >= 0 && motion >= 0)
{
fout << t << " " << motion << endl;
}
//Write invalid data to standard error output.
else
{
cerr << "Bad data encountered on line"
<< count << endl
<< t << " " << motion << endl;
}
//Input next data pair.
fin >> t >> motion;
}//end while
Input file :
Output :
Home Task :
Q1) Program 5.4 :
/*---------------------------------------------------------------*/
/* Program chapter5_6 */
/* This program reads an html file, and writes the text */
/* without the tags to a new file. */
#include<iostream> //Required for cin, cout, cerr.
#include<fstream> //Required for ifstream, ofstream.
#include<string> //Required for string.
using namespace std;
int main()
{
// Declare objects.
char character;
bool text_state(1);
string infile, outfile;
ifstream html;
ofstream htmltext;
// Prompt user for name of input file.
cout << "enter the name of the input file :";
cin >> infile;
// Prompt user for name of output file.
cout << "enter the name of the output file :";
cin >> outfile;
// Open files.
html.open(infile.c_str());
if(html.fail())
{
cerr << "Error opening input file\n";
exit(1);
}
htmltext.open(outfile.c_str());
// Read first character from html file.
html.get(character);
while(!html.eof())
{
// Check state.
if(text_state)
{
if(character == '<') // Beginning of a tag.
text_state=0; // Change States.
else
htmltext << character; // Still text, write to the file
}
else
{
// Command state, no output required.
if(character == '>') // End of tag.
text_state = 1; // Change States.
}
// Read next character from html file.
html.get(character);
}
html.close();
htmltext.close();
return 0;
}
Input file :
Output :
Input file :
Output :
Algorithm :
Algorithm is a step-wise procedure written to give a solution to the particular
problem. It is the basis of any “programming language”. Algorithm are the steps that contain
specific selections, sequences and iterations. It can analyze every problem and can propose a
step by step solution. It is simple and easy to understand for a person who knows about the
language and executes on the available resources.