In addition to creating and opening files, the createfile function can also create console Buffering in the Win32 environment. The Return Value of the createfile function is the Console Buffer handle. View plaincopy to clipboardprint?
Handle console_write = createfile ("conout $", // buffer file on the system console
Generic_read | generic_write, // to read/write the Console Buffer file, which must be opened in read/write mode
File_cmd_read | file_cmd_write, // if multiple threads want to share the output console, set the sharing flag
Null, // file security attribute, null here
Open_existing, // both the output console and the input console must use the open_existing flag.
Null, // ignored Parameter
Null); // ignored Parameter
Handle console_write = createfile ("conout $", // buffer file on the system console
Generic_read | generic_write, // to read/write the Console Buffer file, which must be opened in read/write mode
File_cmd_read | file_cmd_write, // if multiple threads want to share the output console, set the sharing flag
Null, // file security attribute, null here
Open_existing, // both the output console and the input console must use the open_existing flag.
Null, // ignored Parameter
Null); // ignored Parameter
Now we have an available output console. You can use a similar method to get an input console:
View plaincopy to clipboardprint?
Handle console_read = createfile ("conin $", // note that the buffer file in the input console is changed to "conin $"
Generic_read | generic_write, // The following is the same as the output Console
File_pai_read | file_pai_write,
Null,
Open_existing,
Null,
Null );
Handle console_read = createfile ("conin $", // note that the buffer file in the input console is changed to "conin $"
Generic_read | generic_write, // The following is the same as the output Console
File_pai_read | file_pai_write,
Null,
Open_existing,
Null,
Null );
If you already haveProgramYou can use the getstdhandle () function to quickly obtain the console handle. The unique parameter of the getstdhandle function is a DWORD flag. The possible values include:
Std_output_handle // gets the output console handle
Std_input_handle // gets the handle of the input console
Std_error_handle // gets the handle of the error output Console
Now, you can use the input/output console to read and write disk files. You can use the write/readfile function. A better way is to use the write/readconsole function. The explanation given by Microsoft is that the "Write/readfile" function does not support Unicode.
View plaincopy to clipboardprint?
Char * buffer = {"Hello console world! /N "};
Writeconsole (getstdhandle (std_output_handle), // obtain the output console handle
Buffer, // cache of the content to be output
Strlen (buffer), // cache Length
Null, null
);
Char * buffer = {"Hello console world! /N "};
Writeconsole (getstdhandle (std_output_handle), // obtain the output console handle
Buffer, // cache of the content to be output
Strlen (buffer), // cache Length
Null, null
);
The cout and printf functions cannot be used in MFC, which means the formatting of output data is not that convenient. The solution is to use the format function of cstring and then getbuffer. Similarly, the input can only receive strings from the input buffer, and you need to separate the data.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/shwneo/archive/2009/05/10/4165388.aspx