#include <fstream.h>
#include <stdlib.h>
const int MAXCHARS = 21;
char fname[MAXCHARS] = "list.dat"; // here is the file we are working with
void in_out(ofstream&); // function prototype
int main()
{
ofstream out_file;
out_file.open(fname);
if (out_file.fail()) // check for a successful open
{
cout << "\nThe output file " << fname << " was not successfully opened"
<< endl;
exit(1);
}
in_out(out_file); // call the function
return 0;
}
void in_out(ofstream& file_out)
{
const int LINELEN = 80; // longest length of a line of text
const int NUMLINES = 5; // number of lines of text
int count;
char line[LINELEN]; // enough storage for one line of text
cout << "Please enter five lines of text:" << endl;
for (count = 0; count < NUMLINES; count++)
{
cin.getline(line,LINELEN,'\n');
file_out << line << endl;
}
return;
}
--
※ 發信站: 批踢踢實業坊(ptt.twbbs.org)
◆ From: ntumcc66.mba.ntu.edu.tw