我希望能够使用WriteFile或WriteConsole,因此以下三个函数可以工作:
> Output.exe
> Output.exe > File.txt & type File.txt
> for /F "tokens=*" %A in ('Output.exe') do @echo %A我设法让前两个代码使用我的当前代码。但是现在for循环输出:The system cannot write to the specified device.
我想它不喜欢我的BOM。如何使我的代码与for循环一起工作?
这是我目前的密码。
DWORD dwBytesWritten;
DWORD dwConMode;
BOOL bRedirectedToFile;
WCHAR str[] = L"Hello World";
bRedirectedToFile = !GetConsoleMode(hStdOut, &dwConMode);
if (bRedirectedToFile)
{
LONG zero = 0;
DWORD pos = SetFilePointer(hStdOut, 0, &zero, FILE_CURRENT);
if (pos == 0)
{
WORD Unicode = ((BYTE*)L"A")[0] == 0 ? 0xfffe : 0xfeff;
WriteFile(hStdOut, &Unicode, 2, &dwBytesWritten, 0);
}
WriteFile(hStdOut, str, lstrlen(str) * sizeof(WCHAR), &dwBytesWritten, 0);
}
else
{
WriteConsole(hStdOut, str, lstrlen(str), &dwBytesWritten, 0);
}发布于 2013-06-28 15:25:08
我测试了wprintf的输出,它似乎在打印字符串之前将字符串转换为ASCII。
因此,目前可接受的解决办法:
str转换为ASCIIstr打印WriteFile代码也简单得多。
https://stackoverflow.com/questions/17367231
复制相似问题