Skip to content
Snippets Groups Projects
Commit 0982ea47 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

report failure when opening a file

parent d1897c24
No related branches found
No related tags found
No related merge requests found
......@@ -20,6 +20,9 @@ std::string ReadFile::read ( const std::string & filename ) {
return std::string ( ( std::istreambuf_iterator < char > ( std::cin ) ), std::istreambuf_iterator < char > ( ) );
} else {
std::ifstream t ( filename );
if ( ! t.is_open ( ) ) {
throw exception::CommonException ( "File could not be opened." );
}
t >> std::noskipws;
return std::string ( ( std::istreambuf_iterator < char > ( t ) ), std::istreambuf_iterator < char > ( ) );
}
......
......@@ -16,8 +16,13 @@ namespace builtin {
void WriteFile::write ( const std::string & filename, const std::string & data ) {
if ( filename == "-" )
std::cout << data;
else
std::ofstream ( filename ) << data;
else {
std::ofstream t ( filename );
if ( ! t.is_open ( ) ) {
throw exception::CommonException ( "File could not be opened." );
}
t << data;
}
}
 
auto WriteFileString = registration::AbstractRegister < WriteFile, void, const std::string &, const std::string & > ( WriteFile::write );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment