﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-农妇，山泉，有点儿田-随笔分类-Boost</title><link>http://www.cppblog.com/deadpunk/category/2134.html</link><description>我没有技术</description><language>zh-cn</language><lastBuildDate>Sun, 23 Nov 2008 14:43:33 GMT</lastBuildDate><pubDate>Sun, 23 Nov 2008 14:43:33 GMT</pubDate><ttl>60</ttl><item><title>(转载)了解 Boost Filesystem Library</title><link>http://www.cppblog.com/deadpunk/archive/2008/11/23/67670.html</link><dc:creator>活着就是折腾，所以当然要骠悍的折腾</dc:creator><author>活着就是折腾，所以当然要骠悍的折腾</author><pubDate>Sun, 23 Nov 2008 08:31:00 GMT</pubDate><guid>http://www.cppblog.com/deadpunk/archive/2008/11/23/67670.html</guid><wfw:comment>http://www.cppblog.com/deadpunk/comments/67670.html</wfw:comment><comments>http://www.cppblog.com/deadpunk/archive/2008/11/23/67670.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/deadpunk/comments/commentRss/67670.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/deadpunk/services/trackbacks/67670.html</trackback:ping><description><![CDATA[<blockquote>作者:<a href="http://www.ibm.com/developerworks/cn/aix/library/au-boostfs/#author" cmimpressionsent="1"><font color=#996699>Arpan Sen</font></a> (<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#97;&#114;&#112;&#97;&#110;&#64;&#115;&#121;&#110;&#99;&#97;&#100;&#46;&#99;&#111;&#109;&#63;&#115;&#117;&#98;&#106;&#101;&#99;&#116;&#61;&#20102;&#35299;&#32;&#66;&#111;&#111;&#115;&#116;&#32;&#70;&#105;&#108;&#101;&#115;&#121;&#115;&#116;&#101;&#109;&#32;&#76;&#105;&#98;&#114;&#97;&#114;&#121;" cmimpressionsent="1"><font color=#5c81a7>arpan@syncad.com</font></a>), 技术负责人, Synapti Computer Aided Design Pvt Ltd<br><a title="Boost FileSystem" href="http://www.ibm.com/developerworks/cn/aix/library/au-boostfs/" target=_blank><font color=#1d58d1>来源:http://www.ibm.com/developerworks/cn/aix/library/au-boostfs/<br></font></a>缺乏定义良好的、用于处理文件系统操作的库，这一直是 <code>C++</code> 语言存在的一个问题。过去，程序员必须使用本机 API 来解决此问题。通过本文您将了解一个提供安全、可移植且易用的 <code>C++</code> 接口来促进文件系统操作的库：Boost Filesystem Library。 </blockquote><!--start RESERVED FOR FUTURE USE INCLUDE FILES--><!-- include java script once we verify teams wants to use this and it will work on dbcs and cyrillic characters --><!--end RESERVED FOR FUTURE USE INCLUDE FILES-->
<p><code>C++</code> 语言（实际上是 <code>C++</code> 标准）的最常见问题之一是，缺乏定义良好的库来帮助处理文件系统查询和操作。由于这个原因，程序员不得不使用本机操作系统提供的应用程序编程接口（Application Program Interfaces，API），而这使得代码不能在平台之间移植。以下面的简单情况为例：您需要确定某个文件是否是 Directory 类型。在 Microsoft&#174; Windows&#174; 平台中，可以通过调用 <code>GetAttributes</code> 库函数（在 windows.h 头文件中定义）进行此操作： </p>
<table cellSpacing=0 cellPadding=0 width="65%" border=0>
    <tbody>
        <tr>
            <td class=code-outline>
            <pre class=displaycode>DWORD GetFileAttributes (LPCTSTR <em>lpFileName</em>);
            </pre>
            </td>
        </tr>
    </tbody>
