RE: [Dev-C++] first time error return on checking file stream val idity
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: James G. <Jam...@Cl...> - 2001-01-02 14:22:07
|
1st. You should only define your defaults in the declaration of the
function, and not redefine them in the definition of the function.
The reason you are not getting an error the first time you access invalid is
because when it is created the state bits are set to 'good'. After you have
tried to write out the state is not 'bad'. If you were to test and return if
the stream isn't open (is_open() returns true if the file pointer is not a
null pointer).
Possibly change this:
if (!output_file)
{
cout << "Error opening file." << endl;
return 1; // error indication
}
to this
if (!output_file.is_open())
{
cout << "Error opening file." << endl;
return 1; // error indication
}
Regards,
James.
-----Original Message-----
From: Alan Thomas [mailto:ala...@3n...]
Sent: Saturday, December 30, 2000 11:19 PM
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;
}
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
This electronic message and any attachment is intended to be
read by the named addressee(s) only.
Any other recipient should be aware that its contents may be
legally privileged and/or confidential and that its use,
disclosure, copying or distribution may be unlawful.
Unless you are a named addressee, please delete this message
Whilst C. & J. Clark International Limited has taken steps
to prevent the transmission of computer viruses with electronic mail,
responsibility for screening incoming messages and the risk of such
transmission and its consequences lies with the recipient.
C. & J. Clark International Limited
Registered in England and Wales
Company No. 141015
Registered Office: 40 High Street, Street, Somerset BA16 0YA
Telephone: +44 (0) 1458 443131
Fax: +44 (0) 1458 447547
|