• ifstream.open


    ifstream.open打开文件,如果文件不存在等情况并没有错误提示

    一般定义变量直接打开文件,可以正确读文件,但是错误没有反馈

    #include      // std::cerr
    #include       // std::ifstream
    
    int main() {
        std::ifstream file;
        file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
        try {
            file.open("test.txt");
            if (file.eof()) {
                std::cout << "eof\n";
            }
            while (!file.eof()) {
                std::cout << "get ..";
                file.get();
            }
            //while (!file.eof()) file.get();
            file.close();
        }
        catch (std::ifstream::failure e) {
            std::cerr << "Exception opening/reading/closing file\n" << e.what();
        }
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    正确做法在这里:

    method readeof() that does the same of read() but doesn’t set failbit on EOF. Also real read failures have been tested, like an interrupted transfer by hard removal of a USB stick or link drop in a network share access. It has been tested on Windows 7 with VS2010 and VS2013 and on linux with gcc 4.8.1. On linux only USB stick removal has been tried.
    https://stackoverflow.com/questions/6781545/why-is-failbit-set-when-eof-is-found-on-read

    #include 
    #include 
    #include 
    
    using namespace std;
    
    streamsize readeof(istream& stream, char* buffer, streamsize count)
    {
        if (count == 0 || stream.eof())
            return 0;
    
        streamsize offset = 0;
        streamsize reads;
        do
        {
            // This consistently fails on gcc (linux) 4.8.1 with failbit set on read
            // failure. This apparently never fails on VS2010 and VS2013 (Windows 7)
            reads = stream.rdbuf()->sgetn(buffer + offset, count);
    
            // This rarely sets failbit on VS2010 and VS2013 (Windows 7) on read
            // failure of the previous sgetn()
            (void)stream.rdstate();
    
            // On gcc (linux) 4.8.1 and VS2010/VS2013 (Windows 7) this consistently
            // sets eofbit when stream is EOF for the conseguences  of sgetn(). It
            // should also throw if exceptions are set, or return on the contrary,
            // and previous rdstate() restored a failbit on Windows. On Windows most
            // of the times it sets eofbit even on real read failure
            (void)stream.peek();
    
            if (stream.fail())
                throw runtime_error("Stream I/O error while reading");
    
            offset += reads;
            count -= reads;
        } while (count != 0 && !stream.eof());
    
        return offset;
    }
    
    #define BIGGER_BUFFER_SIZE 200000000
    
    int main(int argc, char* argv[])
    {
        ifstream stream;
        stream.exceptions(ifstream::badbit | ifstream::failbit);
        stream.open("test.txt", ios::binary);
    
        char* buffer = new char[BIGGER_BUFFER_SIZE];
    
        streamsize reads = readeof(stream, buffer, BIGGER_BUFFER_SIZE);
    
        if (stream.eof())
            cout << "eof:reads::" << reads << "\n" << buffer << endl << flush;
    
        delete buffer;
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
  • 相关阅读:
    Bitcoin+STARK: ZeroSync & Khepri
    c++征途 --- 函数提高
    C和指针 第11章 动态内存分配 11.10 问题
    Solana流支付协议Zebec完成850万美元融资,CircleVentures等参投
    RabbitMQ特殊应用
    正则表达式
    tf.while_loop
    【Java SE】数据类型与变量
    Hive 分区表
    python之while循环介绍
  • 原文地址:https://blog.csdn.net/mlz_2/article/details/126896273