ifstream & ofstream

closed account (DG6DjE8b)
1.Suppose that infile is an ifstream variable and employee.dat is a file that contains employees' information. Write the C++ statement that opens this file using the variable infile.

2.Suppose that infile is an ifstream variable and it is associated with the file that contains the following data: 27306 savings 7503.35. Write the C++ statement(s) that reads and stores the first input in the int variable acctNumber, the second input in the string variable accountType, and the third input in the double variable balance.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int acctNumber;
    string accountType;
    double balance;

    ifstream infile("employee.dat");

    if(infile.is_open())
    {
        infile >> acctNumber;
        infile >> accountType;
        infile >> balance;

        infile.close();
    }

    cout << "acctNumber = " << acctNumber << endl
         << "accountType = " << accountType << endl
         << "balance = " << balance << endl
         << endl;

    return 0;
}


Don't tell me that this was your homework or something... ^_^

Interesting to remember that all these types (int, string and double) can be outputed to a stream like cout, or inputed from a stream like cin or an ifstream... :-)
Last edited on
Topic archived. No new replies allowed.