- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
wide char printf 单个字符发生了什么? VS10 和 MCBS:
#include<stdio.h>
#include <windows.h>
int const maxPathFolder = MAX_PATH - 3;
wchar_t const *delims = L"T";
wchar_t *testString = L"Codepage is: ";
int main()
{
FILE *stream = NULL;
UINT CP = GetConsoleOutputCP();
wchar_t *testName= (wchar_t *)calloc(maxPathFolder, sizeof(wchar_t));
wcscat_s(testName, maxPathFolder, L"C:\\printemp.txt");
stream = _wfopen(testName, L"w");
if (fwprintf(stream, L"%s%i%c", testString, CP, delims) == EOF) wprintf(L"Problems writing to File.");
fclose (stream);
swprintf (testName, L"%s%i%c", testString, CP, delims);
free (testName);
}
printemp.txt 中的输出是 Codepage is: 850?
swprintf'd testName
中的 delims 变量是 Han character 坠 .根据 Igor 在 this post 中的评论, 宽阔的溪流看起来有点 splinter 。
最终目的是将宽字符数组输出到文件,文件由分隔符分隔。有办法绕过它吗?
最佳答案
代码页大部分已经过时,Unicode 取而代之。这里的问题和之前一样,试图以Text/ANSI模式打开Unicode文件。
由于您已将其标记为 c++,因此您可以只使用标准库、std::wstring
和 std::wfstream
,避免令人头疼的 c 字符串分配。
#include <iostream>
#include <fstream>
#include <string>
#include <io.h> //for _setmode
#include <fcntl.h> //for _O_U16TEXT
int main()
{
//optional: for non-local languages on console
_setmode(_fileno(stdout), _O_U16TEXT);
//write to file (overwrite old file if any)
wchar_t wbuf[128];
std::wofstream fout(L"path.txt", std::ios::binary);
if (fout)
{
fout.rdbuf()->pubsetbuf(wbuf, 128);
fout << L"ελληνικά\n";
fout << L"English\n";
fout << 123 << "\n";
fout.close();
}
std::wifstream fin(L"path.txt", std::ios::binary);
if (fin)
{
fin.rdbuf()->pubsetbuf(wbuf, 128);
std::wstring wstr;
while (getline(fin, wstr, L'\n')) std::wcout << wstr << L"\n";
fin.close();
}
return 0;
}
为了与记事本等其他软件兼容,您必须在文件开头添加字节顺序标记:
fout << L"\xFEFF";
然后在读取文件时跳过第一个字符(前2个字节)。
如果 std::wstring
不是一个选项,则使用 new
/delete
运算符而不是 malloc
。
wchar_t *testName = new wchar_t[MAX_PATH];
...
delete[] testName;
关于c++ - swprintf 和 fwprintf 以及 %c 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34921704/
我正在尝试使用 RtlStringCbPrintfW (swprintf 的安全版本),并在将 int 附加到字符串时得到意外结果。 如果我在 int 之后附加相同的字符串 - 一切正常。所以我的代码
求助!我对 C 语言一无所知,但我必须对 C 代码做一点补充。 我从 GetLastError 得到一个错误号 (int) 并将它显示到一个表单中。相反,我需要获取实际的错误字符串。 我添加了对 Fo
以下代码行在 Visual Studio 2010 中产生垃圾: swprintf(buf, L"Value is %s", "abcd"); 但是,相同的代码在 Linux 上运行良好。 通过试用,
我在 Windows 上使用 MinGW g++ 编译器(gcc 版本 4.8.1),我对 swprintf 声明感到困惑 根据 this reference应该是 int swprintf (wch
我正在修复在 linux 和 windows 上运行的遗留代码,在某些情况下,本应包含格式化内容的缓冲区小于该内容。 代码使用根据 documentation 的 swprintf size - up
我正在尝试从 javascript 执行此操作 swprintf(hexResult, L"%I64X", result);,这可能吗?这样 CityHash64('C:\Program Files
还有人遇到过这样的问题吗? swprintf_s(v, len, L"%ws", var); MessageBox(NULL, v, NULL' NULL); 消息框打印出“%ws”而不是var的内容
在调用swprintf 将char 转换为wchar 后,调用循环会触发段错误;循环 header 中的语句在循环上下文之外工作正常。 swprintf 命令基于返回值 (3, which is th
这发生在 OS X 上,但我怀疑它适用于任何 UNIX-y 操作系统。我有两个如下所示的字符串: const wchar_t *test1 = (const wchar_t *)"\x44\x00\x
我正在尝试使用 MinGW 在 Windows 上编译 Botan,但在编译期间收到以下错误: c:\qt\2010.04\mingw\bin\../lib/gcc/mingw32/4.4.0/inc
我尝试使用 swprintf(buff, 4, L"%#03x", value),但是当 value = 0 时,我得到 000 而不是 0x0。 最佳答案 您需要字面值中的“0x”,例如“0x%03
大部分时间我在 Android 上使用 UTF-8,但我最近也在测试 wchar_t。我知道 wchar_t 在早期很糟糕,但我不关心太旧的设备,所以认为现在使用 wchar_t 会很好。 测试于:
以下代码行与 Visual Studio 2013 一起运行: wchar_t test[] = L"\xffff"; wchar_t buf[100]; int ret = swprintf(
到目前为止,我已经在 visual studio 中编写了所有代码,现在我需要向其中添加一些 UI,因此我将使用 Qt。所以我在我的项目中添加了每个文件(主类除外),然后尝试使用 Qt 编译它。因为我
wide char printf 单个字符发生了什么? VS10 和 MCBS: #include #include int const maxPathFolder = MAX_PATH - 3;
我正在尝试将 char* 字符串转换为 wchar_t*。我看到这个问题已经被问了很多次,但没有解决/可移植的答案/解决方案。 按照建议here , swprintf 对我来说似乎是正确的解决方案,但
以下几行都“按预期工作”: wchar_t u[50], v[50]; swprintf(u, 50, L"%s", L"hello"); swprintf(v, 50, L"%ls", L"good
我正在编写一个 Excel 插件,需要生成 wchar_tExcel 的输出(尽管在内部,我们是 100% char,并且实际上将 char 限制为纯 ASCII)。有一次,我正在使用swprintf
我有一个多平台项目,它在 mac 上编译得很好,但是在 Windows 上,我所有带有 %s 的 swprintf 调用都在寻找 wchar_t 而不是 char * 我通过它。结果 M$ 认为在宽字
在尝试转换一些现有代码以支持 unicode 字符时,突然出现了这个问题。如果我尝试将 unicode 字符(在本例中使用欧元符号)传递给任何 *wprintf 函数,它将失败,但似乎只在 xcode
我是一名优秀的程序员,十分优秀!