error handling - Can I mix C and C++ IO? -


intro

sync_with_stdio, enabled default states internally c , c++ use same buffer streams. there no interoperability beyond that. can't example construct basic_rdbuf file, need use basic_rdbuf::open.

what i'm trying do

i'd use exceptions io there's problem. it's true can use strerror error message, that's way down stack. c++ io has 1 exception (failure) , has 1 error code (io_errc::stream). there no granularity, catching exception far away error occurred not helpful.

here's problem. can configure throw exceptions on failbit. not failbit errors exceptions. example:

try  {   filestr.open(":^(");    if (!(file >> input))   {       // error   } } catch (ios_base::failure& e)  {     cout << strerror(errno); } 

this groups "bad user input" "file not exist", think bad design.

ideally i'd have separation of concerns. like:

try {     auto file = std::fopen(...);      if ( /* bad conditional */ )        throw custom_exception_with_error_code(errno); } catch (custom_exception_with_error_code& e) {     if (e.code() == some_error_code)     {         /* handle error */     } }  auto stream = std::some_something_that_accepts_file(file);  /* ... */ 

this rough sketch. i'm working out details.

the moral

i'd ideally not have start writing bulky custom stream classes scratch. i'm looking friendly built-in way this. possible?

i'm looking advice on flaws approach.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -