Need help opeing and closing the file

// This program reads data from a file and
// prints it in a nicely aligned table.
// PUT YOUR NAME HERE.

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
string code, // Item code of an inventory item
description; // Description of an inventory item
int quantity; // Quantity in stock of an inventory item

ifstream dataIn; // Define an input file stream object

// WRITE A STATEMENT TO OPEN THE table.dat FILE THAT WILL BE
// ACCESSED THROUGH THE dataIn FILE STREAM OBJECT.
tmpfile.open ("table.dat");
// Print table heading
cout << " Warehouse Inventory \n";
cout << "==============================\n\n";
cout << "Item Item Item\n";
cout << "Code Description Qty\n\n";

// Read in five data records and display them
dataIn >> code >> description >> quantity; // Record 1
cout << code << description << quantity << endl;

// REPEAT THE ABOVE CODE FOR RECORDS 2 THROUGH 5.
dataIn >> code >> description >> quantity; // Record 2
cout << code << description << quantity << endl;

// Close the file
// WRITE A STATEMENT TO CLOSE THE DATA FILE.
tmpfile.close();
return 0;
}




table.cpp: In function ‘int main()’:
table.cpp:17:13: error: aggregate ‘std::ifstream dataIn’ has incomplete type and cannot be defined
ifstream dataIn; // Define an input file stream object
^~~~~~
table.cpp:21:13: error: request for member ‘open’ in ‘tmpfile’, which is of non-class type ‘FILE*() {aka _IO_FILE*()}’
tmpfile.open ("table.dat");
^~~~
table.cpp:38:13: error: request for member ‘close’ in ‘tmpfile’, which is of non-class type ‘FILE*() {aka _IO_FILE*()}’
tmpfile.close();
^~~~~
Missing header fstream.

No idea where you're getting tmpfile from; nowhere in this code does such an object get created.

You never associate the input stream, dataIn, with a file to read from.

Topic archived. No new replies allowed.