• Advertisement

Archived

This topic is now archived and is closed to further replies.

Using console window as log entry

This topic is 5503 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Heres intresting code snipped from one book. It create console windows and when u use printf all the text apears in that console.
  
AllocConsole();

freopen("CONOUT$", "a", stdout); // redirect printf to console

printf("test");
// code works in window applications only.

  
Ok its really useful hope u find it usefull too but i dont know how it actualy works becose the documents dont say anything about "CONOUTS$" or stdout what are these? Well using the file stream to stuff like this it self is mystery to me

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by Craazer
... "CONOUTS$" or stdout what are these?



Here''s a stab of an answer:

Those are parts of what''s known as Standard IO. Keyboard in, printer out (from back in the days before monitors).

The thinking behind these handles comes to Windows from Unix where handle redirection has been employed for 20 years.
The freopen function effects a redirection. Redirection allows for the output of one program to become the input for another program. A user typing a command at the prompt typically think of this redirection as "piping". In DOS piping was indicated by stringing together command with |, > and <.

For example if you want to store a directory listing in a text file you could pipe the output to a file instead of to the console:

dir > dirlist.txt

This dos command is still very convenient.

Complicating the matter is that Microsoft wrote it''s own version of this into NT and so there are multiple representations for the standard handles - however - in most cases the MS handles do work with MS file io API''s in the same way that fprintf and fscanf will work with stdout and stdin. But they can''t be mixed and matched without conversion - a HANDLE is not the same as a FILE.

stdout (Standard Output) is a handle that can be written to as if it was a file stream, "CONOUTS$" is a way of representing that handle.

freopen maps the representation of the MS std handle ("CONOUT$") to the more traditional stdout.

printf is the equivalent of calling fprintf passing the stdout handle instead of a file handle.

int i = 12;
printf("%d", i);
fprintf(stdout, "%d", i);

stdout directs the string to the console subsystem where it is displayed.

There is also stdin and stderr. stdin is used to read input entered at the console prompt. The relationship between scanf and fscanf is similar to printf and fprintf.


hhh













"Beautiful maiden," answered Candide, "when a man is in love, is jealous, and has been flogged by the Inquisition, he becomes lost to all reflection."

Share this post


Link to post
Share on other sites
quote:
Original post by LessBread
[quote]Original post by Craazer
... "CONOUTS$" or stdout what are these?



Here''s a stab of an answer:

Those are parts of what''s known as Standard IO. Keyboard in, printer out (from back in the days before monitors).

The thinking behind these handles comes to Windows from Unix where handle redirection has been employed for 20 years.
The freopen function effects a redirection. Redirection allows for the output of one program to become the input for another program. A user typing a command at the prompt typically think of this redirection as "piping". In DOS piping was indicated by stringing together command with |, > and <.

For example if you want to store a directory listing in a text file you could pipe the output to a file instead of to the console:

dir > dirlist.txt

This dos command is still very convenient.

Complicating the matter is that Microsoft wrote it''s own version of this into NT and so there are multiple representations for the standard handles - however - in most cases the MS handles do work with MS file io API''s in the same way that fprintf and fscanf will work with stdout and stdin. But they can''t be mixed and matched without conversion - a HANDLE is not the same as a FILE.

stdout (Standard Output) is a handle that can be written to as if it was a file stream, "CONOUTS$" is a way of representing that handle.

freopen maps the representation of the MS std handle ("CONOUT$") to the more traditional stdout.

printf is the equivalent of calling fprintf passing the stdout handle instead of a file handle.

int i = 12;
printf("%d", i);
fprintf(stdout, "%d", i);

stdout directs the string to the console subsystem where it is displayed.

There is also stdin and stderr. stdin is used to read input entered at the console prompt. The relationship between scanf and fscanf is similar to printf and fprintf.


hhh













"Beautiful maiden," answered Candide, "when a man is in love, is jealous, and has been flogged by the Inquisition, he becomes lost to all reflection."


Ahh I see, thank you LessBread!!


Share this post


Link to post
Share on other sites
There''s actually a function you can use to open a stdio FILE* using a Win32 HANDLE

it''s purty neat.

Share this post


Link to post
Share on other sites
Can you redirect using cout instead? For example if I do:

cout << "Some error text." << endl;

Could I redirect that to the console instead of using printf?

Thanks

Share this post


Link to post
Share on other sites
quote:
Original post by stodge
Can you redirect using cout instead? For example if I do:

