C++ c_str 不返回整个字符串

发布于 2024-08-07 06:41:15 字数 799 浏览 5 评论 0原文

我已经使用正常的 ifstream 和当前使用的 boost:iostream 尝试了以下代码,两者都有相同的结果。

它的目的是将文件从 physfs 加载到内存中,然后将其传递给处理程序进行处理(例如图像、音频或数据)。目前,当调用 c_str 时,它仅返回文件的一小部分。

        PhysFS::FileStream file("Resources/test.png" , PhysFS::OM_READ);

    if(file.is_open()) {

        String* theFile;

        theFile = new String((std::istreambuf_iterator<char>(file)), 
        std::istreambuf_iterator<char>());

        String::iterator it;
        for ( it=theFile->begin() ; it < theFile->end(); it++ ) {
            std::cout << *it; 
        } // Outputs the entire file

        std::cout << theFile->c_str(); // Outputs only the first few lines

    }

迭代器循环按预期输出整个 png 文件,但 c_str 调用仅返回前几个字符 (\211PNG)。

我已经尝试此代码的变体相当长一段时间但没有成功。有什么想法吗?

I've tried the following code with both normal ifstreams and the current boost:iostream I'm using, both have the same result.

It is intended to load a file from physfs into memory then pass it to a handler to process (eg Image, audio or data). Currently when c_str is called it only returns a small part of the file.

        PhysFS::FileStream file("Resources/test.png" , PhysFS::OM_READ);

    if(file.is_open()) {

        String* theFile;

        theFile = new String((std::istreambuf_iterator<char>(file)), 
        std::istreambuf_iterator<char>());

        String::iterator it;
        for ( it=theFile->begin() ; it < theFile->end(); it++ ) {
            std::cout << *it; 
        } // Outputs the entire file

        std::cout << theFile->c_str(); // Outputs only the first few lines

    }

The iterator loop outputs the entire png file as expected, but the c_str call only returns the first few characters (\211PNG).

I've been trying variations of this code for quite some time with no success. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

明天过后 2024-08-14 06:41:15

我想象下一个字符是空(ASCII 0)字节。 c_str() 只是给你一个 *char,因此你对 stdout 的写入被解释为以第一个空字节结尾的 C 类字符串。

如果您确实需要该字符串的类似 C 的接口,那么最主要的是 theFile->c_str() 指向您的数据,而 theFile.length 为您提供字符串中的字符数。所以你可能想做这样的事情:

char *c_value = theFile->c_str()
for (int i = 0; i < theFile.length; i++)
{
   cout << c_value[i];
}

真正的解决方案取决于你为什么要首先转换为 char * 。如果您调用仅接受 char * 的旧函数,则该旧函数可能还有一个长度参数。

I imagine that the next character is a null (ASCII 0) byte. c_str() simply gives you a *char, therefore your write to stdout is interpreted as a class C string which ends at the first null byte.

If you really need a C-like interface to this string, the main thing is that theFile->c_str() points to your data and theFile.length gives you the number of characters in the string. So you might want to do something like this:

char *c_value = theFile->c_str()
for (int i = 0; i < theFile.length; i++)
{
   cout << c_value[i];
}

The real solution depends on why you are converting to a char * in the first place. If you are calling a legacy function that only accepts char *, there is likely also a length argument to that legacy function.

岁月静好 2024-08-14 06:41:15

其中一个字节可能是 0。这意味着传递 char* 时要 cout 的字符串结尾(即 c_str )

One of the bytes is probably 0. That means end of string to cout when passing a char* (which c_str is)

百思不得你姐 2024-08-14 06:41:15

我会考虑使用 std::vector 而不是 std::string 。处理向量中的二进制数据要容易得多。如果您需要访问C 风格数组,您可以使用 &vec[0] 引用底层指针。我还要确保您的文件抽象也使用 std::ios_base::binary 作为底层的文件模式。

I would consider using std::vector<unsigned char> instead of std::string for this. It is a lot easier to deal with binary data in a vector. You can reference the underlying pointer using &vec[0] if you need access to a C-style array. I would also make sure that your file abstraction use std::ios_base::binary for the file mode under the hood as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文