Detecting reason for failure to open an ofstream when fail() is true

前端 未结 4 501
北荒
北荒 2020-12-06 16:16

Seems like this should be simple, but I don\'t find it in a net search.

I have an ofstream which is open(), and fail() is now true. I\'d li

4条回答
  •  醉话见心
    2020-12-06 17:07

    we need not use std::fstream, we use boost::iostream

    #include 
    #include 
    
    void main()
    {
       namespace io = boost::iostreams;
    
       //step1. open a file, and check error.
       int handle = fileno(stdin); //I'm lazy,so...
    
       //step2. create stardard conformance streem
       io::stream s( io::file_descriptor_source(handle) );
    
       //step3. use good facilities as you will
       char buff[32];
       s.getline( buff, 32);
    
       int i=0;
       s >> i;
    
       s.read(buff,32);
    
    }
    

提交回复
热议问题