I have the impression you do not create or open any file, e.g. std::ofstream
invalid("test.txt");
I do not know much about C++ file operations yet but i think this is the
problem.
Ioannis
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...]On Behalf Of Alan Thomas
Sent: Sunday, December 31, 2000 1:19 AM
To: dev...@li...
Subject: [Dev-C++] first time error return on checking file stream
validity
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 = "Alan Thomas";
//
// function prototype
//
int start_HTML_file (ofstream&output_file, char *keywords = NULL,
char *title = 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 = NULL, char
*title = 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=\"Content-Type\" content-\"text/html;
charset=iso-8859-1\"";
output_file << endl;
output_file << "<meta name=\"Author\" content=\"" << Author << "\">" <<
endl;
if (keywords != NULL)
{
output_file << "<meta name=\"KeyWords\" content=\"";
output_file << keywords << ">\"" << endl;
}
// Write out title
if (title != NULL)
{
output_file << "<title>";
output_file << title;
output_file << "</title>" << endl;
}
// End header
output_file << "</head>" << endl;
return 0;
}
|