Hello Alan,
/* I don't mean to be ignoring Ioannis' reply. The way the thread has
progressed has made it simpler to refer to the initial message.
My comments are liberally sprinkled throughout.
Caution - lots of code snipping / reformatting taking place. */
> 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
>
> int main()
> {
> ofstream invalid; // used to test for error opening file
At this point, invalid is a perfectly good ofstream object. No error
conditions to report.
if (invalid) cout << "This line will display."
>
> // 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) {
The first time this function is called, output_file is still fine, no
errors to report.
> if (!output_file)
// So this yields 'false'...no error.
> {
> cout << "Error opening file." << endl;
> return 1;
> }
> output_file << "html header";
Now that you have tried to output to an ofstream object that is not
associated with a file, error conditions are now set.
At this point if you test it...
if (output_file) cout << "This will not be displayed.";
> return 0;
> }
If the above doesn't make much sense, try running the following
(using the appropriate headers), it might make things clearer.
int main(){
ofstream invalid;
if (!invalid) cout << "this won't print";
invalid << "attempt output without a filehandle";
if (!invalid) cout << "now we have an error condition.";
return 0;
}
What you can do is instead of simply testing for whether any errors
are being reported, change your initial test in the function call to
something like:
if ( !output_file.is_open() )
keeping the rest the same.
I would suggest grabbing some books (even if it's just from the
library) as on-line tutorials tend to omit a lot of the detail that is
required.
--
regards,
Nick
|