无法将字符串输出到std :: wofstream(g ++) [英] Can't output strings to std::wofstream (g++)

查看:101
本文介绍了无法将字符串输出到std :: wofstream(g ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在这里磕磕绊绊地想要了解

宽/ Unicode功能。我一般都很喜欢C ++,流媒体的东西

没那么多。


如果我运行以下代码,我得到一个零长度的文件。如果我拿出

*两个*字符串输出,我按预期得到''123abc''。 ''上一个错误''始终为0,

我没有得到任何调试错误输出。


我做错了什么/非常愚蠢? Linux上的g ++ 3.4.4。

#include< iostream>

#include< fstream>

#include< errno.h> ;


int main()

{

std :: string jobby =" Hello \\\wotsit\ u201D \\ \\ n" ;;

std :: wstring wjobby = L" Hello \\\wotsit \ u201D \ n";


std :: wofstream文件( " jobby.out");

if(!file.good())std :: cerr<< 打开错误\ n;


file<< 123;

if(!file.good())std :: cerr<< 写错误1 \ n;


file<< abc;

if(!file.good())std :: cerr<< 写错误2 \ n;


file<< jobby.c_str()<< "普通字符10是'' << jobby [10]<< "''\ n";

if(!file.good())std :: cerr<< 写入错误3 \ n;


file<< wjobby<< L"宽字符10是''' << wjobby [10]<< L"''\ n";

if(!file.good())std :: cerr<< 写入错误4 \ n;


file.close();

std :: cerr<< \\\
Last error << errno<< " \ n";


返回0;

}

-

= ======================= - http://www.thalesgroup.com/ - =======================

Neil Bird校长工程师|答:是的

mailto:ne ******* @ uk.thalesgroup.no.spam.please |问:上贴不好吗?

请用''com''替换''no.spam.please''直接回复。


- = - = -

.... APL是一种只写语言。 - Roy Keir


I''m stumbling around a little bit here trying to get a feel for
wide/Unicode functionality. I''m pretty up on C++ in general, streaming stuff
not so much.

If I run the following code, I get a zero length file. If I take out
*both* string outputs, I get ''123abc'' as expected. ''Last error'' is always 0,
I get none of the debug error output.

Am I doing something wrong/obvisouly stupid? g++ 3.4.4 on Linux.
#include <iostream>
#include <fstream>
#include <errno.h>

int main()
{
std::string jobby = "Hello \u201Cwotsit\u201D\n";
std::wstring wjobby = L"Hello \u201Cwotsit\u201D\n";

std::wofstream file("jobby.out");
if (!file.good()) std::cerr << "Open error\n";

file << 123;
if (!file.good()) std::cerr << "Write error 1\n";

file << "abc";
if (!file.good()) std::cerr << "Write error 2\n";

file << jobby.c_str() << " normal char 10 is ''" << jobby[10] << "''\n";
if (!file.good()) std::cerr << "Write error 3\n";

file << wjobby << L" wide char 10 is ''" << wjobby[10] << L"''\n";
if (!file.good()) std::cerr << "Write error 4\n";

file.close();
std::cerr << "\nLast error " << errno << "\n";

return 0;
}
--
========================- http://www.thalesgroup.com/ -=======================
Neil Bird Principal Engineer | A: Yes
mailto:ne*******@uk.thalesgroup.no.spam.please | Q: Is top-posting bad?
Please replace ''no.spam.please'' with ''com'' to reply directly.

-=-=-
.... APL is a write-only language. - Roy Keir

推荐答案

FNX写道:

我在这里试图磕磕绊绊感受
宽/ Unicode功能。我很擅长C ++,流媒体不是那么多。

如果我运行以下代码,我得到一个零长度的文件。如果我取出两个*字符串输出,我按预期得到''123abc''。 ''上一个错误''总是为0,我没有得到任何调试错误输出。

我做错了什么/看起来很愚蠢? Linux上的g ++ 3.4.4。

#include< iostream>
#include< fstream>
#include< errno.h>
int main()
{
std :: string jobby =" Hello \\\wotsit \ u201D \ n";
std :: wstring wjobby = L" Hello \\\ 2020Cwotsit \ u201D \ n";

std :: wofstream文件(" jobby.out");
if(!file.good())std :: cerr<< ; 打开错误\ n;

文件<< 123;
if(!file.good())std :: cerr<< 写入错误1 \ n;

文件<< " abc" ;;
if(!file.good())std :: cerr<< 写入错误2 \ n;

文件<< jobby.c_str()<< "普通字符10是'' << jobby [10]<< "''\ n";
if(!file.good())std :: cerr<< 写错误3 \ n;

文件<< wjobby<< L"宽字符10是''' << wjobby [10]<< L"''\ n";
if(!file.good())std :: cerr<< 写入错误4 \ n;

file.close();
std :: cerr<< \\\
Last error << errno<< " \ n";

返回0;
}

I''m stumbling around a little bit here trying to get a feel for
wide/Unicode functionality. I''m pretty up on C++ in general, streaming
stuff not so much.

If I run the following code, I get a zero length file. If I take out
*both* string outputs, I get ''123abc'' as expected. ''Last error'' is
always 0, I get none of the debug error output.

Am I doing something wrong/obvisouly stupid? g++ 3.4.4 on Linux.
#include <iostream>
#include <fstream>
#include <errno.h>

int main()
{
std::string jobby = "Hello \u201Cwotsit\u201D\n";
std::wstring wjobby = L"Hello \u201Cwotsit\u201D\n";

std::wofstream file("jobby.out");
if (!file.good()) std::cerr << "Open error\n";

file << 123;
if (!file.good()) std::cerr << "Write error 1\n";

file << "abc";
if (!file.good()) std::cerr << "Write error 2\n";

file << jobby.c_str() << " normal char 10 is ''" << jobby[10] << "''\n";
if (!file.good()) std::cerr << "Write error 3\n";

file << wjobby << L" wide char 10 is ''" << wjobby[10] << L"''\n";
if (!file.good()) std::cerr << "Write error 4\n";

file.close();
std::cerr << "\nLast error " << errno << "\n";

return 0;
}




Sun的CC说\ u201是一个时代错误。我不知道你在做什么

在这里做。但是,如果我删除你,即使用\ 201,我得到一个有效的输出。


HTH,

Tom



Sun''s CC says that \u201 is an Anachronism. I don''t know what you are
doing here. But if I remove the u i.e. use \201, I get a valid output.

HTH,
Tom


''\ u201C''是Unicode代码点201C(开头报价)。这是合法的

,不要与''\ 0201''+'''C'混淆。因此


" Hello \\\wotsit \ u201D \ n"



Hello" wotsit"


不是

您好?Dwotsit?E $ />

Dunno为什么Sun CC与它没有任何关系并且它与输出失败没有相关(我认为)。

''\u201C'' is Unicode code-point 201C (opening quote). That''s legit
and not to be confused with ''\0201'' + ''C''. Thus

"Hello \u201Cwotsit\u201D\n"
is
Hello "wotsit"

not
Hello ?Dwotsit?E

Dunno why Sun CC isn''t having anything to do with it, and it''s not
relevant (I think) to the failure to output.


我认为在C ++中你是不能使用\ U来表示unicode字符。


使用\ x201C。


仅在wstring中使用unicode,因为string是char *,所以显然不能

分配unicode值或在字符串中使用\ x201C的多字节流

I think in C++ u cannot use \U to represent unicode characters.

Use \x201C.

Use unicode only in wstring as string is char* , so obviouly cannot
assign unicode value or use multibyte streams of \x201C in string


这篇关于无法将字符串输出到std :: wofstream(g ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