In: Computer Science
1) How do you read the contents of a text file that contains whitespace
characters
as part of its data?
2) What capability does the fstream data type provide that the ifstream
and ofstream
data types do not?
3) What header file do you need to include in a program that performs
file operations?
4) What data type do you use when you want to create a file stream object
that
can write data to a file?
5) What data type do you use when you want to create a file stream object
that
can read data from a file?
6) Why should a program close a file when it
s finished using it?
7) Write code that does the following:
(a) Opens an output file with the filename Numbers.txt ,
uses a loop to write the numbers 1 through 100 to the file,
and then closes the file.
(b) Opens the Numbers.txt file that was created in Part 7a,
reads all of the numbers from the file and
displays them in rows of 10 values, and then closes the file.
(c) Add all of the numbers read from the file and displays their
total.
1. By using getline() method one can read data from file including white spaces.
Example:
#include<iostream>
#include<fstream>
using namespace std;
//driver program
int main()
{
string str;
ifstream infile; //declare the file object to open the
file
//open the files in read mode
infile.open("D:/test.txt");
//if not open
if(!infile)
{
cout<<endl<<"Unable to open
the source file";
exit(0);
}
//read the data from file
while(! infile.eof())
{
getline(infile,str);//read a
line including space
cout<<endl<<str;//display the read data from file
}
}
OUTPUT
DATA OF TEST.TXT
2)
ifstream data type represents to input file stream and the object will have the capacity to read data from file.
ofstream data type represents to output file stream and the object will have the capacity to write data into file.
fstream data type represents to both input and output file stream and the object will have the capacity to read data from file and write data into a file.
3) fstream header file is used during the file operation in c++.
such as
#include<fstream>
4)
ofstream data type will be used to write data into file.
5)
ifstream data type will be used to read data from file.
6)
First of all it is a good practice of a programmer to close a file after performing any operation.
But logically
1. In some situations if you open a file in write mode, then the changes will not completely reflect with file unless you close the file.
2. Your program may allocate unwanted memory for this reason. So garbage collector will come into action to close the files and free the memory from unwanted memory blockage. It will increase the execution speed.
So it is a good practice to close a file after completion of operation with it.
7)
#include<iostream>
#include<fstream>
using namespace std;
//driver program
int main()
{
int i,sum=0,n;
ifstream in; //declare the file object to open the
file in read mode
ofstream out;//declare the file object to open the
file in write mode
//open the files in write mode
out.open("D:/Numbers.txt");
//if not open
if(!out)
{
cout<<endl<<"Unable to open
the file";
exit(0);
}
for(i=1;i<=100;i++) //loop from 1 to 100
out<<" "<<i; //write
the numbers into file
out.close(); //close the file
//open the files in read mode
in.open("D:/Numbers.txt");
//if not open
if(!in)
{
cout<<endl<<"Unable to open
the file";
exit(0);
}
i=0;
//read the data from file
while(!in.eof())
{
in>>n; //read the
number from file and store it in n
if(i%10==0) //condition for new line
cout<<endl;
i++;
cout<<n<<"
";//print the number
sum=sum+n;//compute the
sum
}
in.close();//close the file
//display the sum
cout<<endl<<"Sum of
numbers from 1 to 100 is : "<<sum;
}
output