#include <fstream.h>
#include <stdlib.h>
#include <iomanip.h>
const int MAXLENGTH = 21; // maximum file name length
const int MAXCHARS = 31; // maximum description length
char filename[MAXLENGTH] = "prices.dat";
int main()
{
int ch;
char descrip[MAXCHARS];
float price;
ifstream in_file;
in_file.open(filename);
if (in_file.fail()) // check for successful open
{
cout << "\nThe file was not successfully opened"
<< "\n Please check that the file currently exists."
<< endl;
exit(1);
}
// set the format for the standard output stream
cout << setiosflags(ios::fixed)
<< setiosflags(ios::showpoint)
<< setprecision(2);
cout << endl; // start on a new line
// read and display the file's contents
while ( (ch = in_file.peek()) != EOF ) // check next character
{
in_file >> descrip >> price; // input the data
cout << descrip << ' ' << price << endl;
}
in_file.close();
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.twbbs.org)
◆ From: ntumcc66.mba.ntu.edu.tw