#include <fstream.h>
#include <stdlib.h>
const int MAXCHARS = 21;
const int NUMLINES = 5; // number of lines
const int LINELEN = 80; // maximum line length
int get_open(ofstream&); // pass a reference to an ofstream
void in_out(ofstream&); // pass a reference to an ofstream
int main()
{
ofstream out_file; // file1 is an fstream object
get_open(out_file); // open the file
in_out(out_file); // write to it
}
int get_open(ofstream& file_out)
{
char name[MAXCHARS];
cout << "\nEnter a file name: " << endl;
cin.getline(name,MAXCHARS,'\n');
file_out.open(name); // open the file
if (file_out.fail()) // check for successful open
{
cout << "Cannot open the file" << endl;
exit(1);
}
else
return 0;
}
void in_out(ofstream& file_out)
{
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