简体   繁体   English

FPrintf仅打印出CString的第一个字母?

[英]FPrintf CString printing out only the first letter?

I would like to use fprintf to print out some CStrings as well as some more text: 我想使用fprintf打印出一些CString以及更多文本:

CString project = "Project";
FILE * pFile;
CString FileName = project + "_Stats.txt";
pFile = _wfopen(FileName, _T("w"));
fprintf(pFile, "Project Name: %s", project);

The file that is created is called "Project_Stats.txt" and the output should be "Project Name: Project" however I get "Project Name: P". 创建的文件称为“ Project_Stats.txt”,输出应为“ Project Name:Project”,但是我得到的是“ Project Name:P”。 Why does only the first letter get printed out and is there a way I can fix it? 为什么只有第一个字母可以打印出来,有什么办法可以解决?

pm100提供了正确的答案,然后将其删除-UNICODE CString的正确代码是%ls ,而不是%s

fprintf(pFile, "Project Name: %s", project.GetBuffer());

The function fprintf(FILE*, char*, ...) has no idea what type project should have. 函数fprintf(FILE*, char*, ...)不知道项目应该具有哪种类型。 So the compiler will compile anything like a cast to void* . 因此,编译器将编译任何类似强制转换为void* When you want a char* you have to use th GetBuffer() member of CString. 当需要char* ,必须使用CString的GetBuffer()成员。

This happens because CString is in UNICODE, so to write ANSI text to your file first convert it to multi byte, ie: 发生这种情况是因为CString在UNICODE中,所以要先将ANSI文本写入文件,请将其转换为多字节,即:

CStringA projectA(project);

and then: 接着:

fprintf(pFile, "Project Name: %s", projectA.GetString());

Try 尝试

fprintf(pFile, "Project Name: %s", (const char_t*)project);

or 要么

fprintf(pFile, "Project Name: %s", (const wchar_t*)project);

see this topic for further info http://msdn.microsoft.com/en-us/library/72b2swax.aspx 请参阅本主题以获取更多信息http://msdn.microsoft.com/zh-cn/library/72b2swax.aspx

I don't have MFC now, but in Win32++ the following works properly (I have 1252 codepage, so Russian chars are not in 1252 codepage, also cpp file is saved as UTF-8 with BOM): 我现在没有MFC,但是在Win32 ++中,以下功能可以正常工作(我具有1252代码页,因此俄语字符不在1252代码页中,而且cpp文件也用BOM保存为UTF-8):

Win32xx::CString project{L"Z:\\Projectцц"};
FILE * pFile;
Win32xx::CString FileName{project + L"_Staпрпфts.txt"};
pFile = _wfopen(FileName.GetBuffer(FileName.GetLength()), _T("w, ccs=UTF-16LE"));
fwprintf(pFile, L"Project Name: %s", project.GetBuffer(project.GetLength()));

As a note never mix Unicode and ANSI methods in Win32. 注意,切勿在Win32中混合使用Unicode和ANSI方法。 This also refers to string literals, use _T macro or just always make project Unicode and use L"" for sting literals. 这也指的是字符串文字,使用_T宏,或者仅始终使项目Unicode并使用L“”作为字符串文字。
Maybe is better to get used to something like boost::filesystem (which will be included in next standard) for file path handling and C++ streams for formatting. 也许最好习惯使用boost :: filesystem之类的东西(将在下一个标准中包括)来处理文件路径,并使用C ++流来进行格式化。 One thing to remember is that proper Unicode handling in Windows is a trial and error method, until you get it right. 要记住的一件事是,在Windows中正确处理Unicode是一种反复试验的方法,直到正确为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM