相关疑难解决方法(0)

为什么这个专门用于basic_ifstream模板的char_traits <uint8_t>和codecvt <uint8_t>会抛出std :: bad_cast?

目前 已经 问题在这里问#1 为什么 basic_fstream<uint8_t>不工作.答案说这char_traits只是专门用于charwchar_t(加上char16_t,char32_t在C++ 11中),你应该坚持basic_fstream<char>阅读二进制数据并在需要时投射它.

好吧,这还不够好!:)

没有任何答案(我能找到)说明如何专门化char_traits<uint8_t>和使用basic_fstream模板,或者甚至可能.所以我想我会尝试自己实现它.

在Windows 7 64位上使用Visual Studio Express 2013 RC并在Kubuntu GNU/Linux 13.04 64位上使用g ++ - 4.7时,以下编译没有错误.但是它会在运行时抛出std :: bad_cast异常.我没有访问clang ++和libc ++来测试这个组合.

#include <cinttypes>
#include <cstring>

#include <algorithm>
#include <fstream>
#include <iostream>
#include <locale>

#ifdef _WIN32
    #define constexpr
    #define NOEXCEPT throw()
#else
    #define NOEXCEPT noexcept
#endif

// Change this to char and it works.
using byte_type …
Run Code Online (Sandbox Code Playgroud)

c++ g++ visual-studio c++11 char-traits

12
推荐指数
1
解决办法
1971
查看次数

std::basic_ifstream 抛出 std::bad_cast

我正在尝试使用以下代码从文件中读取数据。(请注意,您需要在 GCC 上启用 C++11 功能才能进行此编译。)

#include <fstream>

typedef unsigned char byte;

int main()
{
    std::string filename = "test.cpp";
    std::basic_ifstream<byte> in(filename, std::basic_ifstream<byte>::in | std::basic_ifstream<byte>::binary);
    in.exceptions(std::ios::failbit | std::ios::badbit);
    byte buf[5];
    in.read(buf, 5);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,在读取数据时出现异常:

抛出“std::bad_cast”实例后调用终止
  what(): std::bad_cast

in.read(buf, 5)调用命令时会发生这种情况。

我知道我可以通过不设置我设置的异常掩码来抑制这个异常,但这并不能解决问题,它只会掩盖它。没有异常掩码,代码继续工作,但读取了 0 个字符。

有谁知道为什么会抛出这个异常?我该如何让它消失?

c++ io file-io ifstream c++11

5
推荐指数
1
解决办法
1316
查看次数

visual c ++ - 为什么读取带有unsigned char ifstream的文件比使用char ifstream慢得多

我一直在使用Visual Studio在C++中读取文件来测试性能,我得到了一些我真的不明白的结果.

我的代码如下:

std::vector<unsigned char> Method1(std::string filePath)
{
    std::basic_ifstream<unsigned char> file(filePath, std::ios::binary);

    file.seekg(0, std::ios::end);
    std::streamsize size = file.tellg();
    file.seekg(0, std::ios::beg);

    std::vector<unsigned char> buffer(size);
    file.read(buffer.data(), size);

    return buffer;
}
Run Code Online (Sandbox Code Playgroud)

(我实际上使用了uint8_t而不是unsigned char,但因为它只是一个typedef,我在这里用后者来更好地演示问题)

我给它一个3mb文件来读取并使用std :: chrono函数来计时它们是我的结果:
Debug Configuration - 10.2秒
Release Configuration - 98 ms

调试和发布之间的巨大差异已经引起关注.

所以我尝试用"char"替换所有对"unsigned char"的引用(std :: basic_ifstream <char>而不是std :: basic_ifstream <unsigned char>等)并重新运行程序.
我发现它在调试和发布中都运行不到3ms.

稍微摆弄一下之后,我意识到basic_ifstream <unsigned char>就是问题所在.如果我按原样保留其他所有内容并将文件更改为basic_ifstream <char>(同时使用reinterpret_cast <char*>(buffer.data()),那么它也可以正常工作.

我在两台独立的机器上验证了这一点,一台运行Visual Studio 2015的最新版本,一台运行Visual Studio 15的预览版3.

是否存在某种微妙之处,使得basic_ifstream在使用无符号字符时表现如此糟糕?

c++ io performance visual-studio visual-c++

1
推荐指数
1
解决办法
450
查看次数