728x90 AdSpace

Latest Article



C++ Filing file I/O Quiz Question with Solution





C++ Programming Filing file I/O  Input and ouput read and write in file Quiz Question with Solution sample problem

1. Which of the following classes handles file input?
A. ofstream
B. ifstream
C. instream
D. inputfile
2. What header file contains C++ file I/O instructions?
A. iostream.h
B. fstream.h
C. infstream.h
D. outstream.h

3. How would you output to an open file named a_file?
A. a_file.out("Output");
B. a_file="Output";
C. a_file<<"Output";
D. a_file.printf("Output");

4. Write a program that read 10 integer values from file (input.txt) and print the sum and product of all values in another file (output.txt).

5. What will be the output of this code?

input.txt file
56 4.5


char ch;
int ivalue;
float fvalue;
ifstream in_fstream;
ofstream out_fstream;
in_fstream.open("in.txt");
in_fstream>>ch>>ivalue>>fvalue;
cout<<ch<<" "<<ivalue<<" "<<fvalue;
in_fstream.close();


SOLUTION :



1. Which of the following classes handles file input?

B. ifstream

2. What header file contains C++ file I/O instructions?

B. fstream.h


3. How would you output to an open file named a_file?

C. a_file<<"Output";


4. Write a program that read 10 integer values from file (input.txt) and print the sum and product of all values in another file (output.txt).

ifstream ifs;
 ofstream ofs;
 int values[5];
 int sum=0,pro=1;
 int ivalue;
 ifs.open("data.txt");
 ofs.open("output.txt");
 for(int i=0;i<5;i++)
 {
  ifs>>ivalue;
  sum=sum+ivalue;
  pro=pro*ivalue;
 }  
 ofs<<"Sum:"<<sum<<" Product:"<<pro;
 cout<<"File Create Successfully!!";
 ofs.close();
 ifs.close(); 

no image






  • Blogger Comments
  • Facebook Comments

0 comments:

Post a Comment