tutorial.html 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Language" content="en-us">
  4. <meta name="GENERATOR" content="Microsoft FrontPage 5.0">
  5. <meta name="ProgId" content="FrontPage.Editor.Document">
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7. <title>Filesystem Tutorial</title>
  8. <link href="styles.css" rel="stylesheet">
  9. </head>
  10. <body>
  11. <table border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111">
  12. <tr>
  13. <td width="277">
  14. <a href="../../../index.htm">
  15. <img src="../../../boost.png" alt="boost.png (6897 bytes)" align="middle" width="300" height="86" border="0"></a></td>
  16. <td align="middle">
  17. <font size="7">Filesystem Tutorial</font>
  18. </td>
  19. </tr>
  20. </table>
  21. <table border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse"
  22. bordercolor="#111111" bgcolor="#D7EEFF" width="100%">
  23. <tr>
  24. <td><a href="index.htm">Home</a> &nbsp;&nbsp;
  25. <a href="tutorial.html">Tutorial</a> &nbsp;&nbsp;
  26. <a href="reference.html">Reference</a> &nbsp;&nbsp;
  27. <a href="faq.htm">FAQ</a> &nbsp;&nbsp;
  28. <a href="release_history.html">Releases</a> &nbsp;&nbsp;
  29. <a href="portability_guide.htm">Portability</a> &nbsp;&nbsp;
  30. <a href="v3.html">V3 Intro</a> &nbsp;&nbsp;
  31. <a href="v3_design.html">V3 Design</a> &nbsp;&nbsp;
  32. <a href="deprecated.html">Deprecated</a> &nbsp;&nbsp;
  33. <a href="issue_reporting.html">Bug Reports </a>&nbsp;&nbsp;
  34. </td>
  35. </table>
  36. <p>
  37. <a href="#Introduction">Introduction</a><br>
  38. <a href="#Preliminaries">Preliminaries</a><br>
  39. <a href="#Reporting-size">Reporting the size of a file - (tut1.cpp)</a><br>
  40. <a href="#Using-status-queries">Using status queries to determine file existence and type - (tut2.cpp)</a><br>
  41. <a href="#Directory-iteration">Directory iteration plus catching
  42. exceptions - (tut3.cpp)</a><br>
  43. <a href="#Using-path-decomposition">Using path decomposition, plus sorting results - (tut4.cpp)</a><br>
  44. <a href="#Class-path-Constructors">Class path: Constructors, including
  45. Unicode - (tut5.cpp)</a><br>
  46. <a href="#Class-path-formats">Class path: Generic format vs. Native format</a><br>
  47. <a href="#Class path-iterators-etc">Class path: Iterators, observers, composition, decomposition, and query - (path_info.cpp)</a><br>
  48. <a href="#Error-reporting">Error reporting</a><br>
  49. </p>
  50. <h2><a name="Introduction">Introduction</a></h2>
  51. <p>This tutorial develops a little command line program to list information
  52. about files and directories - essentially a much simplified version of the POSIX <code>ls</code> or Windows <code>dir</code>
  53. commands. We'll start with the simplest possible version and progress to more
  54. complex functionality. Along the way we'll digress to cover topics you'll need
  55. to know about to understand Boost.Filesystem.</p>
  56. <p>Source code for each of the tutorial programs is available, and you
  57. are encouraged to compile, test, and experiment with it. To conserve space, we won't
  58. always show boilerplate code here, but the provided source is complete and
  59. ready to build.</p>
  60. <h2><a name="Preliminaries">Preliminaries</a></h2>
  61. <p>Install the Boost distribution if you haven't already done so. See the
  62. <a href="http://www.boost.org/more/getting_started/index.html">Boost Getting
  63. Started</a> docs.</p>
  64. <p>This tutorial assumes you are going to compile and test the examples using
  65. the provided scripts. That's highly recommended.</p>
  66. <blockquote>
  67. <p><b>If you are planning to compile and test the examples but not use the
  68. scripts, make sure your build setup knows where to
  69. locate or build the Boost library binaries.</b></p>
  70. </blockquote>
  71. <p>Fire up your command line interpreter, and type the following commands:</p>
  72. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  73. <tr>
  74. <td align="center"><i><b>Ubuntu Linux </b></i></td>
  75. </tr>
  76. <tr>
  77. <td>
  78. <pre>$ cd <i><b>boost-root</b></i>/libs/filesystem/example/test
  79. $ ./setup.sh
  80. Copying example programs...
  81. $ ./build.sh
  82. Compiling example programs...
  83. $ ./tut1
  84. Usage: tut1 path</pre>
  85. </td>
  86. </tr>
  87. </table>
  88. &nbsp;
  89. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  90. <tr>
  91. <td align="center"><i><b>Microsoft Windows</b></i></td>
  92. </tr>
  93. <tr>
  94. <td>
  95. <pre>&gt;cd <i><b>boost-root</b></i>\libs\filesystem\example\test
  96. &gt;setup
  97. Copying example programs...
  98. &gt;build
  99. Compiling example programs...
  100. &gt;tut1
  101. Usage: tut1 path</pre>
  102. </td>
  103. </tr>
  104. </table>
  105. <p>If the <code>tut1</code> command outputs &quot;<code>Usage: tut1 path</code>&quot;, all
  106. is well. A set of tutorial example programs has been copied (by <code>setup</code>) to
  107. <i><b><code>boost-root</code></b></i><code>/libs/filesystem/example/test</code>
  108. and then built. You are encouraged to modify and experiment with them as the
  109. tutorial progresses. Just invoke the <code>build</code> script again to rebuild,
  110. or invoke <code>b2</code> directly.</p>
  111. <p>If something didn't work right, here are some troubleshooting suggestions:</p>
  112. <ul>
  113. <li>If the <code>b2</code> program executable isn't being found, check your path environmental variable
  114. or see
  115. <a href="http://www.boost.org/more/getting_started/windows.html">Boost
  116. Getting Started</a>.<br>
  117. &nbsp;</li>
  118. <li>Look at <code>b2.log</code> to try to spot an indication of the
  119. problem.</li>
  120. </ul>
  121. <h2><a name="Reporting-size">Reporting the size of a file</a> - (<a href="../example/tut1.cpp">tut1.cpp</a>)</h2>
  122. <p>Let's get started. Our first example program, <a href="../example/tut1.cpp">tut1.cpp</a>,
  123. reports the size of a file:</p>
  124. <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  125. <tr>
  126. <td>
  127. <pre><!-- include file "../example/tut1.cpp" -->#include &lt;iostream&gt;
  128. #include &lt;boost/filesystem.hpp&gt;
  129. using namespace boost::filesystem;
  130. int main(int argc, char* argv[])
  131. {
  132. if (argc &lt; 2)
  133. {
  134. std::cout &lt;&lt; &quot;Usage: tut1 path\n&quot;;
  135. return 1;
  136. }
  137. std::cout &lt;&lt; argv[1] &lt;&lt; &quot; &quot; &lt;&lt; file_size(argv[1]) &lt;&lt; '\n';
  138. return 0;
  139. }<!-- end include file --></pre>
  140. </td>
  141. </tr>
  142. </table>
  143. <p>The Boost.Filesystem <code><a href="reference.html#file_size">file_size</a></code>
  144. function returns a <code>uintmax_t</code>
  145. containing the size of the file named by the argument. The declaration looks
  146. like this:</p>
  147. <blockquote>
  148. <pre><code>uintmax_t file_size(const path&amp; p);</code> </pre>
  149. </blockquote>
  150. <p>For now, all you need to know is that <code>class path</code> has constructors that take
  151. <code>const char *</code> and other string types. (If you can't wait to
  152. find out more, skip ahead to the <a href="#Class-path-Constructors">class path</a> section of
  153. the tutorial.)</p>
  154. <p>Please take a minute to try out <code>tut1</code> on your system, using a
  155. file that is known to exist, such as <code>tut1.cpp</code>. Here is what the
  156. results look like on two different operating systems:</p>
  157. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  158. <tr>
  159. <td align="center"><i><b>Ubuntu Linux </b></i></td>
  160. </tr>
  161. <tr>
  162. <td valign="top">
  163. <pre>$ ./tut1 tut1.cpp
  164. tut1.cpp 569</pre>
  165. <pre>$ ls -l tut1.cpp
  166. -rw-rw-r-- 1 beman beman 569 Jul 26 12:04 tut1.cpp</pre>
  167. </td>
  168. </tr>
  169. </table>
  170. &nbsp;
  171. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  172. <tr>
  173. <td align="center"><i><b>Microsoft Windows</b></i></td>
  174. </tr>
  175. <tr>
  176. <td valign="top">
  177. <pre>&gt;tut1 tut1.cpp
  178. tut1.cpp 592
  179. &gt;dir tut1.cpp
  180. ...
  181. 07/26/2015 07:20 AM 592 tut1.cpp
  182. ...</pre>
  183. </td>
  184. </tr>
  185. </table>
  186. <p>So far, so good. The reported Linux and Windows sizes are different because
  187. the Linux tests used <code>&quot;\n&quot;</code> line endings, while the Windows tests
  188. used <code>&quot;\r\n&quot;</code> line endings. The sizes reported may differ
  189. from the above if changes have been made to <code>tut1.cpp</code>.</p>
  190. <p>Now try again, but give a path that doesn't exist:</p>
  191. <table border="1" cellpadding="5" cellspacing="0"
  192. style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  193. <tr>
  194. <td align="center"><i><b>Ubuntu Linux </b></i></td>
  195. </tr>
  196. <tr>
  197. <td valign="top">
  198. <pre>$ ./tut1 foo
  199. terminate called after throwing an instance of &#39;boost::filesystem::filesystem_error&#39;
  200. what(): boost::filesystem::file_size: No such file or directory: &quot;foo&quot;
  201. Aborted (core dumped)</pre>
  202. </td>
  203. </tr>
  204. </table>
  205. &nbsp;
  206. <table border="1" cellpadding="5" cellspacing="0"
  207. style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  208. <tr>
  209. <td align="center"><i><b>Microsoft Windows</b></i></td>
  210. </tr>
  211. <tr>
  212. <td valign="top">
  213. <pre>&gt;tut1 foo</pre>
  214. <p><b><i>An exception is thrown;<br>
  215. the exact form of the response depends on
  216. Windows system options.</i></b></td>
  217. </tr>
  218. </table>
  219. <p>What happens?
  220. There's no file named <code>foo</code> in the current directory, so by default an
  221. exception is thrown. See <a href="#Error-reporting">Error reporting</a> to learn
  222. about error reporting via error codes rather than exceptions.</p>
  223. <p>Try this:</p>
  224. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  225. <tr>
  226. <td align="center"><i><b>Ubuntu Linux </b></i></td>
  227. </tr>
  228. <tr>
  229. <td>
  230. <pre>$ ./tut1 .
  231. terminate called after throwing an instance of &#39;boost::filesystem::filesystem_error&#39;
  232. what(): boost::filesystem::file_size: Operation not permitted: &quot;.&quot;
  233. Aborted (core dumped)</pre>
  234. </td>
  235. </tr>
  236. </table>
  237. &nbsp;
  238. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  239. <tr>
  240. <td align="center"><i><b>Microsoft Windows</b></i></td>
  241. </tr>
  242. <tr>
  243. <td valign="top">
  244. <pre>&gt;tut1 .</pre>
  245. <p><b><i>An exception is thrown;<br>
  246. the exact form of the response depends on Windows system options.</i></b></td>
  247. </tr>
  248. </table>
  249. <p>The current directory exists, but <code>file_size()</code> works on regular
  250. files, not directories, so again an exception is thrown.</p>
  251. <p>We'll deal with those situations in <code>tut2.cpp</code>.</p>
  252. <h2><a name="Using-status-queries">Using status queries to determine file existence and type</a> - (<a href="../example/tut2.cpp">tut2.cpp</a>)</h2>
  253. <p>Boost.Filesystem includes status query functions such as <code>
  254. <a href="reference.html#exists-path">exists</a></code>,
  255. <code><a href="reference.html#is_directory-path">is_directory</a></code>, and <code>
  256. <a href="reference.html#is_regular_file-path">is_regular_file</a></code>. These return
  257. <code>bool</code>'s, and will return <code>true</code> if the condition
  258. described by their name is met. Otherwise they return <code>false</code>,
  259. including when any element
  260. of the path argument can't be found.</p>
  261. <p><a href="../example/tut2.cpp">tut2.cpp</a> uses several of the status query functions to cope with non-existent
  262. files and with different kinds of files:</p>
  263. <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  264. <tr>
  265. <td>
  266. <pre><!-- include file "../example/tut2.cpp" -->#include &lt;iostream&gt;
  267. #include &lt;boost/filesystem.hpp&gt;
  268. using namespace std;
  269. using namespace boost::filesystem;
  270. int main(int argc, char* argv[])
  271. {
  272. if (argc &lt; 2)
  273. {
  274. cout &lt;&lt; &quot;Usage: tut2 path\n&quot;;
  275. return 1;
  276. }
  277. path p(argv[1]); // avoid repeated path construction below
  278. if (exists(p)) // does path p actually exist?
  279. {
  280. if (is_regular_file(p)) // is path p a regular file?
  281. cout &lt;&lt; p &lt;&lt; &quot; size is &quot; &lt;&lt; file_size(p) &lt;&lt; '\n';
  282. else if (is_directory(p)) // is path p a directory?
  283. cout &lt;&lt; p &lt;&lt; &quot; is a directory\n&quot;;
  284. else
  285. cout &lt;&lt; p &lt;&lt; &quot; exists, but is not a regular file or directory\n&quot;;
  286. }
  287. else
  288. cout &lt;&lt; p &lt;&lt; &quot; does not exist\n&quot;;
  289. return 0;
  290. }<!-- end include file --></pre>
  291. </td>
  292. </tr>
  293. </table>
  294. <p>Give it a try:</p>
  295. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  296. <tr>
  297. <td align="center"><i><b>Ubuntu Linux </b></i></td>
  298. </tr>
  299. <tr>
  300. <td valign="top">
  301. <pre>$ ./tut2 tut2.cpp
  302. &quot;tut2.cpp&quot; size is 997
  303. $ ./tut2 foo
  304. &quot;foo&quot; does not exist
  305. $ ./tut2 .
  306. &quot;.&quot; is a directory</pre>
  307. </td>
  308. </tr>
  309. </table>
  310. &nbsp;
  311. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  312. <tr>
  313. <td align="center"><i><b>Microsoft Windows</b></i></td>
  314. </tr>
  315. <tr>
  316. <td valign="top">
  317. <pre>&gt;tut2 tut2.cpp
  318. tut2.cpp size is 1039
  319. &gt;tut2 foo
  320. &quot;foo&quot; does not exist
  321. &gt;tut2 .
  322. &quot;.&quot; is a directory</pre>
  323. </td>
  324. </tr>
  325. </table>
  326. <p>Although tut2 works OK in these tests, the output is less than satisfactory
  327. for a directory. We'd typically like to see a list of the directory's contents. In <code>tut3.cpp</code>
  328. we will see how to iterate over directories.</p>
  329. <p>But first, let's try one more test:</p>
  330. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  331. <tr>
  332. <td align="center"><i><b>Ubuntu Linux </b></i></td>
  333. </tr>
  334. <tr>
  335. <td valign="top">
  336. <pre>$ ls /home/jane/foo
  337. ls: cannot access /home/jane/foo: No such file or directory
  338. $ ./tut2 /home/jane/foo
  339. terminate called after throwing an instance of 'boost::
  340. filesystem::filesystem_error&gt;'
  341. what(): boost::filesystem::status: Permission denied:
  342. &quot;/home/jane/foo&quot;
  343. Aborted</pre>
  344. </td>
  345. </tr>
  346. </table>
  347. &nbsp;
  348. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  349. <tr>
  350. <td align="center"><i><b>Microsoft Windows</b></i></td>
  351. </tr>
  352. <tr>
  353. <td valign="top">
  354. <pre>&gt;dir e:\
  355. The device is not ready.
  356. &gt;tut2 e:\</pre>
  357. <p dir="ltr"><b><i>An exception is thrown;<br>
  358. the exact form of the response depends on
  359. Windows system options.</i></b></td>
  360. </tr>
  361. </table>
  362. <p>On the Linux system, the test was being run from an account that did not have
  363. permission to access <code>/home/jane/foo</code>. On the Windows system, <code>
  364. e:</code> was a Compact Disc reader/writer that was not ready. End users
  365. shouldn't have to interpret cryptic exceptions reports, so as we move on to <code>tut3.cpp</code>
  366. we will increase the robustness of the code, too.</p>
  367. <h2><a name="Directory-iteration">Directory iteration</a> plus catching
  368. exceptions - (<a href="../example/tut3.cpp">tut3.cpp</a>)</h2>
  369. <p>Boost.Filesystem's <code><a href="reference.html#directory_iterator">
  370. directory_iterator</a></code> class is just what we need here. It follows the
  371. general pattern of the standard library's <code>istream_iterator</code>. Constructed from
  372. a path, it iterates over the contents of the directory. A default constructed <code>directory_iterator</code>
  373. acts as the end iterator.</p>
  374. <p>The value type of <code>directory_iterator</code> is <code>
  375. <a href="reference.html#directory_entry">directory_entry</a></code>. A <code>
  376. directory_entry</code> object contains <code>path</code> and <code><a href="reference.html#file_status">file_status</a></code>
  377. information.&nbsp; A <code>
  378. directory_entry</code> object
  379. can be used directly, but can also be passed to <code>path</code> arguments in function calls.</p>
  380. <p>The other need is increased robustness in the face of the many kinds of
  381. errors that can affect file system operations. We could do that at the level of
  382. each call to a Boost.Filesystem function (see <a href="#Error-reporting">Error
  383. reporting</a>), but for simplicity <a href="../example/tut3.cpp">tut3.cpp</a>
  384. uses an overall try/catch block.</p>
  385. <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  386. <tr>
  387. <td>
  388. <pre><!-- include file "../example/tut3.cpp" -->#include &lt;iostream&gt;
  389. #include &lt;boost/filesystem.hpp&gt;
  390. using std::cout;
  391. using namespace boost::filesystem;
  392. int main(int argc, char* argv[])
  393. {
  394. if (argc &lt; 2)
  395. {
  396. cout &lt;&lt; &quot;Usage: tut3 path\n&quot;;
  397. return 1;
  398. }
  399. path p (argv[1]);
  400. try
  401. {
  402. if (exists(p))
  403. {
  404. if (is_regular_file(p))
  405. cout &lt;&lt; p &lt;&lt; &quot; size is &quot; &lt;&lt; file_size(p) &lt;&lt; '\n';
  406. else if (is_directory(p))
  407. {
  408. cout &lt;&lt; p &lt;&lt; &quot; is a directory containing:\n&quot;;
  409. for (directory_entry&amp; x : directory_iterator(p))
  410. cout &lt;&lt; &quot; &quot; &lt;&lt; x.path() &lt;&lt; '\n';
  411. }
  412. else
  413. cout &lt;&lt; p &lt;&lt; &quot; exists, but is not a regular file or directory\n&quot;;
  414. }
  415. else
  416. cout &lt;&lt; p &lt;&lt; &quot; does not exist\n&quot;;
  417. }
  418. catch (const filesystem_error&amp; ex)
  419. {
  420. cout &lt;&lt; ex.what() &lt;&lt; '\n';
  421. }
  422. return 0;
  423. }<!-- end include file --></pre>
  424. </td>
  425. </tr>
  426. </table>
  427. <p>Give <code>tut3</code> a try, passing it a path to a directory as a command line argument.
  428. Here is a run on a checkout of the Boost Git develop branch, followed by a repeat
  429. of the test cases that caused exceptions on Linux and Windows:</p>
  430. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  431. <tr>
  432. <td align="center"><i><b>Ubuntu Linux </b></i></td>
  433. </tr>
  434. <tr>
  435. <td valign="top">
  436. <pre>$ ./tut3 ~/boost/develop
  437. &quot;/home/beman/boost/develop&quot; is a directory containing:
  438. &quot;/home/beman/boost/develop/rst.css&quot;
  439. &quot;/home/beman/boost/develop/boost&quot;
  440. &quot;/home/beman/boost/develop/boost.png&quot;
  441. &quot;/home/beman/boost/develop/libs&quot;
  442. &quot;/home/beman/boost/develop/doc&quot;
  443. &quot;/home/beman/boost/develop/project-config.jam.2&quot;
  444. &quot;/home/beman/boost/develop/.gitmodules&quot;
  445. &quot;/home/beman/boost/develop/boostcpp.py&quot;
  446. &quot;/home/beman/boost/develop/.travis.yml&quot;
  447. &quot;/home/beman/boost/develop/.gitattributes&quot;
  448. &quot;/home/beman/boost/develop/index.htm&quot;
  449. &quot;/home/beman/boost/develop/index.html&quot;
  450. &quot;/home/beman/boost/develop/bjam&quot;
  451. &quot;/home/beman/boost/develop/project-config.jam.1&quot;
  452. &quot;/home/beman/boost/develop/LICENSE_1_0.txt&quot;
  453. &quot;/home/beman/boost/develop/.git&quot;
  454. &quot;/home/beman/boost/develop/tools&quot;
  455. &quot;/home/beman/boost/develop/stage&quot;
  456. &quot;/home/beman/boost/develop/boostcpp.jam&quot;
  457. &quot;/home/beman/boost/develop/Jamroot&quot;
  458. &quot;/home/beman/boost/develop/.gitignore&quot;
  459. &quot;/home/beman/boost/develop/INSTALL&quot;
  460. &quot;/home/beman/boost/develop/more&quot;
  461. &quot;/home/beman/boost/develop/bin.v2&quot;
  462. &quot;/home/beman/boost/develop/project-config.jam&quot;
  463. &quot;/home/beman/boost/develop/boost-build.jam&quot;
  464. &quot;/home/beman/boost/develop/bootstrap.bat&quot;
  465. &quot;/home/beman/boost/develop/bootstrap.sh&quot;
  466. &quot;/home/beman/boost/develop/status&quot;
  467. &quot;/home/beman/boost/develop/boost.css&quot;</pre>
  468. </td>
  469. </tr>
  470. </table>
  471. &nbsp;
  472. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  473. <tr>
  474. <td align="center"><i><b>Microsoft Windows</b></i></td>
  475. </tr>
  476. <tr>
  477. <td valign="top">
  478. <pre>&gt;tut3 \boost\develop
  479. "\boost\develop" is a directory containing:
  480. "\boost\develop\.git"
  481. "\boost\develop\.gitattributes"
  482. "\boost\develop\.gitignore"
  483. "\boost\develop\.gitmodules"
  484. "\boost\develop\.travis.yml"
  485. "\boost\develop\bin.v2"
  486. "\boost\develop\boost"
  487. "\boost\develop\boost-build.jam"
  488. "\boost\develop\boost.css"
  489. "\boost\develop\boost.png"
  490. "\boost\develop\boostcpp.jam"
  491. "\boost\develop\boostcpp.py"
  492. "\boost\develop\bootstrap.bat"
  493. "\boost\develop\bootstrap.sh"
  494. "\boost\develop\doc"
  495. "\boost\develop\index.htm"
  496. "\boost\develop\index.html"
  497. "\boost\develop\INSTALL"
  498. "\boost\develop\Jamroot"
  499. "\boost\develop\libs"
  500. "\boost\develop\LICENSE_1_0.txt"
  501. "\boost\develop\more"
  502. "\boost\develop\project-config.jam"
  503. "\boost\develop\rst.css"
  504. "\boost\develop\stage"
  505. "\boost\develop\status"
  506. "\boost\develop\tools"</pre>
  507. <pre>&gt;tut3 e:\
  508. boost::filesystem::status: The device is not ready: &quot;e:\&quot;</pre>
  509. </td>
  510. </tr>
  511. </table>
  512. <p>Not bad, but we can make further improvements:</p>
  513. <ul>
  514. <li>The listing would be much easier to read if only the filename was
  515. displayed, rather than the full path.<br>
  516. &nbsp;</li>
  517. <li>The Linux listing isn't sorted. That's because the ordering of
  518. directory iteration is unspecified. Ordering depends on the underlying
  519. operating system API and file system specifics. So we need to sort the
  520. results ourselves. </li>
  521. </ul>
  522. <p>The next sections show how those changes play out, so read on!</p>
  523. <h2><a name="Using-path-decomposition">Using path decomposition, plus sorting results</a> - (<a href="../example/tut4.cpp">tut4.cpp</a>)</h2>
  524. <p>For directories, <a href="../example/tut4.cpp">tut4.cpp</a> builds a <code>
  525. std::vector</code> of all the entries and then sorts it before writing to <code>
  526. cout</code>.</p>
  527. <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  528. <tr>
  529. <td>
  530. <pre><!-- include file "../example/tut4.cpp" -->#include &lt;iostream&gt;
  531. #include &lt;vector&gt;
  532. #include &lt;algorithm&gt;
  533. #include &lt;boost/filesystem.hpp&gt;
  534. using std::cout;
  535. using namespace boost::filesystem;
  536. int main(int argc, char* argv[])
  537. {
  538. if (argc &lt; 2)
  539. {
  540. cout &lt;&lt; &quot;Usage: tut4 path\n&quot;;
  541. return 1;
  542. }
  543. path p (argv[1]);
  544. try
  545. {
  546. if (exists(p))
  547. {
  548. if (is_regular_file(p))
  549. cout &lt;&lt; p &lt;&lt; &quot; size is &quot; &lt;&lt; file_size(p) &lt;&lt; '\n';
  550. else if (is_directory(p))
  551. {
  552. cout &lt;&lt; p &lt;&lt; &quot; is a directory containing:\n&quot;;
  553. std::vector&lt;path&gt; v;
  554. for (auto&amp;&amp; x : directory_iterator(p))
  555. v.push_back(x.path());
  556. std::sort(v.begin(), v.end());
  557. for (auto&amp;&amp; x : v)
  558. cout &lt;&lt; &quot; &quot; &lt;&lt; x.filename() &lt;&lt; '\n';
  559. }
  560. else
  561. cout &lt;&lt; p &lt;&lt; &quot; exists, but is not a regular file or directory\n&quot;;
  562. }
  563. else
  564. cout &lt;&lt; p &lt;&lt; &quot; does not exist\n&quot;;
  565. }
  566. catch (const filesystem_error&amp; ex)
  567. {
  568. cout &lt;&lt; ex.what() &lt;&lt; '\n';
  569. }
  570. return 0;
  571. }<!-- end include file --></pre>
  572. </blockquote>
  573. </td>
  574. </tr>
  575. </table>
  576. <p>The only difference between <code>tut3.cpp</code> and <code>tut4.cpp</code> is
  577. what happens for directories. We changed:</p>
  578. <blockquote>
  579. <pre>for (const directory_entry&amp; x : directory_iterator(p))
  580. cout &lt;&lt; &quot; &quot; &lt;&lt; x.path() &lt;&lt; &#39;\n&#39;;</pre>
  581. </blockquote>
  582. <p>to:</p>
  583. <blockquote>
  584. <pre>std::vector&lt;path&gt; v;
  585. for (auto&amp;&amp; x : directory_iterator(p))
  586. v.push_back(x.path());
  587. std::sort(v.begin(), v.end());
  588. for (auto&amp;&amp; x : v)
  589. cout &lt;&lt; &quot; &quot; &lt;&lt; x.filename() &lt;&lt; &#39;\n&#39;;
  590. </pre>
  591. </blockquote>
  592. <p> <code>
  593. <a href="reference.html#path-filename">filename()</a></code> is one of
  594. several class <code>path</code> decomposition functions. It extracts the
  595. filename portion
  596. from a path (<font face="Courier New">i.e. </font><code>&quot;index.html&quot;</code><font face="Courier New">
  597. from </font><code>&quot;/home/beman/boost/trunk/index.html&quot;</code>). These decomposition functions are
  598. more fully explored in the <a href="#Class path-iterators-etc">Path iterators, observers,
  599. composition, decomposition and query</a> portion of this tutorial.</p>
  600. <p>The above was written as two lines of code for clarity. It could have
  601. been written more concisely as:</p>
  602. <blockquote>
  603. <pre>v.push_back(it-&gt;path().filename()); // we only care about the filename</pre>
  604. </blockquote>
  605. <p>Here is the output from a test of <code><a href="../example/tut4.cpp">tut4.cpp</a></code>:</p>
  606. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  607. <tr>
  608. <td align="center"><i><b>Ubuntu Linux </b></i></td>
  609. </tr>
  610. <tr>
  611. <td>
  612. <pre>$ ./tut4 v</pre>
  613. </td>
  614. </tr>
  615. </table>
  616. &nbsp;
  617. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  618. <tr>
  619. <td align="center"><i><b>Microsoft Windows</b></i></td>
  620. </tr>
  621. <tr>
  622. <td>
  623. <pre>$ ./tut4 ~/boost/develop
  624. "/home/beman/boost/develop" is a directory containing:
  625. .git
  626. .gitattributes
  627. .gitignore
  628. .gitmodules
  629. .travis.yml
  630. INSTALL
  631. Jamroot
  632. LICENSE_1_0.txt
  633. bin.v2
  634. boost
  635. boost-build.jam
  636. boost.css
  637. boost.png
  638. boostcpp.jam
  639. boostcpp.py
  640. bootstrap.bat
  641. bootstrap.sh
  642. doc
  643. index.htm
  644. index.html
  645. libs
  646. more
  647. project-config.jam
  648. project-config.jam.1
  649. project-config.jam.2
  650. rst.css
  651. stage
  652. status
  653. tools</pre>
  654. </td>
  655. </tr>
  656. </table>
  657. <p>That completes the main portion of this tutorial. If you haven't already
  658. worked through the <a href="#Class-path-Constructors">Class path</a> sections of this tutorial, dig into them now.
  659. The <a href="#Error-reporting">Error reporting</a> section may also be of
  660. interest, although it can be skipped unless you are deeply concerned about
  661. error handling issues.</p>
  662. <h2><a name="Class-path-Constructors">Class path: Constructors</a>,
  663. including Unicode - (<a href="../example/tut5.cpp">tut5.cpp</a>)</h2>
  664. <p>Traditional C interfaces pass paths as <code>const char*</code> arguments.
  665. C++ interfaces may add <code>const std::string&amp;</code> overloads, but adding
  666. overloads becomes untenable if wide characters, containers, and iterator ranges
  667. need to be supported.</p>
  668. <p>Passing paths as <code>const path&amp;</code> arguments is far simpler, yet far
  669. more flexible because class <code>path</code> itself is far more flexible:</p>
  670. <ol>
  671. <li>Class <code>path</code> supports multiple character types and encodings, including Unicode, to
  672. ease internationalization.</li>
  673. <li>Class <code>path</code> supports multiple source types, such as iterators for null terminated
  674. sequences, iterator ranges, containers (including <code>std::basic_string</code>),
  675. and <code><a href="reference.html#Class-directory_entry">directory_entry</a></code>'s,
  676. so functions taking paths don't need to provide several overloads.</li>
  677. <li>Class <code>path</code> supports both native and generic pathname formats, so programs can be
  678. portable between operating systems yet use native formats where desirable.</li>
  679. <li>Class <code>path</code> supplies a full set of iterators, observers, composition,
  680. decomposition, and query functions, making pathname manipulations easy,
  681. convenient, reliable, and portable.</li>
  682. </ol>
  683. <p>Here is how (1) and (2) work. Class path constructors,
  684. assignments, and appends have member templates for sources. For example, here
  685. are the constructors that take sources:</p>
  686. <blockquote>
  687. <pre>template &lt;class <a href="reference.html#Source">Source</a>&gt;
  688. path(Source const&amp; source);</pre>
  689. <pre>template &lt;class InputIterator&gt;
  690. path(InputIterator begin, InputIterator end);</pre>
  691. </blockquote>
  692. <p>Let's look at a little program that shows how comfortable class <code>path</code> is with
  693. both narrow and wide characters in C-style strings, C++ strings, and via C++
  694. iterators:</p>
  695. <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  696. <tr>
  697. <td>
  698. <pre><!-- include file "../example/tut5.cpp" -->#include &lt;boost/filesystem/fstream.hpp&gt;
  699. #include &lt;string&gt;
  700. #include &lt;list&gt;
  701. namespace fs = boost::filesystem;
  702. int main()
  703. {
  704. // \u263A is &quot;Unicode WHITE SMILING FACE = have a nice day!&quot;
  705. std::string narrow_string (&quot;smile2&quot;);
  706. std::wstring wide_string (L&quot;smile2\u263A&quot;);
  707. std::list&lt;char&gt; narrow_list;
  708. narrow_list.push_back('s');
  709. narrow_list.push_back('m');
  710. narrow_list.push_back('i');
  711. narrow_list.push_back('l');
  712. narrow_list.push_back('e');
  713. narrow_list.push_back('3');
  714. std::list&lt;wchar_t&gt; wide_list;
  715. wide_list.push_back(L's');
  716. wide_list.push_back(L'm');
  717. wide_list.push_back(L'i');
  718. wide_list.push_back(L'l');
  719. wide_list.push_back(L'e');
  720. wide_list.push_back(L'3');
  721. wide_list.push_back(L'\u263A');
  722. { fs::ofstream f(&quot;smile&quot;); }
  723. { fs::ofstream f(L&quot;smile\u263A&quot;); }
  724. { fs::ofstream f(narrow_string); }
  725. { fs::ofstream f(wide_string); }
  726. { fs::ofstream f(narrow_list); }
  727. { fs::ofstream f(wide_list); }
  728. narrow_list.pop_back();
  729. narrow_list.push_back('4');
  730. wide_list.pop_back();
  731. wide_list.pop_back();
  732. wide_list.push_back(L'4');
  733. wide_list.push_back(L'\u263A');
  734. { fs::ofstream f(fs::path(narrow_list.begin(), narrow_list.end())); }
  735. { fs::ofstream f(fs::path(wide_list.begin(), wide_list.end())); }
  736. return 0;
  737. }<!-- end include file --></pre>
  738. </td>
  739. </tr>
  740. </table>
  741. <p>Testing <code>tut5</code>:</p>
  742. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  743. <tr>
  744. <td align="center"><i><b>Ubuntu Linux </b></i></td>
  745. </tr>
  746. <tr>
  747. <td valign="top">
  748. <pre>$ ./tut5
  749. $ ls smile*
  750. smile smile&#9786; smile2 smile2&#9786; smile3 smile3&#9786; smile4 smile4&#9786;</pre>
  751. </td>
  752. </tr>
  753. </table>
  754. &nbsp;
  755. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  756. <tr>
  757. <td align="center"><i><b>Microsoft Windows</b></i></td>
  758. </tr>
  759. <tr>
  760. <td valign="top">
  761. <pre>&gt;tut5
  762. &gt;dir /b smile*
  763. smile
  764. smile2
  765. smile2&#9786;
  766. smile3
  767. smile3&#9786;
  768. smile4
  769. smile4&#9786;
  770. smile&#9786;</pre>
  771. </td>
  772. </tr>
  773. </table>
  774. <p>The exact appearance of the smiling face will depend on the font,
  775. font size, and other settings for your command line window. The above tests were
  776. run with out-of-the-box Ubuntu 14.04 and Windows 7, US Edition. If you don't get
  777. the above results, take a look at the <code><i>boost-root</i>/libs/filesystem/example/test</code>
  778. directory with your system's GUI file browser, such as Linux Nautilus, Mac OS X
  779. Finder, or Windows Explorer. These tend to be more comfortable with
  780. international character sets than command line interpreters.</p>
  781. <p>Class <code>path</code> takes care of whatever character type or encoding
  782. conversions are required by the particular operating system. Thus as <code>
  783. tut5</code> demonstrates, it's no problem to pass a wide character string to a
  784. Boost.Filesystem operational function even if the underlying operating system
  785. uses narrow characters, and visa versa. And the same applies to user supplied
  786. functions that take <code>const path&amp;</code> arguments.</p>
  787. <p>Class <code>path</code> also provides path syntax that is portable across operating systems,
  788. element iterators, and observer, composition, decomposition, and query
  789. functions to manipulate the elements of a path. The next section of this
  790. tutorial deals with path syntax.</p>
  791. <h2><a name="Class-path-formats">Class path: Generic format vs. Native format</a></h2>
  792. <p>Class <code>path</code> deals with two different pathname
  793. formats - generic format and native format. For POSIX-like
  794. file systems, these formats are the same. But for users of Windows and
  795. other non-POSIX file systems, the distinction is important. Even
  796. programmers writing for POSIX-like systems need to understand the distinction if
  797. they want their code to be portable to non-POSIX systems.</p>
  798. <p>The <b>generic format</b> is the familiar <code>/my_directory/my_file.txt</code> format used by POSIX-like
  799. operating systems such as the Unix variants, Linux, and Mac OS X. Windows also
  800. recognizes the generic format, and it is the basis for the familiar Internet URL
  801. format. The directory
  802. separator character is always one or more slash characters.</p>
  803. <p>The <b>native format</b> is the format as defined by the particular
  804. operating system. For Windows, either the slash or the backslash can be used as
  805. the directory separator character, so <code>/my_directory\my_file.txt</code>
  806. would work fine. Of course, if you write that in a C++ string literal, it
  807. becomes <code>&quot;/my_directory\\my_file.txt&quot;</code>.</p>
  808. <p>If a drive specifier or a backslash appears
  809. in a pathname on a Windows system, it is always treated as the native format.</p>
  810. <p>Class <code>path</code> has observer functions that allow you to
  811. obtain the string representation of a path object in either the native format
  812. or the generic format. See the <a href="#Class path-iterators-etc">next section</a>
  813. for how that plays out.</p>
  814. <p>The distinction between generic format and native format is important when
  815. communicating with native C-style API's and with users. Both tend to expect
  816. paths in the native format and may be confused by the generic format. The generic
  817. format is great, however, for writing portable programs that work regardless
  818. of operating system.</p>
  819. <p>The next section covers class <code>path</code> observers, composition,
  820. decomposition, query, and iteration over the elements of a path.</p>
  821. <h2><a name="Class path-iterators-etc">Class path: Iterators, observers, composition, decomposition, and query</a>
  822. - (<a href="../example/path_info.cpp">path_info.cpp</a>)</h2>
  823. <p>The <code><a href="../example/path_info.cpp">path_info.cpp</a></code> program is handy for learning how class <code>path</code>
  824. iterators,
  825. observers, composition, decomposition, and query functions work on your system.
  826. It is one of the programs built by the <code>build.sh</code> and <code>build.bat</code>
  827. scripts:</p>
  828. <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  829. <tr>
  830. <td>
  831. <pre><!-- include file "../example/path_info.cpp" -->#include &lt;iostream&gt;
  832. #include &lt;boost/filesystem.hpp&gt;
  833. using namespace std;
  834. using namespace boost::filesystem;
  835. const char * say_what(bool b) { return b ? &quot;true&quot; : &quot;false&quot;; }
  836. int main(int argc, char* argv[])
  837. {
  838. if (argc &lt; 2)
  839. {
  840. cout &lt;&lt; &quot;Usage: path_info path-element [path-element...]\n&quot;
  841. &quot;Composes a path via operator/= from one or more path-element arguments\n&quot;
  842. &quot;Example: path_info foo/bar baz\n&quot;
  843. # ifdef BOOST_POSIX_API
  844. &quot; would report info about the composed path foo/bar/baz\n&quot;;
  845. # else // BOOST_WINDOWS_API
  846. &quot; would report info about the composed path foo/bar\\baz\n&quot;;
  847. # endif
  848. return 1;
  849. }
  850. path p;
  851. for (; argc &gt; 1; --argc, ++argv)
  852. p /= argv[1]; // compose path p from the command line arguments
  853. cout &lt;&lt; &quot;\ncomposed path:\n&quot;;
  854. cout &lt;&lt; &quot; operator&lt;&lt;()---------: &quot; &lt;&lt; p &lt;&lt; &quot;\n&quot;;
  855. cout &lt;&lt; &quot; make_preferred()-----: &quot; &lt;&lt; p.make_preferred() &lt;&lt; &quot;\n&quot;;
  856. cout &lt;&lt; &quot;\nelements:\n&quot;;
  857. for (auto element : p)
  858. cout &lt;&lt; &quot; &quot; &lt;&lt; element &lt;&lt; '\n';
  859. cout &lt;&lt; &quot;\nobservers, native format:&quot; &lt;&lt; endl;
  860. # ifdef BOOST_POSIX_API
  861. cout &lt;&lt; &quot; native()-------------: &quot; &lt;&lt; p.native() &lt;&lt; endl;
  862. cout &lt;&lt; &quot; c_str()--------------: &quot; &lt;&lt; p.c_str() &lt;&lt; endl;
  863. # else // BOOST_WINDOWS_API
  864. wcout &lt;&lt; L&quot; native()-------------: &quot; &lt;&lt; p.native() &lt;&lt; endl;
  865. wcout &lt;&lt; L&quot; c_str()--------------: &quot; &lt;&lt; p.c_str() &lt;&lt; endl;
  866. # endif
  867. cout &lt;&lt; &quot; string()-------------: &quot; &lt;&lt; p.string() &lt;&lt; endl;
  868. wcout &lt;&lt; L&quot; wstring()------------: &quot; &lt;&lt; p.wstring() &lt;&lt; endl;
  869. cout &lt;&lt; &quot;\nobservers, generic format:\n&quot;;
  870. cout &lt;&lt; &quot; generic_string()-----: &quot; &lt;&lt; p.generic_string() &lt;&lt; endl;
  871. wcout &lt;&lt; L&quot; generic_wstring()----: &quot; &lt;&lt; p.generic_wstring() &lt;&lt; endl;
  872. cout &lt;&lt; &quot;\ndecomposition:\n&quot;;
  873. cout &lt;&lt; &quot; root_name()----------: &quot; &lt;&lt; p.root_name() &lt;&lt; '\n';
  874. cout &lt;&lt; &quot; root_directory()-----: &quot; &lt;&lt; p.root_directory() &lt;&lt; '\n';
  875. cout &lt;&lt; &quot; root_path()----------: &quot; &lt;&lt; p.root_path() &lt;&lt; '\n';
  876. cout &lt;&lt; &quot; relative_path()------: &quot; &lt;&lt; p.relative_path() &lt;&lt; '\n';
  877. cout &lt;&lt; &quot; parent_path()--------: &quot; &lt;&lt; p.parent_path() &lt;&lt; '\n';
  878. cout &lt;&lt; &quot; filename()-----------: &quot; &lt;&lt; p.filename() &lt;&lt; '\n';
  879. cout &lt;&lt; &quot; stem()---------------: &quot; &lt;&lt; p.stem() &lt;&lt; '\n';
  880. cout &lt;&lt; &quot; extension()----------: &quot; &lt;&lt; p.extension() &lt;&lt; '\n';
  881. cout &lt;&lt; &quot;\nquery:\n&quot;;
  882. cout &lt;&lt; &quot; empty()--------------: &quot; &lt;&lt; say_what(p.empty()) &lt;&lt; '\n';
  883. cout &lt;&lt; &quot; is_absolute()--------: &quot; &lt;&lt; say_what(p.is_absolute()) &lt;&lt; '\n';
  884. cout &lt;&lt; &quot; has_root_name()------: &quot; &lt;&lt; say_what(p.has_root_name()) &lt;&lt; '\n';
  885. cout &lt;&lt; &quot; has_root_directory()-: &quot; &lt;&lt; say_what(p.has_root_directory()) &lt;&lt; '\n';
  886. cout &lt;&lt; &quot; has_root_path()------: &quot; &lt;&lt; say_what(p.has_root_path()) &lt;&lt; '\n';
  887. cout &lt;&lt; &quot; has_relative_path()--: &quot; &lt;&lt; say_what(p.has_relative_path()) &lt;&lt; '\n';
  888. cout &lt;&lt; &quot; has_parent_path()----: &quot; &lt;&lt; say_what(p.has_parent_path()) &lt;&lt; '\n';
  889. cout &lt;&lt; &quot; has_filename()-------: &quot; &lt;&lt; say_what(p.has_filename()) &lt;&lt; '\n';
  890. cout &lt;&lt; &quot; has_stem()-----------: &quot; &lt;&lt; say_what(p.has_stem()) &lt;&lt; '\n';
  891. cout &lt;&lt; &quot; has_extension()------: &quot; &lt;&lt; say_what(p.has_extension()) &lt;&lt; '\n';
  892. return 0;
  893. }<!-- end include file --></pre>
  894. </td>
  895. </tr>
  896. </table>
  897. <p>Run the examples below on your system, and try some different path arguments
  898. as we go along. Here is the invocation we will talk about in detail:</p>
  899. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  900. <tr>
  901. <td align="center"><i><b>Ubuntu Linux </b></i></td>
  902. </tr>
  903. <tr>
  904. <td>
  905. <pre>$ ./path_info /foo bar baa.txt
  906. composed path:
  907. operator<<()---------: "/foo/bar/baa.txt"
  908. make_preferred()-----: "/foo/bar/baa.txt"
  909. elements:
  910. "/"
  911. "foo"
  912. "bar"
  913. "baa.txt"
  914. observers, native format:
  915. native()-------------: /foo/bar/baa.txt
  916. c_str()--------------: /foo/bar/baa.txt
  917. string()-------------: /foo/bar/baa.txt
  918. wstring()------------: /foo/bar/baa.txt
  919. observers, generic format:
  920. generic_string()-----: /foo/bar/baa.txt
  921. generic_wstring()----: /foo/bar/baa.txt
  922. decomposition:
  923. root_name()----------: ""
  924. root_directory()-----: "/"
  925. root_path()----------: "/"
  926. relative_path()------: "foo/bar/baa.txt"
  927. parent_path()--------: "/foo/bar"
  928. filename()-----------: "baa.txt"
  929. stem()---------------: "baa"
  930. extension()----------: ".txt"
  931. query:
  932. empty()--------------: false
  933. is_absolute()--------: true
  934. has_root_name()------: false
  935. has_root_directory()-: true
  936. has_root_path()------: true
  937. has_relative_path()--: true
  938. has_parent_path()----: true
  939. has_filename()-------: true
  940. has_stem()-----------: true
  941. has_extension()------: true</pre>
  942. </td>
  943. </tr>
  944. </table>
  945. &nbsp;
  946. <table border="1" cellpadding="5" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  947. <tr>
  948. <td align="center"><i><b>Microsoft Windows</b></i></td>
  949. </tr>
  950. <tr>
  951. <td>
  952. <pre>&gt;path_info \foo bar baa.txt
  953. composed path:
  954. operator&lt;&lt;()---------: &quot;\foo\bar\baa.txt&quot;
  955. make_preferred()-----: &quot;\foo\bar\baa.txt&quot;
  956. elements:
  957. &quot;/&quot;
  958. &quot;foo&quot;
  959. &quot;bar&quot;
  960. &quot;baa.txt&quot;
  961. observers, native format:
  962. native()-------------: \foo\bar\baa.txt
  963. c_str()--------------: \foo\bar\baa.txt
  964. string()-------------: \foo\bar\baa.txt
  965. wstring()------------: \foo\bar\baa.txt
  966. observers, generic format:
  967. generic_string()-----: /foo/bar/baa.txt
  968. generic_wstring()----: /foo/bar/baa.txt
  969. decomposition:
  970. root_name()----------: &quot;&quot;
  971. root_directory()-----: &quot;\&quot;
  972. root_path()----------: &quot;\&quot;
  973. relative_path()------: &quot;foo\bar\baa.txt&quot;
  974. parent_path()--------: &quot;\foo\bar&quot;
  975. filename()-----------: &quot;baa.txt&quot;
  976. stem()---------------: &quot;baa&quot;
  977. extension()----------: &quot;.txt&quot;
  978. query:
  979. empty()--------------: false
  980. is_absolute()--------: false
  981. has_root_name()------: false
  982. has_root_directory()-: true
  983. has_root_path()------: true
  984. has_relative_path()--: true
  985. has_parent_path()----: true
  986. has_filename()-------: true
  987. has_stem()-----------: true
  988. has_extension()------: true</pre>
  989. </td>
  990. </tr>
  991. </table>
  992. <p>We will go through the above code in detail to gain a better
  993. understanding of what is going on.</p>
  994. <p dir="ltr">A common need is to compose a path from its constituent
  995. directories. Class <code>path</code> uses <code>/</code> and <code>/=</code> operators to
  996. append elements. That's a reminder
  997. that these operations append the operating system's preferred directory
  998. separator if needed. The preferred
  999. directory separator is a slash on POSIX-like systems, and a backslash on
  1000. Windows-like systems.</p>
  1001. <p dir="ltr">That&#39;s what this code does before displaying the resulting <code>
  1002. path p</code> using the <code>class path</code> stream inserter: </p>
  1003. <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  1004. <tr>
  1005. <td>
  1006. <pre><!-- include file "../example/path_info.cpp" --> path p;
  1007. for (; argc &gt; 1; --argc, ++argv)
  1008. p /= argv[1]; // compose path p from the command line arguments
  1009. cout &lt;&lt; &quot;\ncomposed path:\n&quot;;
  1010. cout &lt;&lt; &quot; operator&lt;&lt;()---------: &quot; &lt;&lt; p &lt;&lt; &quot;\n&quot;;
  1011. cout &lt;&lt; &quot; make_preferred()-----: &quot; &lt;&lt; p.make_preferred() &lt;&lt; &quot;\n&quot;;</pre>
  1012. </td>
  1013. </tr>
  1014. </table>
  1015. <p>One abstraction for thinking about a path is as a sequence of elements, where
  1016. the elements are directory and file names. To support this abstraction, class
  1017. <code>path</code> provides STL-like&nbsp; iterators and also <code>begin()</code>
  1018. and <code>end()</code> functions.</p>
  1019. <p>Here is the code that produced the list of elements in the above output listing:</p>
  1020. <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  1021. <tr>
  1022. <td>
  1023. <pre>cout &lt;&lt; &quot;\nelements:\n&quot;;
  1024. for (auto element : p)
  1025. cout &lt;&lt; &quot; &quot; &lt;&lt; element &lt;&lt; &#39;\n&#39;;</pre>
  1026. </td>
  1027. </tr>
  1028. </table>
  1029. <p>Let&#39;s look at class path observer functions:</p>
  1030. <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  1031. <tr>
  1032. <td>
  1033. <pre><!-- include file "../example/path_info.cpp" --> cout &lt;&lt; &quot;\nobservers, native format:&quot; &lt;&lt; endl;
  1034. # ifdef BOOST_POSIX_API
  1035. cout &lt;&lt; &quot; native()-------------: &quot; &lt;&lt; p.native() &lt;&lt; endl;
  1036. cout &lt;&lt; &quot; c_str()--------------: &quot; &lt;&lt; p.c_str() &lt;&lt; endl;
  1037. # else // BOOST_WINDOWS_API
  1038. wcout &lt;&lt; L&quot; native()-------------: &quot; &lt;&lt; p.native() &lt;&lt; endl;
  1039. wcout &lt;&lt; L&quot; c_str()--------------: &quot; &lt;&lt; p.c_str() &lt;&lt; endl;
  1040. # endif
  1041. cout &lt;&lt; &quot; string()-------------: &quot; &lt;&lt; p.string() &lt;&lt; endl;
  1042. wcout &lt;&lt; L&quot; wstring()------------: &quot; &lt;&lt; p.wstring() &lt;&lt; endl;
  1043. cout &lt;&lt; &quot;\nobservers, generic format:\n&quot;;
  1044. cout &lt;&lt; &quot; generic_string()-----: &quot; &lt;&lt; p.generic_string() &lt;&lt; endl;
  1045. wcout &lt;&lt; L&quot; generic_wstring()----: &quot; &lt;&lt; p.generic_wstring() &lt;&lt; endl;</pre>
  1046. </td>
  1047. </tr>
  1048. </table>
  1049. <p>Native format observers should be used when interacting with the
  1050. operating system or with users; that's what they expect.</p>
  1051. <p>Generic format observers should be used when the results need to be
  1052. portable and uniform regardless of the operating system.</p>
  1053. <p><code>path</code> objects always hold pathnames in the native
  1054. format, but otherwise leave them unchanged from their source. The
  1055. <a href="reference.html#preferred">preferred()</a> function will convert to the
  1056. preferred form, if the native format has several forms. Thus on Windows, it will
  1057. convert slashes to backslashes.</p>
  1058. <p>Moving on to decomposition:</p>
  1059. <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  1060. <tr>
  1061. <td>
  1062. <pre><!-- include file "../example/path_info.cpp" --> cout &lt;&lt; &quot;\ndecomposition:\n&quot;;
  1063. cout &lt;&lt; &quot; root_name()----------: &quot; &lt;&lt; p.root_name() &lt;&lt; '\n';
  1064. cout &lt;&lt; &quot; root_directory()-----: &quot; &lt;&lt; p.root_directory() &lt;&lt; '\n';
  1065. cout &lt;&lt; &quot; root_path()----------: &quot; &lt;&lt; p.root_path() &lt;&lt; '\n';
  1066. cout &lt;&lt; &quot; relative_path()------: &quot; &lt;&lt; p.relative_path() &lt;&lt; '\n';
  1067. cout &lt;&lt; &quot; parent_path()--------: &quot; &lt;&lt; p.parent_path() &lt;&lt; '\n';
  1068. cout &lt;&lt; &quot; filename()-----------: &quot; &lt;&lt; p.filename() &lt;&lt; '\n';
  1069. cout &lt;&lt; &quot; stem()---------------: &quot; &lt;&lt; p.stem() &lt;&lt; '\n';
  1070. cout &lt;&lt; &quot; extension()----------: &quot; &lt;&lt; p.extension() &lt;&lt; '\n';</pre>
  1071. </td>
  1072. </tr>
  1073. </table>
  1074. <p>&nbsp;And, finally, query functions:</p>
  1075. <table border="1" cellpadding="3" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" bgcolor="#D7EEFF">
  1076. <tr>
  1077. <td>
  1078. <pre><!-- include file "../example/path_info.cpp" --> cout &lt;&lt; &quot;\nquery:\n&quot;;
  1079. cout &lt;&lt; &quot; empty()--------------: &quot; &lt;&lt; say_what(p.empty()) &lt;&lt; '\n';
  1080. cout &lt;&lt; &quot; is_absolute()--------: &quot; &lt;&lt; say_what(p.is_absolute()) &lt;&lt; '\n';
  1081. cout &lt;&lt; &quot; has_root_name()------: &quot; &lt;&lt; say_what(p.has_root_name()) &lt;&lt; '\n';
  1082. cout &lt;&lt; &quot; has_root_directory()-: &quot; &lt;&lt; say_what(p.has_root_directory()) &lt;&lt; '\n';
  1083. cout &lt;&lt; &quot; has_root_path()------: &quot; &lt;&lt; say_what(p.has_root_path()) &lt;&lt; '\n';
  1084. cout &lt;&lt; &quot; has_relative_path()--: &quot; &lt;&lt; say_what(p.has_relative_path()) &lt;&lt; '\n';
  1085. cout &lt;&lt; &quot; has_parent_path()----: &quot; &lt;&lt; say_what(p.has_parent_path()) &lt;&lt; '\n';
  1086. cout &lt;&lt; &quot; has_filename()-------: &quot; &lt;&lt; say_what(p.has_filename()) &lt;&lt; '\n';
  1087. cout &lt;&lt; &quot; has_stem()-----------: &quot; &lt;&lt; say_what(p.has_stem()) &lt;&lt; '\n';
  1088. cout &lt;&lt; &quot; has_extension()------: &quot; &lt;&lt; say_what(p.has_extension()) &lt;&lt; '\n';</pre>
  1089. </td>
  1090. </tr>
  1091. </table>
  1092. <p>These are pretty self-evident, but do note the difference in the
  1093. result of <code>is_absolute()</code> between Linux and Windows. Because there is
  1094. no root name (i.e. drive specifier or network name), a lone slash (or backslash)
  1095. is a relative path on Windows but an absolute path on POSIX-like operating
  1096. systems. </p>
  1097. <h2><a name="Error-reporting">Error reporting</a></h2>
  1098. <p>The Boost.Filesystem <code>file_size</code> function, like many of the
  1099. operational functions, has two overloads:</p>
  1100. <blockquote>
  1101. <pre>uintmax_t <a name="file_size">file_size</a>(const path&amp; p);
  1102. uintmax_t <a name="file_size2">file_size</a>(const path&amp; p, system::error_code&amp; ec);</pre>
  1103. </blockquote>
  1104. <p>The only significant difference between the two is how they report errors.</p>
  1105. <p>The
  1106. first signature will throw exceptions to report errors. A <code>
  1107. <a href="reference.html#Class-filesystem_error">filesystem_error</a></code> exception will be thrown
  1108. on an
  1109. operational error. <code>filesystem_error</code> is derived from <code>std::runtime_error</code>.
  1110. It has a
  1111. member function to obtain the <code>
  1112. <a href="../../system/doc/reference.html#Class-error_code">error_code</a></code> reported by the source
  1113. of the error. It also has member functions to obtain the path or paths that caused
  1114. the error.</p>
  1115. <blockquote>
  1116. <p><b>Motivation for the second signature:</b> Throwing exceptions on errors was the entire error reporting story for the earliest versions of
  1117. Boost.Filesystem, and indeed throwing exceptions on errors works very well for
  1118. many applications. But user reports trickled in that some code became so
  1119. littered with try and catch blocks as to be unreadable and unmaintainable. In
  1120. some applications I/O errors aren't exceptional, and that's the use case for
  1121. the second signature.</p>
  1122. </blockquote>
  1123. <p>Functions with a <code>system::error_code&amp;</code> argument set that
  1124. argument to report operational error status, and so do not throw exceptions when I/O
  1125. related errors occur. For a full explanation, see
  1126. <a href="reference.html#Error-reporting">Error reporting</a> in the reference
  1127. documentation. </p>
  1128. <hr>
  1129. <p>&copy; Copyright Beman Dawes 2010, 2015</p>
  1130. <p>Distributed under the Boost Software License, Version 1.0. See
  1131. <a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a></p>
  1132. <p>Revised
  1133. <!--webbot bot="Timestamp" S-Type="EDITED" S-Format="%d %B %Y" startspan -->07 August 2017<!--webbot bot="Timestamp" endspan i-checksum="31490" --></p>
  1134. </body>
  1135. </html>