为什么std :: wofstream不将所有wstring打印到文件中? [英] why std::wofstream do not print all wstring into file?

查看:259
本文介绍了为什么std :: wofstream不将所有wstring打印到文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个std::wstring,其大小为139,580,199个字符.

I have a std::wstring whose size is 139,580,199 characters.

为了调试,我使用以下代码将其打印到文件中:

For debugging I printed it into file with this code:

std::wofstream f(L"C:\\some file.txt");
f << buffer;
f.close();

此后,发现字符串末尾丢失.创建的文件大小为109,592,584字节(磁盘上的大小"为109,596,672字节).

After that noticed that the end of string is missing. The created file size is 109,592,584 bytes (and the "size on disk" is 109,596,672 bytes).

还要检查缓冲区是否包含空字符,是否这样做:

Also checked if buffer contains null chars, did this:

size_t pos = buffer.find(L'\0');

期望结果为std::wstring::npos,但是它为18446744073709551615,但是我的字符串末尾没有空字符,因此可能没问题.

Expecting result to be std::wstring::npos but it is 18446744073709551615, but my string doesn't have null char at the end so probably it's ok.

有人可以解释一下,为什么我没有将所有字符串都打印到文件中吗?

Can somebody explain, why I have not all string printed into file?

推荐答案

很大程度上取决于语言环境,但是通常情况下,磁盘上的文件会 不使用与以下相同的编码形式(甚至相同的编码) wchar_t使用的代码; filebuf实际 读写根据其编码进行翻译 充满语言环境.而且之间只有模糊的关系 不同编码或编码形式的字符串的长度. (而且系统看到的尺寸与 您可以从文件中读取的字节数.)

A lot depends on the locale, but typically, files on disk will not use the same encoding form (or even the same encoding) as that used by wchar_t; the filebuf which does the actual reading and writing translates the encodings according to its imbued locale. And there is only a vague relationship between the length of a string in different encodings or encoding form. (And the size the system sees doesn't correspond directly to the number of bytes you can read from the file.)

要查看是否所有内容均已写入,请检查f的状态 收盘后,即:

To see if everything was written, check the status of f after the close, i.e.:

f.close();
if ( !f ) {
    //  Something went wrong...
}

可能出错的一件事是外部编码 没有其中一个字符的表示.如果 您在"C"语言环境中,任何字符都可能发生 在基本执行字符集之外.

One thing that can go wrong is that the external encoding doesn't have a representation for one of the characters. If you're in the "C" locale, this could occur for any character outside of the basic execution character set.

如果上面没有错误,则没有理由立即采取行动 并非所有的字符串都已被写入.如果发生什么情况 您尝试在另一个程序中阅读它?你也一样吗 字符数吗?

If there is no error above, there's no reason off hand to assume that not all of the string has been written. What happens if you try to read it in another program? Do you get the same number of characters or not?

对于其他字符,nul字符与 std::wstring;他们没有什么特别的,包括 当它们输出到流中时.和18446744073709551615 看起来非常像我期望的价值 std::wstring::npos在64位计算机上.

For the rest, nul characters are characters like any others in a std::wstring; there's nothing special about them, including when they are output to a stream. And 18446744073709551615 looks very much like the value I would expect for std::wstring::npos on a 64 bit machine.

关注Mat Petersson的评论:实际上这是非常重要的 文件结束时的字节数少于字节数的可能性不大 std::wstring中的代码点. (std::wstring::size() 返回代码点的数量.) 字节,而不是std::wstring::size()返回的内容.所以 最有可能的爆炸是,您在其中有一些角色 您的字符串,这些字符串在目标编码中无法表示 (它可能仅支持带代码点的字符 32-126,默认情况下还有一些控制字符.

Following up on Mat Petersson's comment: it's actually highly unlikely that the file ends up with less bytes than there are code points in the std::wstring. (std::wstring::size() returns the number of code points.) I was thinking in terms of bytes, not in terms of what std::wstring::size() returns. So the most likely explination is that you have some characters in your string which aren't representable in the target encoding (which probably only supports characters with code points 32-126, plus a few control characters, by default).

这篇关于为什么std :: wofstream不将所有wstring打印到文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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