[Dev-C++] first time error return on checking file stream validity
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Alan T. <ala...@3n...> - 2000-12-30 23:21:46
|
The code below gives some interesting results. First return from the
function start_HTML_file is 0 (no error), while the second time it =
returns 1
(could not open file). What does this mean?
Thanks, Alan
#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>
#include <string.h>
// define HTML author
const char *Author =3D "Alan Thomas";
//
// function prototype
//
int start_HTML_file (ofstream&output_file, char *keywords =3D NULL,
char *title =3D NULL);
int main()
{
ofstream invalid; // used to test for error opening file
// Test invalid file checking
cout << "Error return was " << start_HTML_file (invalid, NULL,
"This is the title") << endl;
cout << "Error return was " << start_HTML_file (invalid, NULL,
"This is the title") << endl;
system("PAUSE");
return 0;
}
int start_HTML_file (ofstream&output_file, char *keywords =3D NULL, char
*title =3D NULL)
{
if (!output_file)
{
cout << "Error opening file." << endl;
return 1; // error indication
}
// Write header information
output_file << "<!doctype html public \"-//w3c//dtd html 4.0
transitional//en\">";
output_file << endl;
output_file << "<html>" << endl;
output_file << "<head>" << endl;
output_file << "<meta http-equiv=3D\"Content-Type\" =
content-\"text/html;
charset=3Diso-8859-1\"";
output_file << endl;
output_file << "<meta name=3D\"Author\" content=3D\"" << Author << =
"\">" <<
endl;
if (keywords !=3D NULL)
{
output_file << "<meta name=3D\"KeyWords\" content=3D\"";
output_file << keywords << ">\"" << endl;
}
// Write out title
if (title !=3D NULL)
{
output_file << "<title>";
output_file << title;
output_file << "</title>" << endl;
}
// End header
output_file << "</head>" << endl;
return 0;
}
|