{"id":387,"date":"2007-07-12T10:28:11","date_gmt":"2007-07-12T16:28:11","guid":{"rendered":"http:\/\/blog.codedread.com\/archives\/2007\/07\/12\/getting-to-know-c-next-filesystem\/"},"modified":"2007-07-12T10:28:11","modified_gmt":"2007-07-12T16:28:11","slug":"getting-to-know-c-next-filesystem","status":"publish","type":"post","link":"https:\/\/www.codedread.com\/blog\/archives\/2007\/07\/12\/getting-to-know-c-next-filesystem\/","title":{"rendered":"Getting To Know C++ Next: Filesystem"},"content":{"rendered":"<p>The <a href=\"http:\/\/en.wikipedia.org\/wiki\/C%2B%2B0x\">next version of C++<\/a> is going to include some nice, shiny new libraries in its standard namespace.  These libraries will really beef up the things that C++ developers want to be able to do in a cross-platform fashion.  It's not clear to me what will happen to the libraries in <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2005\/n1810.html\" title=\"Technical Report 2\">TR2<\/a>. I hope things like accessing the bloody file system will make its way into the next version of C++.  Here's a quicky look at how one might do that using Boost.Filesystem.  <!--more--><\/p>\n<p>I've really only started using <a href=\"http:\/\/www.boost.org\/libs\/filesystem\/doc\/index.htm\">Boost.Filesystem<\/a> very recently to replace some platform-specific code that accessed the file system so that I could port my code over to <a href=\"http:\/\/www.opensuse.org\/\">OpenSUSE<\/a> (Linux).  That change was pretty much the only thing I had to do because the rest of the program relies on <a href=\"http:\/\/www.libsdl.org\/\" title=\"Simple DirectMedia Library\">SDL<\/a> which is already cross-platform.<\/p>\n<p>The code I was using was originally ported from DOS to Windows and used the functions <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/zyzxfzac(VS.71).aspx\">_findfirst()<\/a>, _findnext() to access a directory contents, iterate through each file and find all files ending in a specific extension.  A pretty common operation really, so I thought I'd post this as an example to get people looking at Boost.Filesystem:<\/p>\n<div class=\"code\">\n<pre>\n#include &#60;string>\n#include &#60;vector>\n#include &#60;boost\/filesystem.hpp>\nusing namespace boost::filesystem;\nusing std::string;\nusing std::vector;\n\n  <span style=\"color:green\">\/\/ get all files ending in .tsg<\/span>\n  vector&#60; string > filenames;\n  path dir_path(\"data\/savegame\/\");\n  directory_iterator end_it; <span style=\"color:green\">\/\/ [1]<\/span>\n  <span style=\"color:green\">\/\/ loop through each file in the directory<\/span>\n  for(directory_iterator it(dir_path); it != end_it; ++it) {\n    <span style=\"color:green\">\/\/ if it's not a directory and its extension is .tsg [2]<\/span>\n    if( !is_directory(it->status()) && extension(it->path()) == \".tsg\" ) {\n      <span style=\"color:green\">\/\/ store filename for later use<\/span>\n      filenames.push_back( it->path().string() );\n    }\n  }\n<\/pre>\n<\/div>\n<p>Now your filenames vector contains all the filenames in the directory \"data\/savegame\/\" that had an extension \".tsg\".  Some notes on this example:<\/p>\n<ol>\n<li>By default, a directory_iterator usefully points to \"past the end\"<\/li>\n<li>Extensions are case-sensitive on Linux<\/li>\n<\/ol>\n<p>So basically, a <i>path<\/i> is pretty much what you'd expect:  a location in the filesystem which can either be a directory or a file.  You can get the full string representation by calling the member method string().  A <i>directory_iterator<\/i> allows you to iterate through all items in the path.  There are some convenience functions like <i>extension()<\/i> and <i>basename()<\/i>.  The <i>leaf()<\/i> member function of a <i>path<\/i> returns a string holding the last file or directory name in the path object if you ever need that.<\/p>\n<div class=\"ads\"><object type=\"text\/html\" width=\"475\" height=\"70\" data=\"http:\/\/www.codedread.com\/gads.php\"><\/object><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The next version of C++ is going to include some nice, shiny new libraries in its standard namespace. These libraries will really beef up the things that C++ developers want to be able to do in a cross-platform fashion. It&#8217;s not clear to me what will happen to the libraries in TR2. I hope things [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34,25,11],"tags":[],"class_list":["post-387","post","type-post","status-publish","format-standard","hentry","category-c","category-software","category-technology"],"_links":{"self":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/posts\/387","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/comments?post=387"}],"version-history":[{"count":0,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/posts\/387\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/media?parent=387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/categories?post=387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/tags?post=387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}