</table>
<br>
<p>对于目录，所得到的结果应该为 FILE_ATTRIBUTE_DIRECTORY，而您的代码必须检查是否为此结果。在 UNIX&#174; 和 Linux&#174; 平台上，可以通过使用 <code>stat</code> 或 <code>fstat</code> 函数及 sys/stat.h 中定义的 S_ISDIR 宏来实现相同的功能。您还必须理解 <code>stat</code> 结构。下面是对应的代码： </p>
<table cellSpacing=0 cellPadding=0 width="65%" border=0>
    <tbody>
        <tr>
            <td class=code-outline>
            <pre class=displaycode>#include &lt;sys/stat.h&gt;
            #include &lt;stdio.h&gt;
            int main()
            {
            struct stat s1;
            int status = stat(&lt;const char* denoting pathname&gt;, &amp;s1);
            printf(&#8220;Path is a directory : %d\n&#8221;, S_ISDIR(s1.st_mode));
            return 0;
            }
            </pre>
            </td>
        </tr>
    </tbody>
</table>
<br>
<p>对于 I/O 操作较多的程序，这样的不一致就意味着需要进行大量的工程工作才能在平台间移植代码。正是因为这个原因，我们才引入了 Boost Filesystem Library。这个广泛使用的库提供了安全、可移植且易用的 <code>C++</code> 接口，用于执行文件系统操作。可以从 <a href="http://www.boost.org/libs/filesystem" cmimpressionsent="1"><font color=#5c81a7>Boost</font></a> 站点免费下载此库。 </p>
<p><a name=N10096><font color=#1d58d1><span class=atitle>使用 boost::filesystem 的第一个程序</span> </font></a></p>
<p>在深入研究 Boost Filesystem Library 的更多细节之前，请看一下<a href="http://www.ibm.com/developerworks/cn/aix/library/au-boostfs/#list1" cmimpressionsent="1"><font color=#996699>清单 1</font></a> 中所示的代码；此代码使用 Boost API 确定某个文件的类型是否为 Directory。 </p>
<br><a name=list1><font color=#1d58d1><strong>清单 1. 用于确定某个文件的类型是否为 Directory 的代码</strong> </font></a><br>
<table cellSpacing=0 cellPadding=0 width="65%" border=0>
    <tbody>
        <tr>
            <td class=code-outline>
            <pre class=displaycode>
            #include &lt;stdio.h&gt;
            #include &#8220;boost/filesystem.hpp&#8221;
            int main()
            {
            boost::filesystem::path path("/usr/local/include"); // random pathname
            bool result = boost::filesystem::is_directory(path);
            printf(&#8220;Path is a directory : %d\n&#8221;, result);
            return 0;
            }
            </pre>
            </td>
        </tr>
    </tbody>
</table>
<br>
<p>此代码非常明了易懂，您并不需要了解任何系统特定的例程。此代码经过验证，能在不用修改的情况下在 gcc-3.4.4 和 cl-13.10.3077 上成功编译。 </p>
<br>
<table cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr>
            <td><img height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"> <br><img height=6 alt="" src="http://www.ibm.com/i/c.gif" width=8 border=0> </td>
        </tr>
    </tbody>
</table>
<table class=no-print cellSpacing=0 cellPadding=0 align=right>
    <tbody>
        <tr align=right>
            <td><img height=4 alt="" src="http://www.ibm.com/i/c.gif" width="100%"> <br>
            <table cellSpacing=0 cellPadding=0 border=0>
                <tbody>
                    <tr>
                        <td vAlign=center><img height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0> <br></td>
                        <td vAlign=top align=right><a class=fbox href="http://www.ibm.com/developerworks/cn/aix/library/au-boostfs/#main" cmimpressionsent="1"><strong><font color=#996699>回页首</font><font color=#1d58d1> </font></strong></a></td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
    </tbody>
</table>
<br><br>
<p><a name=N100AF><font color=#1d58d1><span class=atitle>了解 Boost path 对象</span> </font></a></p>
<p>了解 Boost Filesystem Library 的关键是 <code>path</code> 对象，因为 Filesystem Library 中定义的多个例程都要对相应的 <code>path</code> 对象操作。文件系统路径通常依赖于操作系统。例如，众所周知，UNIX 和 Linux 系统使用正斜杠 (<code> /</code>) 字符作为目录分隔符，而 Windows 将反斜杠 (<code>\</code>) 字符用于类似的用途。<code>boost::filesystem::path</code> 旨在准确地抽象此特性。<code>path</code> 对象可以通过多种方式进行初始化，最常见的方式是使用 <code>char*</code> 或 <code>std::string</code> 进行初始化，如<a href="http://www.ibm.com/developerworks/cn/aix/library/au-boostfs/#list2" cmimpressionsent="1"><font color=#996699>清单 2</font></a> 中所示。 </p>
<br><a name=list2><font color=#1d58d1><strong>清单 2. 创建 Boost path 对象的方法</strong> </font></a><br>
<table cellSpacing=0 cellPadding=0 width="65%" border=0>
    <tbody>
        <tr>
            <td class=code-outline>
            <pre class=displaycode>
            path(); // empty path
            path(const char* pathname);
            path(const std::string&amp; pathname);
            path(const char* pathname, boost::filesystem::path::name_check checker);
            path(const char* pathname, boost::filesystem::path::name_check checker);
            </pre>
            </td>
        </tr>
    </tbody>
</table>
<br>
<p>在初始化 <code>path</code> 对象时，可以采用本机格式或可移植操作系统接口（Portable Operating System Interface，POSIX）委员会定义的可移植格式提供 PATHNAME 变量。这两种方法在实际中各有优缺点。考虑以下情况：您希望操作软件所创建的目录，此目录在 UNIX 和 Linux 系统上位于 /tmp/mywork，而在 Windows 上位于 C:\tmp\mywork。可以采用多种方法处理问题。<a href="http://www.ibm.com/developerworks/cn/aix/library/au-boostfs/#list3" cmimpressionsent="1"><font color=#996699>清单 3</font></a> 显示了面向本机格式的方法。 </p>
<br><a name=list3><font color=#1d58d1><strong>清单 3. 使用本机格式初始化 path</strong> </font></a><br>
<table cellSpacing=0 cellPadding=0 width="65%" border=0>
    <tbody>
        <tr>
            <td class=code-outline>
            <pre class=displaycode>
            #ifdef UNIX
            boost::filesystem::path path("/tmp/mywork");
            #else
            boost::filesystem::path path("C:\\tmp\\mywork ");
            #endif
            </pre>
            </td>
        </tr>
    </tbody>
</table>
<br>
<p>需要单个 <code>#ifdef</code> 来按操作系统初始化 path 对象。不过，如果您喜欢使用可移植格式，请参见<a href="http://www.ibm.com/developerworks/cn/aix/library/au-boostfs/#list4" cmimpressionsent="1"><font color=#996699>清单 4</font></a>。 </p>
<br><a name=list4><font color=#1d58d1><strong>清单 4. 使用可移植格式初始化 path</strong> </font></a><br>
<table cellSpacing=0 cellPadding=0 width="65%" border=0>
    <tbody>
        <tr>
            <td class=code-outline>
            <pre class=displaycode>
            boost::filesystem::path path("/tmp/mywork");
            </pre>
            </td>
        </tr>
    </tbody>
</table>
<br>
<p>请注意，<code>path::name_check</code> 指的是一个名称检查函数原型。如果其参数输入 PATHNAME 对于特定的操作系统或文件系统有效，名称检查函数将返回&#8220;True&#8221;。Boost Filesystem Library 提供了多个名称检查函数，而且也欢迎您提供自己的变体。常用的名称检查函数是 Boost 提供的 <code>portable_posix_name</code> 和 <code>windows_name</code>。 </p>
<br>
<table cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr>
            <td><img height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"> <br><img height=6 alt="" src="http://www.ibm.com/i/c.gif" width=8 border=0> </td>
        </tr>
    </tbody>
</table>
<table class=no-print cellSpacing=0 cellPadding=0 align=right>
    <tbody>
        <tr align=right>
            <td><img height=4 alt="" src="http://www.ibm.com/i/c.gif" width="100%"> <br>
            <table cellSpacing=0 cellPadding=0 border=0>
                <tbody>
                    <tr>
                        <td vAlign=center><img height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0> <br></td>
                        <td vAlign=top align=right><a class=fbox href="http://www.ibm.com/developerworks/cn/aix/library/au-boostfs/#main" cmimpressionsent="1"><strong><font color=#996699>回页首</font><font color=#1d58d1> </font></strong></a></td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
    </tbody>
</table>
<br><br>
<p><a name=N1011E><font color=#1d58d1><span class=atitle>path 成员函数概述</span> </font></a></p>
<p><code>path</code> 对象提供了多个成员方法。这些成员例程并不会修改文件系统，但会根据 path 名称提供有用的信息。此部分提供了其中几个例程的概述： </p>
<ul>
    <li><strong><code>const std::string&amp; string( )</code>：</strong>此例程会返回用于初始化 path 的字符串的副本，其格式符合 path 语法规则。
    <li><strong><code>std::string root_directory( )</code>：</strong>在提供了路径的情况下，此 API 将返回根目录，否则将返回空字符串。例如，如果路径包含 <em>/tmp/var1</em>，则此例程将返回 <code>/</code>，即 UNIX 文件系统的根。不过，如果路径是相对路径，如 <em>../mywork/bin</em>，此例程将返回空字符串。
    <li><strong><code>std::string root_name( )</code>：</strong>在给定从文件系统根目录开始的路径的情况下，此例程将返回包含 PATHNAME 的第一个字符的字符串。
    <li><strong><code>std::string leaf( )</code>：</strong>在给定绝对路径名称（例如，/home/user1/file2）的情况下，此例程将提供与文件名称对应的字符串（即 file2）。
    <li><strong><code>std::string branch_path( )</code>：</strong>这是与 <code>leaf</code> 互补的例程。在给定路径的情况下，将会返回其构造所用的所有元素（除了最后一个元素）。例如，对于使用 <em>/a/b/c</em> 初始化的 path，<code>path.branch_path( )</code> 将返回 <code>/a/b</code>。对于包含单个元素的路径，如 <em>c</em>，此例程将返回空字符串。
    <li><strong><code>bool empty( )</code>：</strong>如果 path 对象包含空字符串（例如 <em>path path1("")</em>），则此例程将返回 True。
    <li><strong><code>boost::filesystem::path::iterator</code>：</strong>此例程用于遍历 path 的各个元素。请看<a href="http://www.ibm.com/developerworks/cn/aix/library/au-boostfs/#list5" cmimpressionsent="1"><font color=#996699>清单 5</font></a> 所示的代码。 <br><br><a name=list5><strong><font color=#1d58d1>清单 5. 使用 path::iterator（begin 和 end 接口）</font></strong></a><br>
    <table cellSpacing=0 cellPadding=0 width="65%" border=0>
        <tbody>
            <tr>
                <td class=code-outline>
                <pre class=displaycode>
                #include &lt;iostream&gt;
                #include &#8220;boost/filesystem.hpp&#8221;
                int main()
                {
                boost::filesystem::path path1("/usr/local/include"); // random pathname
                boost::filesystem::path::iterator pathI = path1.begin();
                while (pathI != path1.end())
                {
                std::cout &lt;&lt; *pathI &lt;&lt; std::endl;
                ++pathI;
                }
                return 0;
                }
                // result: 1
                </pre>
                </td>
            </tr>
        </tbody>
    </table>
    <br>
    <p>上述程序的输出依次是 <code>/</code>、<code>usr</code>、<code>local</code>、<code>include</code>，代表了该目录的层次结构。 </p>
    <li><strong><code>path operator / (char* lhs, const path&amp; rhs)</code>：</strong>此例程是 <code>path</code> 的非成员函数。它将返回使用 <code>lhs</code> 和 <code>rhs</code> 形成的路径的串联值。它将自动插入 <code>/</code> 作为路径分隔符，如<a href="http://www.ibm.com/developerworks/cn/aix/library/au-boostfs/#list6" cmimpressionsent="1"><font color=#996699>清单 6</font></a> 中所示。 <br><br><a name=list6><strong><font color=#1d58d1>清单 6. 路径字符串的串联</font></strong></a><br>
    <table cellSpacing=0 cellPadding=0 width="65%" border=0>
        <tbody>
            <tr>
                <td class=code-outline>
                <pre class=displaycode>
                #include &lt;iostream&gt;
                #include &#8220;boost/filesystem.hpp&#8221;
                int main()
                {
                boost::filesystem::path path1("/usr/local/include"); // random pathname
                boost::filesystem::path::iterator pathI = path1.begin();
                while (pathI != path1.end())
                {
                std::cout &lt;&lt; *pathI &lt;&lt; std::endl;
                ++pathI;
                }
                return 0;
                }
                // result: 1
                </pre>
                </td>
            </tr>
        </tbody>
    </table>
    <br></li>
</ul>
<br>
<table cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr>
            <td><img height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"> <br><img height=6 alt="" src="http://www.ibm.com/i/c.gif" width=8 border=0> </td>
        </tr>
    </tbody>
</table>
<table class=no-print cellSpacing=0 cellPadding=0 align=right>
    <tbody>
        <tr align=right>
            <td><img height=4 alt="" src="http://www.ibm.com/i/c.gif" width="100%"> <br>
            <table cellSpacing=0 cellPadding=0 border=0>
                <tbody>
                    <tr>
                        <td vAlign=center><img height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0> <br></td>
                        <td vAlign=top align=right><a class=fbox href="http://www.ibm.com/developerworks/cn/aix/library/au-boostfs/#main" cmimpressionsent="1"><strong><font color=#996699>回页首</font><font color=#1d58d1> </font></strong></a></td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
    </tbody>
</table>
<br><br>
<p><a name=N101DB><font color=#1d58d1><span class=atitle>错误处理</span> </font></a></p>
<p>文件系统操作经常遇到意外的问题，Boost Filesystem Library 将使用 <code>C++</code> 异常报告运行时错误。<code>boost::filesystem_error</code> 类派生自 <code>std::runtime_error</code> 类。库中的函数使用 <code>filesystem_error</code> 异常报告操作错误。与不同的可能错误类型对应，Boost 头文件定义了相应的错误代码。用户代码通常驻留在 <code>try...catch</code> 块内，使用 <code>filesystem_error</code> 异常来报告相关错误消息。<a href="http://www.ibm.com/developerworks/cn/aix/library/au-boostfs/#list7" cmimpressionsent="1"><font color=#996699>清单 7</font></a> 提供了重命名文件的小示例，在 <code>from</code> 路径中的文件不存在时引发异常。 </p>
<br><a name=list7><font color=#1d58d1><strong>清单 7. Boost 中的错误处理</strong> </font></a><br>
<table cellSpacing=0 cellPadding=0 width="65%" border=0>
    <tbody>
        <tr>
            <td class=code-outline>
            <pre class=displaycode>
            #include &lt;iostream&gt;
            #include &#8220;boost/filesystem.hpp&#8221;
            int main()
            {
            try {
            boost::filesystem::path path("C:\\src\\hdbase\\j1");
            boost::filesystem::path path2("C:\\src\\hdbase\\j2");
            boost::filesystem::rename(path, path2);
            }
            catch(boost::filesystem::filesystem_error e) {
            // do the needful
            }
            return 0;
            }
            </pre>
            </td>
        </tr>
    </tbody>
</table>
<br><br>
<table cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr>
            <td><img height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"> <br><img height=6 alt="" src="http://www.ibm.com/i/c.gif" width=8 border=0> </td>
        </tr>
    </tbody>
</table>
<table class=no-print cellSpacing=0 cellPadding=0 align=right>
    <tbody>
        <tr align=right>
            <td><img height=4 alt="" src="http://www.ibm.com/i/c.gif" width="100%"> <br>
            <table cellSpacing=0 cellPadding=0 border=0>
                <tbody>
                    <tr>
                        <td vAlign=center><img height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0> <br></td>
                        <td vAlign=top align=right><a class=fbox href="http://www.ibm.com/developerworks/cn/aix/library/au-boostfs/#main" cmimpressionsent="1"><strong><font color=#996699>回页首</font><font color=#1d58d1> </font></strong></a></td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
    </tbody>
</table>
<br><br>
<p><a name=N1020D><font color=#1d58d1><span class=atitle>Boost Filesystem Library 中的函数类别</span> </font></a></p>
<p><code>boost::filesystem</code> 提供了不同类别的函数：有些函数（如 <code>is_directory</code>）用于查询文件系统，而其他函数（如 <code>create_directory</code>）则主动对文件系统进行修改。根据各自功能的不同，这些函数可以大略归入以下类别： </p>
<ul>
    <li><strong>属性函数：</strong>提供杂项信息，如文件大小、磁盘使用量等。
    <li><strong>文件系统操作函数：</strong>用于创建常规文件、目录和符号链接；复制和重命名文件；提供删除功能。
    <li><strong>实用工具：</strong>测试文件的扩展名等。
    <li><strong>杂项常规函数：</strong>以编程方式更改文件扩展名等。 </li>
</ul>
<p><a name=N1023C><span class=smalltitle><strong><font color=#1d58d1><font face=Arial>属性函数</font> </font></strong></span></a></p>
<p>Boost Filesystem Library 包括以下属性函数：</p>
<ul>
    <li><strong><code>uintmax_t file_size(const path&amp;)</code>：</strong>返回常规文件的大小（以字节为单位）
    <li><strong><code>boost::filesystem::space_info space(const path&amp;)</code>：</strong>接受路径作为输入，并返回定义如下的 <code>space_info</code> 结构：
    <table cellSpacing=0 cellPadding=0 width="50%" border=0>
        <tbody>
            <tr>
                <td class=code-outline>
                <pre class=displaycode>
                struct space_info {
                uintmax_t capacity;
                uintmax_t free;
                uintmax_t available;
                };
                </pre>
                </td>
            </tr>
        </tbody>
    </table>
    <br>
    <p>根据文件系统所属的磁盘分区，此流程将对该分区的所有目录返回相同的磁盘使用量统计数据（以字节为单位）。例如，对于 C:\src\dir1 和 C:\src\dir2，都会返回相同的磁盘使用数据。 </p>
    <li><strong><code>std::time_t last_write_time(const path&amp;)</code>：</strong>返回文件的最后修改时间。
    <li><strong><code>void last_write_time(const path&amp;, std::time_t new_time)</code>：</strong>修改文件的最后修改时间。
    <li><strong><code>const path&amp; current_path( )</code>：</strong>返回程序的当前工作目录的完整路径（注意，此路径与最初运行程序的路径<em>可能不同</em>，因为可能采用编程方式更改目录）。 </li>
</ul>
<p><a name=N10288><span class=smalltitle><strong><font color=#1d58d1><font face=Arial>文件系统操作函数</font> </font></strong></span></a></p>
<p>这组函数负责进行新文件和目录创建、文件删除等操作： </p>
<ul>
    <li><strong><code>bool create_directory(const path&amp;)</code>：</strong>此函数使用给定的路径名称创建目录。（请注意，如果 PATHNAME 本身包含无效字符，则结果经常是由平台定义的。例如，在 UNIX 和 Windows 系统中，星号 (<code>*</code>)、问号 (<code>?</code>) 及其他此类字符视为无效，不能出现在目录名称中。）
    <li><strong><code>bool create_directories(const path&amp;)</code>：</strong>与创建单个目录相对，您可以使用此 API 创建目录树。例如，以目录树 /a/b/c 为例，必须在 /tmp 文件夹内创建此目录树。可调用此 API 完成任务，但使用相同的参数调用 <code>create_directory</code> 时将引发异常。
    <li><strong><code>bool create_hard_link (const path&amp; frompath, const path&amp; topath)</code>：</strong>此函数在 <code>frompath</code> 和 <code>topath</code> 间创建硬链接。
    <li><strong><code>bool create_symlink(const path&amp; frompath, const path&amp; topath)</code>：</strong>此函数在 <code>frompath</code> 和 <code>topath</code> 间创建符号（软）链接。
    <li><strong><code>void copy_file(const path&amp; frompath, const path&amp; topath)</code>：</strong>将 <code>frompath</code> 引用的文件的内容和属性复制到 <code>topath</code> 引用的文件中。例程<strong>expects a destination file to be absent</strong>；如果存在目标文件，则会引发异常。因此，此函数与 UNIX 中系统指定的 <code>cp</code> 命令并不等效。另外，此函数还预期 <code>frompath</code> 变量将引用正确的常规文件。请看以下示例：<code>frompath</code> 引用符号链接 /tmp/file1，而后者反过来引用文件 /tmp/file2；而 <code>topath</code> 可以为 /tmp/file3。在这种情况下，<code>copy_file</code> 将失败。这是此 API 与 <code>cp</code> 命令相比的另一个差别。
    <li><strong><code>void rename(const path&amp; frompath, const path&amp; topath)</code>：</strong>此函数是用于重命名文件的 API。可以通过在 <code>topath</code> 参数中指定完整路径名来同时重命名和更改文件的位置，如<a href="http://www.ibm.com/developerworks/cn/aix/library/au-boostfs/#list8" cmimpressionsent="1"><font color=#996699>清单 8</font></a> 中所示。 <br><br><a name=list8><strong><font color=#1d58d1>清单 8. Boost 中的重命名功能</font></strong></a><br>
    <table cellSpacing=0 cellPadding=0 width="50%" border=0>
        <tbody>
            <tr>
                <td class=code-outline>
                <pre class=displaycode>
                #include &lt;stdio.h&gt;
                #include &#8220;boost/filesystem.hpp&#8221;
                int main()
                {
                boost::filesystem::path path("/home/user1/abc");
                boost::filesystem::rename(path, "/tmp/def");
                return 0;
                }
                // abc is renamed def and moved to /tmp folder
                </pre>
                </td>
            </tr>
        </tbody>
    </table>
    <br>
    <li><strong><code>bool remove(const path&amp; p)</code>：</strong>此例程将尝试删除路径 <em>p</em> 所引用的文件或目录。对于目录的情况，如果目录的内容不为空，则此例程将引发异常。警告：此例程<em>并不考虑所删除的内容</em>，即使其他程序在访问同一文件也如此！
    <li><strong><code>unsigned long remove_all(const path&amp; p)</code>：</strong>此 API 尝试删除路径 <em>p</em> 所引用的文件或目录。与 <code>remove</code> 不同，此函数并不会特殊考虑不为空的目录。此函数是 UNIX <code>rm &#8211;rf</code> 命令的 Boost 对等项。 </li>
</ul>
<p><a name=N10345><span class=smalltitle><strong><font color=#1d58d1><font face=Arial>实用工具</font> </font></strong></span></a></p>
<p>Boost Filesystem Library 包含以下实用工具：</p>
<ul>
    <li><strong><code>bool exists(const path&amp;)</code>：</strong>此函数检查文件的扩展名。文件可以为任何类型：常规文件、目录、符号链接等等。
    <li><strong><code>bool is_directory(const path&amp;)</code>：</strong>此函数检查路径是否与目录对应。
    <li><strong><code>bool is_regular(const path&amp;)</code>：</strong>此函数检查普通文件（即此文件不是目录、符号链接、套接字或设备文件）。
    <li><strong><code>bool is_other(const path&amp;)</code>：</strong>通常，此函数检查设备文件（如 /dev/tty0）或套接字文件。
    <li><strong><code>bool is_empty(const path&amp;)</code>：</strong>如果路径与文件夹对应，此函数将检查文件夹是否为空，并据此返回&#8220;True&#8221;或&#8220;False&#8221;。如果路径与文件对应，此函数将检查文件的大小是否等于 0。对于文件的硬链接或符号链接的情况，此 API 将检查原始文件是否为空。
    <li><strong><code>bool equivalent(const path1&amp; p1, const path2&amp; p2)</code>：</strong>此 API 非常实用，可用于比较相对路径和绝对路径名。请看<a href="http://www.ibm.com/developerworks/cn/aix/library/au-boostfs/#list9" cmimpressionsent="1"><font color=#996699>清单 9</font></a>： <br><br><a name=list9><strong><font color=#1d58d1>清单 9. 测试两个路径是否等效</font></strong></a><br>
    <table cellSpacing=0 cellPadding=0 width="50%" border=0>
        <tbody>
            <tr>
                <td class=code-outline>
                <pre class=displaycode>
                #include &lt;stdio.h&gt;
                #include &#8220;boost/filesystem.hpp&#8221;
                int main()
                {
                boost::filesystem::path path1("/usr/local/include"); // random pathname
                boost::filesystem::path path2("/tmp/../usr/local/include");
                bool result = boost::filesystem::is_equivalent(path1, path2);
                printf(&#8220;Paths are equivalent : %d\n&#8221;, result);
                return 0;
                }
                // result: 1
                </pre>
                </td>
            </tr>
        </tbody>
    </table>
    <br>
    <li><strong><code>path system_complete(const path&amp;)</code>：</strong>此函数是与 <code>bool equivalent(const path1&amp; p1, const path2&amp; p2)</code> 同一系列的另一个 API。在给定当前工作目录中任意文件路径的情况下，此 API 将返回该文件的绝对路径。例如，如果用户位于目录 /home/user1 并查询文件 ../user2/file2，此函数将返回 <code>/home/user2/file2</code>，即文件 file2 的完整路径名。 </li>
</ul>
<p><a name=N103AC><span class=smalltitle><strong><font color=#1d58d1><font face=Arial>杂项函数</font> </font></strong></span></a></p>
<p>Boost Filesystem Library 包括以下杂项函数：</p>
<ul>
    <li><strong><code>std::string extension(const path&amp;)</code>：</strong>此函数以前面带句点 (<code>.</code>) 的形式返回给定文件名的扩展名。例如，对于文件名为 <em>test.cpp</em> 的文件，<code>extension</code> 将返回 <code>.cpp</code>。对于文件没有扩展名的情况，此函数将返回空字符串。对于隐藏文件（即 UNIX 系统中文件名以 <code>.</code> 开始的文件），此函数将相应地计算扩展名类型或返回空字符串（因此，对于 <em>.test.profile</em>，此例程将返回 <code>.profile</code>）。
    <li><strong><code>std::string basename(const path&amp;)</code>：</strong>这是与 <code>extension</code> 互补的例程。它将返回文件名中 <code>.</code> 之前的字符串。请注意，即使提供了绝对文件名，此 API 仍然仅会返回属于文件名的直接部分，如<a href="http://www.ibm.com/developerworks/cn/aix/library/au-boostfs/#list10" cmimpressionsent="1"><font color=#996699>清单 10</font></a> 中所示。 <br><br><a name=list10><strong><font color=#1d58d1>清单 10. 使用 boost::basename</font></strong></a><br>
    <table cellSpacing=0 cellPadding=0 width="50%" border=0>
        <tbody>
            <tr>
                <td class=code-outline>
                <pre class=displaycode>
                #include &lt;stdio.h&gt;
                #include &lt;cstring&gt;
                #include &#8220;boost/filesystem.hpp&#8221;
                use namespace std;
                int main()
                {
                boost::filesystem::path path1("/tmp/dir1/test1.c ");
                boost::filesystem::path path2("/tmp/dir1/.test1.profile");
                string result1 = boost::filesystem::basename (path1);
                string result2 = boost::filesystem::basename (path2);
                printf(&#8220;Basename 1: %s  Basename2 : %s\n&#8221;, result1.c_str(), result2.c_str());
                return 0;
                }
                // result: Basename1: test1 Basename2: .test1
                </pre>
                </td>
            </tr>
        </tbody>
    </table>
    <br>
    <li><strong><code>std::string change_extension(const path&amp; oldpath, const std::string new_extension)</code>：</strong>此 API 将返回反映更改后的名称的新字符串。请注意，与 <code>oldpath</code> 对应的文件保持<strong>不变</strong>。这只是一个常规函数。另请注意，您必须显式地在扩展名中指定<em>点</em>。例如，<code>change_extension("test.c", "so")</code> 会得到 <code>testso</code>，而不是 <code>test.so</code>。 </li>
</ul>
<br>
<table cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tbody>
        <tr>
            <td><img height=1 alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%"> <br><img height=6 alt="" src="http://www.ibm.com/i/c.gif" width=8 border=0> </td>
        </tr>
    </tbody>
</table>
<table class=no-print cellSpacing=0 cellPadding=0 align=right>
    <tbody>
        <tr align=right>
            <td><img height=4 alt="" src="http://www.ibm.com/i/c.gif" width="100%"> <br>
            <table cellSpacing=0 cellPadding=0 border=0>
                <tbody>
                    <tr>
                        <td vAlign=center><img height=16 alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width=16 border=0> <br></td>
                        <td vAlign=top align=right><a class=fbox href="http://www.ibm.com/developerworks/cn/aix/library/au-boostfs/#main" cmimpressionsent="1"><strong><font color=#996699>回页首</font><font color=#1d58d1> </font></strong></a></td>
                    </tr>
                </tbody>
            </table>
            </td>
        </tr>
    </tbody>
</table>
<br><br>
<p><a name=N1041B><font color=#1d58d1><span class=atitle>结束语</span> </font></a></p>
<p>本文提供了 Boost Filesystem Library 的简单概述。不应将本文视为 Boost 中的整个文件系统接口的综合文档。并未讨论此 API 集的内部情况，也没有讨论这些 API 在非 UNIX 或 Windows 平台（如 VMS）中的细节。有关文件系统的更多信息，请参见<a href="http://www.ibm.com/developerworks/cn/aix/library/au-boostfs/#resources" cmimpressionsent="1"><font color=#996699>参考资料</font></a>。 </p>
<br><br>
<p><a name=resources><span class=atitle><font color=#1d58d1>参考资料 </font></span></a></p>
<strong>学习</strong> <br>
<ul>
    <li>您可以参阅本文在 developerWorks 全球站点上的 <a href="http://www.ibm.com/developerworks/aix/library/au-boostfs/?S_CMP=cn-a-aix&amp;S_TACT=105AGX52" target=_blank cmimpressionsent="1"><font color=#5c81a7>英文原文</font></a> 。<br><br>
    <li><a href="http://www.boost.org/libs/filesystem/doc/index.htm" cmimpressionsent="1"><font color=#5c81a7>Boost Filesystem Library 文档</font><font color=#1d58d1> </font></a>：阅读 Boost Filesystem Library 文档（相对而言，更加适合开发人员，而不是普通用户）。<br><br>
    <li><a href="http://beans.seartipy.com/2006/05/10/boost-filesystem-library-writing-portable-c-programs-to-acess-the-filesystem/" cmimpressionsent="1"><font color=#5c81a7>Every Flavour Beans</font><font color=#1d58d1> </font></a>：这篇文章对 Boost Filesystem Library 非常独到的介绍，提供了安装 Boost 库的详细信息。<br><br>
    <li><a href="http://solarix.ru/for_developers/cpp/boost/filesystem/en/index.shtml" cmimpressionsent="1"><font color=#5c81a7>Detailed Filesystem Library 文档</font><font color=#1d58d1> </font></a>：查找关于 PATHNAME 检查函数的信息。<br><br>
    <li><a href="http://www.ibm.com/developerworks/cn/aix" cmimpressionsent="1"><font color=#5c81a7>AIX and UNIX 专区</font><font color=#1d58d1> </font></a>：developerWorks 的&#8220;AIX and UNIX 专区&#8221;提供了大量与 AIX 系统管理的所有方面相关的信息，您可以利用它们来扩展自己的 UNIX 技能。<br><br>
    <li><a href="http://www.ibm.com/developerworks/cn/aix/newto" cmimpressionsent="1"><font color=#5c81a7>AIX and UNIX 新手入门</font><font color=#1d58d1> </font></a>：访问&#8220;AIX and UNIX 新手入门&#8221;页面可了解更多关于 AIX 和 UNIX 的内容。<br><br>
    <li><a href="http://www.ibm.com/developerworks/cn/aix/lpsummary.html" cmimpressionsent="1"><font color=#5c81a7>AIX and UNIX 专题汇总</font><font color=#1d58d1> </font></a>：AIX and UNIX 专区已经为您推出了很多的技术专题，为您总结了很多热门的知识点。我们在后面还会继续推出很多相关的热门专题给您，为了方便您的访问，我们在这里为你把本专区的所有专题进行汇总，让您更方便的找到你需要的内容。<br><br>
    <li><a href="http://www.ibm.com/developerworks/cn/offers/techbriefings" cmimpressionsent="1"><font color=#5c81a7>developerWorks 技术事件和网络广播</font><font color=#1d58d1> </font></a>：了解最新的 developerWorks 技术事件和网络广播。<br><br>
    <li><a href="http://www-941.ibm.com/collaboration/wiki/display/WikiPtype/Home" cmimpressionsent="1"><font color=#5c81a7>AIX Wiki</font><font color=#1d58d1> </font></a>：访问这个与 AIX 相关的技术信息协作环境。<br><br>
    <li><a href="http://www-128.ibm.com/developerworks/podcast/" cmimpressionsent="1"><font color=#5c81a7>Podcast</font><font color=#1d58d1> </font></a>：收听 Podcast 并与 IBM 技术专家保持同步。<br></li>
</ul>
<img src ="http://www.cppblog.com/deadpunk/aggbug/67670.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/deadpunk/" target="_blank">活着就是折腾，所以当然要骠悍的折腾</a> 2008-11-23 16:31 <a href="http://www.cppblog.com/deadpunk/archive/2008/11/23/67670.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>VS2003安装Boost1.33.1 </title><link>http://www.cppblog.com/deadpunk/archive/2007/03/08/19414.html</link><dc:creator>活着就是折腾，所以当然要骠悍的折腾</dc:creator><author>活着就是折腾，所以当然要骠悍的折腾</author><pubDate>Thu, 08 Mar 2007 03:38:00 GMT</pubDate><guid>http://www.cppblog.com/deadpunk/archive/2007/03/08/19414.html</guid><wfw:comment>http://www.cppblog.com/deadpunk/comments/19414.html</wfw:comment><comments>http://www.cppblog.com/deadpunk/archive/2007/03/08/19414.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/deadpunk/comments/commentRss/19414.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/deadpunk/services/trackbacks/19414.html</trackback:ping><description><![CDATA[学习Boost库是学习C++领域里是不可缺少的步骤，至少我是这么认为的。所以我就Download了其1.33.1版本，发现安装有各种方法，但是对与VS2003，用下面的方法是比较便捷的，特别是对于要设置环境变量的那些方法。<br />下面是我安装Boost1.33.1库的过程：<br />首先安装VS2003和下载Boost1.33.1库我在这里就不多说了。那就开始按下面步骤执行：<br />1、打开控制台窗口，请使用vs2003在开始菜单中的“Visual Studio.net工具-&gt;Visual Studio.net  2003 命令提示”打开控制台，这样VC.net的的编译环境就设置好了。<br /><br />2、假设boost安装包的解压的目录为{BOOSTDIR}中。<br />我的路径{BOOSTDIR}为C:\C++\Library\Boost_1_33_1<br />先编译出bjam.exe，它被用于安装boost库<br />cd {BOOSTDIR}\tools\build\jam_src<br />即：cd  C:\C++\Library\Boost_1_33_1\tools\build\jam_src<br />输入build.bat<br /><br />3、利用编译出的bjam.exe程序编译并安装boost库<br />cd {BOOSTDIR}(<font style="BACKGROUND-COLOR: #ff0000">注意先要进到boost根目录才能执行下面的bjam命令,否则找不到JamFile</font>)<br />即：cd  C:\C++\Library\Boost_1_33_1<br />用FileExplorer浏览到{BOOST_SRC}\tools\build\jam_src\bin.ntx86\bjam.exe（即：C:\......\bjam.exe)<br />用Ctrl+C 把路径Copy下来，也就是把C:\C++\Library\Boost_1_33_1\tools\build\jam_src\bin.ntx86\bjam.exe<br /><br />4、输入C:\C++\Library\Boost_1_33_1\tools\build\jam_src\bin.ntx86\bjam "-sVC71_ROOT=D:\Program Files\Microsoft Visual Studio .NET 2003\Vc7" "-sTOOLS=vc-7_1"  "-sPYTHON_ROOT=C:\Python24" "--prefix=C:\C++\librarys\boost" install<br /><br />下面的命令的各选项的说明：<br />prefix    将boost安装到的路径（生成的头文件和库文件都会放到该路径中）。<br />重定义以下变量（利用-s设置，即Set）：<br />VC71_ROOT　　vc2003的安装路径，如果未将vc2003安装到默认位置，你必须指定该项。<br />TOOLS         使用的编译工具，vc2003对应的是vc-7_1（可以查看{BOOSTDIR}tools\build\v1下看是否有其对应文件，也就是在vc-7_1后多一个-tools.jam的文件（例如vc-7_1-tools.jam)。<br />PYTHON_ROOT        python的安装目录，如果未将BOOST安装到默认位置，你必须指定该项。<br />BUILD         编译结果选项，默认会生成尽可能多的版本，如调试版／发行版，静态库／动态库，单线程／多线程。<br /><img src ="http://www.cppblog.com/deadpunk/aggbug/19414.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/deadpunk/" target="_blank">活着就是折腾，所以当然要骠悍的折腾</a> 2007-03-08 11:38 <a href="http://www.cppblog.com/deadpunk/archive/2007/03/08/19414.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>