1<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 2'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'> 3<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'> 4<head> 5 <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/> 6 <title>errinfo_file_handle</title> 7 <link href='reno.css' type='text/css' rel='stylesheet'/> 8</head> 9<body> 10<div class="body-0"> 11<div class="body-1"> 12<div class="body-2"> 13<div> 14<div id="boost_logo"> 15<a href="http://www.boost.org"><img style="border:0" src="../../../boost.png" alt="Boost" width="277" height="86"/></a> 16</div> 17<h1>Boost Exception</h1> 18</div> 19<!-- Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc. --> 20<!-- Distributed under the Boost Software License, Version 1.0. (See accompanying --> 21<!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) --> 22<div class="RenoIncludeDIV"><div class="RenoAutoDIV"><h3>errinfo_file_handle</h3> 23</div> 24<div class="RenoIncludeDIV"><p><span class="RenoEscape">#<!--<wiki>`#</wiki>--></span>include <<span class="RenoLink"><a href="boost_exception_errinfo_file_handle_hpp.html">boost/exception/errinfo_file_handle.hpp</a></span>></p> 25<div class="RenoIncludeDIV"><pre>#include <<span class="RenoLink"><a href="boost_exception_error_info_hpp.html">boost/exception/error_info.hpp</a></span>> 26 27namespace 28boost 29 { 30 template <class> class weak_ptr; 31<span class="RenoIncludeSPAN"> <span class="RenoIncludeSPAN">typedef <span class="RenoLink"><a href="error_info.html">error_info</a></span><struct errinfo_file_handle_,weak_ptr<FILE> > <span class="RenoLink">errinfo_file_handle</span>;</span></span> 32 }</pre> 33</div></div><p>This type is designed to be used as a standard <span class="RenoLink"><a href="error_info.html">error_info</a></span> instance for transporting a relevant FILE pointer managed by a boost::shared_ptr<FILE> in exceptions deriving from boost::<span class="RenoLink"><a href="exception.html">exception</a></span>.</p> 34<h3>Example:</h3> 35<div class="RenoIncludeDIV"><pre>#include <<span class="RenoLink"><a href="boost_exception_errinfo_api_function_hpp.html">boost/exception/errinfo_api_function.hpp</a></span>> 36#include <<span class="RenoLink"><a href="boost_exception_errinfo_at_line_hpp.html">boost/exception/errinfo_at_line.hpp</a></span>> 37#include <<span class="RenoLink"><a href="boost_exception_errinfo_errno_hpp.html">boost/exception/errinfo_errno.hpp</a></span>> 38#include <<span class="RenoLink"><a href="boost_exception_errinfo_file_handle_hpp.html">boost/exception/errinfo_file_handle.hpp</a></span>> 39#include <<span class="RenoLink"><a href="boost_exception_errinfo_file_name_hpp.html">boost/exception/errinfo_file_name.hpp</a></span>> 40#include <<span class="RenoLink"><a href="boost_exception_errinfo_file_open_mode_hpp.html">boost/exception/errinfo_file_open_mode.hpp</a></span>> 41#include <<span class="RenoLink"><a href="boost_exception_info_hpp.html">boost/exception/info.hpp</a></span>> 42#include <<span class="RenoLink"><a href="boost_throw_exception_hpp.html">boost/throw_exception.hpp</a></span>> 43#include <boost/shared_ptr.hpp> 44#include <boost/weak_ptr.hpp> 45#include <stdio.h> 46#include <errno.h> 47#include <exception> 48 49struct error : virtual std::exception, virtual boost::<span class="RenoLink"><a href="exception.html">exception</a></span> { }; 50struct file_error : virtual error { }; 51struct file_open_error: virtual file_error { }; 52struct file_read_error: virtual file_error { }; 53 54boost::shared_ptr<FILE> 55open_file( char const * file, char const * mode ) 56 { 57 if( FILE * f=fopen(file,mode) ) 58 return boost::shared_ptr<FILE>(f,fclose); 59 else 60 <span class="RenoLink"><a href="BOOST_THROW_EXCEPTION.html">BOOST_THROW_EXCEPTION</a></span>( 61 file_open_error() << 62 boost::<span class="RenoLink"><a href="errinfo_api_function.html">errinfo_api_function</a></span>("fopen") << 63 boost::<span class="RenoLink"><a href="errinfo_errno.html">errinfo_errno</a></span>(errno) << 64 boost::<span class="RenoLink"><a href="errinfo_file_name.html">errinfo_file_name</a></span>(file) << 65 boost::<span class="RenoLink"><a href="errinfo_file_open_mode.html">errinfo_file_open_mode</a></span>(mode) ); 66 } 67 68size_t 69read_file( boost::shared_ptr<FILE> const & f, void * buf, size_t size ) 70 { 71 size_t nr=fread(buf,1,size,f.get()); 72 if( ferror(f.get()) ) 73 <span class="RenoLink"><a href="BOOST_THROW_EXCEPTION.html">BOOST_THROW_EXCEPTION</a></span>( 74 file_read_error() << 75 boost::<span class="RenoLink"><a href="errinfo_api_function.html">errinfo_api_function</a></span>("fread") << 76 boost::<span class="RenoLink"><a href="errinfo_errno.html">errinfo_errno</a></span>(errno) << 77 boost::<span class="RenoLink">errinfo_file_handle</span>(f) ); 78 return nr; 79 }</pre> 80</div></div><div class="RenoAutoDIV"><div class="RenoHR"><hr/></div> 81See also: <span class="RenoPageList"><a href="boost_exception_errinfo_file_handle_hpp.html">boost/exception/errinfo_file_handle.hpp</a></span> 82</div> 83<!-- Copyright (c) 2006-2009 Emil Dotchevski and Reverge Studios, Inc. --> 84<!-- Distributed under the Boost Software License, Version 1.0. (See accompanying --> 85<!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) --> 86<div id="footer"> 87<p> 88<a class="logo" href="http://jigsaw.w3.org/css-validator/check/referer"><img class="logo_pic" src="valid-css.png" alt="Valid CSS" height="31" width="88"/></a> 89<a class="logo" href="http://validator.w3.org/check?uri=referer"><img class="logo_pic" src="valid-xhtml.png" alt="Valid XHTML 1.0" height="31" width="88"/></a> 90<small>Copyright (c) 2006-2009 by Emil Dotchevski and Reverge Studios, Inc.<br/> 91Distributed under the <a href="http://www.boost.org/LICENSE_1_0.txt">Boost Software License, Version 1.0</a>.</small> 92</p> 93</div> 94</div> 95</div> 96</div> 97</body> 98</html> 99