.h problem

B

bruceminoo

Hi

I have a problem where I am using VC6. I can normally find solutions
on the web or newsgroups but this one is eluding me probably because
the answer is so simple;


I have developed a program using one .cpp file

The program has grown and I now span a number of .cpp files

I have managed to set up the .h files and the program works with one
important exception


I have a file that I open & leave open for the programs execution


ofstream Cout ("output_log.txt" , ios::eek:ut); // this is a testing log
ie drop << to it

I want to access Cout in the other .cpp files - but I don't know how
to refer to Cout in the appropriate headers.

{
The reason that I am doing this is because I am moving my program from
a console program to a windows enabled program and until I get the
program going I want the convience of having a

cout << "xxxx" << endl;

syntax
}

thanks for any answers

Bruce
 
J

Jonathan Mcdougall

Hi

I have a problem where I am using VC6. I can normally find solutions
on the web or newsgroups but this one is eluding me probably because
the answer is so simple;


I have developed a program using one .cpp file

The program has grown and I now span a number of .cpp files

I have managed to set up the .h files and the program works with one
important exception


I have a file that I open & leave open for the programs execution


ofstream Cout ("output_log.txt" , ios::eek:ut); // this is a testing log
ie drop << to it

I want to access Cout in the other .cpp files - but I don't know how
to refer to Cout in the appropriate headers.

There are several solutions. First, a simple global object:

// log.h
# include <fstream>

extern std::eek:fstream Cout;

// log.cpp
# include "log.h"

std::eek:fstream Cout("output_log.txt", ios::eek:ut);


// test.cpp
# include "log.h"

int main()
{
Cout << "test";
}

This has several problems, such as the order of initialization
(http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.16) and the
fact that global objects are difficult to track. A singleton could help
(google has many good hits).
{
The reason that I am doing this is because I am moving my program from
a console program to a windows enabled program and until I get the
program going I want the convience of having a

cout << "xxxx" << endl;

syntax
}

You *can* redirect std::cout:

int main()
{
std::cout << "On screen";

std::streambuf* original_buffer = std::cout.rdbuf();
std::eek:fstream out("log.txt");
std::cout.rdbuf(out.rdbuf());

std::cout << "In file":

std::cout.rdbuf(original_buffer);

std::cout << "On screen (again)";
}

However, consider using std::clog and std::cerr for logging and error
facilities. It makes more sense to redirect std::clog to a log file
than std::cout, which should semantically send characters to the
standard output.


Jonathan
 
B

bruceminoo

Thank you - I was close

I had tried

# include <fstream>

extern ofstream Cout;


I guess that it comes from the use of "using" statements & not thinking

I will go and have a look at it now

regards
Bruce
 
J

Jonathan Mcdougall

Thank you - I was close
I had tried

# include <fstream>
extern ofstream Cout;

I guess that it comes from the use of "using" statements & not thinking
I will go and have a look at it now

Please do not top-post. Instead, add your reply under the post you are
replying to. This is part of the netiquette in c.l.++ and in Usenet in
general.

Have a look at
http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5
for more informations on namespace std and the namespace directive.


Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top