12/04/2017

BCS 1013 PSCP. Lab File Input and File Output

1. Consider the following C++ program. What is the content of "test.txt".

#include
#include

using namespace std;
int main(int argc, char *argv[])
{
   ofstream out("test.txt");
   out << 10 << " " << 123.23 << "\n";
   out << "This is a short text file.";
   out.close();
   system("PAUSE");
   return EXIT_SUCCESS;

}


2. Write a program that prompts user to enter:-

    Name
    Age
    State
    Country

and write into a file named biodata.txt as follows.


Name   : Ahmad Ashraaf
Age    : 19
State  : terengganu
Country: Malaysia

3. The following program is supposed to read two numbers from a file named input.dat and write the sum of the numbers to a file named output.dat. However, it fails to do so. Rewrite the program so that it accomplishes whatit is intended to do. (Also, include statements to close the files.)

#include
#include
using namespace std;
int main()
{
int num1, num2;
ifstream infile;
outfile.open("output.dat");
infile >> num1 >> num2;
outfile << "Sum = " << num1 + num2 << endl;
return 0;
}


4. Assume that each of the following statements applies to the same program.

i)   Write a statement that opens file oldmast.dat for input; use an ifstream object called inOldMaster.

ii)  Write a statement that opens file trans.dat for input; use an ifstream object called inTransaction.

iii) Write a statement that opens file newmast.dat for output , use ofstream object outNewMaster.

iv) Write a statement that reads a record from the file oldmast.dat. The record consists of integer accountNumber, string name and floating-point currentBalance; use ifstream object inOldMaster.

No comments:

Cara download Installer windows 10 dalam format ISO

1. Jika anda bercadang untuk download windows 10 melalui website rasmi windows - pilihan untuk download dalam format ISO tidak di berikan.  ...