【问题标题】:Success message when read function fails读取功能失败时的成功消息
【发布时间】:2016-03-09 19:10:36
【问题描述】:

我正在尝试使用 ifstream 和所有 try and catch 方法打开文件。我想测试读取功能。当我试图阅读更多文件长度时,我遇到了异常。但是错误按摩:

Error:basic_ios::clear: Success

我的代码:

    void OpenFile(char* raw_slice) throw(std::ios_base::failure, std::string) {

        ifstream init;
        init.exceptions(ifstream::failbit | ifstream::badbit | ifstream::eofbit);
        try {
             init.open("/home/path/file", ios_base::in | ios_base::binary );
       }catch(const ifstream::failure &e) {
             cerr << "Error:" << e.what() << ": " << strerror(errno) << endl;
             throw;
       }
       try {
             init.read((char*)raw_slice, 1000); //out of bound of source file
       }catch (const ifstream::failure &e){
             cerr << "Error:" << e.what() << ": " <<  strerror(errno) << endl;
       throw;
       }
       try{
            init.close();
       }catch(const ifstream::failure &e){
            cerr << "Error:"<< e.what() << ": " << strerror(errno) << endl;
           throw;
       }
    }

int main()
{
    char* buf = new char[72];
    try{
        OpenFile(buf);
    }catch (const ifstream::failure &e){
        cerr << "Error in main:" << e.what() << ": " << strerror(errno) << endl;
        delete[] buf;
        return 0;
    }catch (const string &e) {
        cerr << "Error in main:: " << e << endl;
        delete[] buf;
        return 0;
    ``}
    delete[] buf;
    return 1;
}

任何想法为什么?我已经查看了 read 函数的返回值,但找不到此行为的答案。

【问题讨论】:

    标签: c++ exception try-catch throw


    【解决方案1】:

    我认为ifstream::read 没有设置errno 值。如果读取的字节数超过可用字节,只读设置failbiteofbit。您应该使用rdstate 而不是errno 来获取这两位的值。


    更新

    要检查是否设置了 failbiteofbit,请将它们与 rdstate() 返回值进行比较

    if ( (init.rdstate() & std::ifstream::failbit ) != 0 && (init.rdstate() & std::ifstream::eofbit ) != 0 )
        cerr << "Error: reached end of file before reading all required bytes\n";
    

    【讨论】:

    【解决方案2】:

    您的代码有一个错误:init.read((char*)raw_slice, 1000); 需要一个 1000 字节的缓冲区,但您正在传递一个使用 char* 创建的 72 字节缓冲区buf = 新字符[72];

    当 ifstream::read 尝试读取 72 字节缓冲区中的 1000 字节时,引发了一个不是 ifstream::read 句柄的异常,它只是返回“成功”(内部 ifstream::read 必须在变量中初始化返回代码开始)

    我建议你采用这种方法:

    void OpenFile(char* raw_slice, int buffer_size) throw(std::ios_base::failure, std::string) {
    
        ifstream init;
        init.exceptions(ifstream::failbit | ifstream::badbit | ifstream::eofbit);
        try {
             init.open("/home/path/file", ios_base::in | ios_base::binary );
       }catch(const ifstream::failure &e) {
             cerr << "Error:" << e.what() << ": " << strerror(errno) << endl;
             throw;
       }
       try {
             init.read((char*)raw_slice, buffer_size); //out of bound of source file
       }catch (const ifstream::failure &e){
             cerr << "Error:" << e.what() << ": " <<  strerror(errno) << endl;
       throw;
       }
       try{
            init.close();
       }catch(const ifstream::failure &e){
            cerr << "Error:"<< e.what() << ": " << strerror(errno) << endl;
           throw;
       }
    }
    
    int main()
    {
       char* buf = new char[72];
       try{
           OpenFile(buf, sizeof(buf));
    ...
    

    【讨论】:

      猜你喜欢
      • 2017-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-05
      • 2020-11-11
      • 2016-09-27
      • 1970-01-01
      相关资源
      最近更新 更多