cout << "Some error text." << endl;

Could I redirect that to the console instead of using printf?

Thanks


I believe that "cout" uses "stdout" - so Craazer''s example could be followed up like so

AllocConsole();
freopen("CONOUT$", "a", stdout);
cout << "Some error text." << endl;

Share this post


Link to post
Share on other sites
Are you sure LessBread? I though they used two different methods of writing to standard out. I''m probably wrong though.

Share this post


Link to post
Share on other sites
quote:
Original post by stodge
Are you sure LessBread? I though they used two different methods of writing to standard out. I''m probably wrong though.


Oddly enough, for me, I was able to use std::cout, but not printf(). The console showed up alright but it printed nothing using the printf() function....This should prove useful as hell in the debugging process.

_____________________________

And the Phoenix shall rise from the ashes...

--Thunder_Hawk -- ¦þ
______________________________

Share this post


Link to post
Share on other sites
Oooh, creative thought processes running...Is it possible to redirect to two different locations? I know it seems a bit redundant, but there should be a way even if though some overloaded operators on my part, right?

_____________________________

And the Phoenix shall rise from the ashes...

--Thunder_Hawk -- ¦þ
______________________________

Share this post


Link to post
Share on other sites
Ok - I don''t know exactly what goes on inside std::cout - whether the line is sent to a HANDLE or a FILE * is likely OS dependent. As far as the C standard goes - stdout doesn''t exist. Obviously, that''s not so in the C++ standard.

To learn more on the subject of console redirection, google on "Standard Input/Output", "Console", "Window Console", "Win 32 Console Subsystem", "Pipes", "DOS", "DOS Batch Files", "Console Redirection" and related topics that you come across along the way.

There are a couple of "DOS Dump" related tips of the day at flipcode.com - and it shouldn''t be too difficult to find several public domain "console log entry" libraries as well.




"It is always a simple matter to drag the people along, whether it is a democracy, or a fascist dictatorship, or a parliament, or a communist dictatorship. Voice or no voice, the people can always be brought to the bidding of their leaders. That is easy. All you have to tell them is that they are being attacked and denounce the peacemakers for lack of patriotism and exposing the country to danger. It works the same in any country."
Hermann Goering

Share this post


Link to post
Share on other sites
ThunderHawk: yes, if you use std C++ streams - which I assume you do sincec you talk about operator<>, there are ways to redirect / forward an stream so you can have debug out to both a HTML logging file and a console log out for example. It's all with stream buffers and so on.

The 'correct' way to make cout work is to write a stream buffer class that outputs to the console, then do a .rdbuf() on cout.

Such code can be found in libGDN, as well as color manipulators (red = bad error, green = all is good, amber = possible problems...)
It's all in the /io directory - streams.h, consolestream.h, I think is what you need to look at.

If you don't like that, you can have a google at "stream buffers" and stream_buf (the class). Then look at rdbuf() in ..istream and ostream I think...

EDIT: Some links:
consolestream.h and
streams.h



-----------------------------
Gamedev for learning.
libGDN for putting it all together.

[edited by - risingdragon3 on January 25, 2003 10:49:05 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by LessBread
As far as the C standard goes - stdout doesn''t exist. Obviously, that''s not so in the C++ standard.
<hr>
"It is always a simple matter to drag the people along, whether it is a democracy, or a fascist dictatorship, or a parliament, or a communist dictatorship. Voice or no voice, the people can always be brought to the bidding of their leaders. That is easy. All you have to tell them is that they are being attacked and denounce the peacemakers for lack of patriotism and exposing the country to danger. It works the same in any country."
Hermann Goering



I like the quote, but stdout and freopen() are part of C.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
I like the quote, but stdout and freopen() are part of C.


I think more of a "defacto" existance - at least that''s what I gather from reading many-many posts at comp.lang.c that cried over questions about FILE pointers... however, I''ll admit that I didn''t refer to the standard before making the assertion.



"It is always a simple matter to drag the people along, whether it is a democracy, or a fascist dictatorship, or a parliament, or a communist dictatorship. Voice or no voice, the people can always be brought to the bidding of their leaders. That is easy. All you have to tell them is that they are being attacked and denounce the peacemakers for lack of patriotism and exposing the country to danger. It works the same in any country."
Hermann Goering

Share this post


Link to post
Share on other sites

  • Advertisement