我有const void*形式的Base64编码数据。当我使用writeFile()函数将数据写入文件时,数据将以正确的格式写入。但是,当我尝试使用std::cout打印它时(直接打印,或在将其键入为char*),格式不同/不需要。因此,我更改了传递给writeFile()的句柄,以便将正确的输出打印到控制台,结果成功了。
有什么方法可以保留/重定向字符串(或char数组)中的等效数据吗?我尝试搜索句柄,以便使用writeFile()将输出写入字符串变量,但到目前为止还没有成功。代码如下:void Write(const void* data, int size)
{
/* filePath is a local variable here */
HANDLE mFile = CreateFileA(filePath, FILE_APPEND_DATA, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if ((mFile != NULL) && (size > 0))
{
std:: cout << data; // incorrect/undesired output
char* stringData = (char *)data;
std::cout << stringData; // incorrect/undesired output
DWORD bytesWritten = 0;
/* Writes output to the file */
WriteFile(mFile, data, static_cast(size), &bytesWritten, NULL);
/* Writes output to the console */
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
WriteFile(hStdOut, data, static_cast(size), &bytesWritten, NULL);
}
}
WriteFile()函数:BOOL WINAPI WriteFile(
_In_ HANDLE hFile,
_In_ LPCVOID lpBuffer,
_In_ DWORD nNumberOfBytesToWrite,
_Out_opt_ LPDWORD lpNumberOfBytesWritten,
_Inout_opt_ LPOVERLAPPED lpOverlapped
);