#### Archived

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

# Using console window as log entry

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

## 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 on other sites
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 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 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 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 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 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 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 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

1. 1
2. 2
frob
16
3. 3
4. 4
5. 5
Rutin
10

• 18
• 13
• 14
• 76
• 22
• ### Forum Statistics

• Total Topics
632149
• Total Posts
3004364

×