“The time has come…to talk of many [technologies].” –Lewis Carroll(‘The Walrus and the Carpenter’)
C++ Boost Filesystem Library(Part II): Example Programs
Continuing from where I had left in my earlier post containing the basics of the C++ Boost Filesystem Library, below are some example programs that make use of some common facilities available in the Boost Filesystem Library. I assume that all the code snippets shown in this post are properly nested within the main() function apart from including the following things:
[cpp]
#include
#include
#include
namespace bfs=boost::filesystem;
[/cpp]
1. Simple program to demonstrate the use of file creation and removal operations:
[cpp]
std::cout<<"Enter your choice:\n";
std::cout<<"1. Create folder\n2. Rename File\n3. Remove File\n4. Copy File\n";
char ch;
std::cin>>ch;
std::string name, new_name;
switch(ch)
{
case ’1′:
std::cout<<"Enter folder name:";
std::cin>>name;
bfs::create_directory(bfs::path(name));
break;
case ’2′:
std::cout<<"Enter file name:";
std::cin>>name;
std::cout<<"Enter new name:";
std::cin>>new_name;
bfs::rename(name, new_name);
break;
case ’3′:
std::cout<<"Enter file name:";
std::cin>>name;
bfs::remove(bfs::path(name));
break;
case ’4′:
std::cout<<"Enter file name:";
std::cin>>name;
std::cout<<"Enter new name:";
std::cin>>new_name;
bfs::copy_file(name, new_name);
break;
}
std::cout<<"Operation finished."<
[/cpp]
Pay attention to the following four Boost Filesystem functions used:
[cpp]
bfs::create_directory(bfs::path(name));
bfs::rename(name, new_name);
bfs::remove(bfs::path(name));
bfs::copy_file(name, new_name);
[/cpp]
They all do what their function names suggest. Like its always the case with the simple example programs, no error checking is done here ;)
2. Removing all the files from a directory:
[cpp]
std::cout<<"Enter the name of the folder to empty:";
std::string name;
std::cin>>name;
bfs::remove_all(bfs::path(name));
std::cout<<"Operation completed."<
The other examples needs an introduction to the directory_iterator class which is used to iterate over the contents of a directory. A simple usage looks like this:
[cpp]
bfs::path p(“folder”);
bfs::directory_iterator dir_iter(p), dir_end;
for(;dir_iter != dir_end; ++dir_iter)
{
std::cout<<(*dir_iter).leaf();
}
[/cpp]
A directory_iterator object can be created by passing it a name of a directory. Applying the prefix increment operator(++) to it makes it point to the next file in the directory. Applying the indirection operator(*) returns the file currently being pointed to, as a ‘path’ object. We call the leaf() method on the returned ‘path’ object to print the file name. Using ‘dir_iter->leaf()’ instead of ‘(*dir_iter).leaf()’ has the same effect.
Using a ‘directory_iter’ object, we can create a function similar to the ‘remove_all()’ function that we used in Example 2.
3. Removing all the files from a directory by iteration:
[cpp]
std::cout<<"Enter the name of the folder to empty:";
std::string name;
std::cin>>name;
bfs::path p(name);
if(!bfs::exists(p) || !bfs::is_directory(p))
{
std::cout<<"Invalid input."<
}
bfs::directory_iterator dir_iter(p), dir_end;
for(;dir_iter != dir_end; ++dir_iter)
{
std::cout<<"Removing file: "<
bfs::remove(*dir_iter);
}
std::cout<<"Operation Completed."<
Simple error checking is done on the input before calling the remove()function on all the files present in the specified folder. Will follow up with more examples using the Boost Filesystem Library.
| Print article | This entry was posted by tabrez on May 28, 2006 at 7:56 pm, and is filed under C++ Boost. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 3 years ago
I need to know how to get started on writing a program that uses enumeration with three paramters (Iosceles, noTriangle, something else I forgot) but when do I use int main () and how to do a loop to get the output of the triangle to print out?
about 3 years ago
There is some mistakes in your code: the order of choice 2 and choice 3.
2. Remove File\n3. Rename File
case ’2′:
std::cout>name;
std::cout>new_name;
bfs::rename(name, new_name);
break;
case ’3′:
std::cout>name;
bfs::remove(bfs::path(name));
break;
about 3 years ago
Thanks for the correction, I have updated the post.
about 3 years ago
How can you delete a directory, I’m getting errors when i try to use “remove(path_to_dir)”, I’ll also tried “path_to_dir.remove_leaf()”.
about 3 years ago
I got it to work if you use remove_all(‘path_to_dir’), is that the only/best way to go about it?
about 2 years ago
hello every one ,
can any one please help me where to get tutorials of boost filesystem ???
i wan’t able to get it naywhere
mohan gupta
about 2 years ago
Mohan Gupta,
1. I have made three blog posts about Boost Filesystem library:
Writing Portable C++ Programs to Acess The Filesystem
C++ Boost Filesystem Library(Part II): Example Programs
C++ Boost Filesystem Library(Part III): Example Programs
2. Boost Filesystem official documentation is informative and easy to follow:
Boost Filesystem Library
3. The following book covers many useful Boost libraries, including Boost Filesystem library:
Beyond the C++ Standard Library: An Introduction to Boost
about 2 years ago
Hi,
i am new to the Boost library. I was trying to check if a directory exists in a path and if it doesnt exits, the directory needs to be created. Tried the program as given above, but I am encountering compilation problems. I need help
My code:
#include<iostream>
#include<boost/filesystem>
using namespace std;
namespace bfs=boost::filesystem;
int main()
{
bfs::path p(“/home/mnadig/ANj”);
bfs::create_directory(p);
std::cout<<”Operation finished.”<<std::endl;
return 0;
}
Error:
/vobs/src9005/SS_TestSNMP/src/SNMP_Delivery/myExample/dir/dir.cpp:16: undefined reference to `boost::filesystem::path::path(char const*)’
dir.o(.text+0×173):/vobs/src9005/SS_TestSNMP/src/SNMP_Delivery/myExample/dir/dir.cpp:17: undefined reference to `boost::filesystem::create_directory(boost::filesystem::path const&)’
about 2 years ago
#include<iostream>
#include<boost/filesystem>
using namespace std;
namespace bfs=boost::filesystem;
int main()
{
bfs::path p(“/home/mnadig/ANj”);
bfs::create_directory(p);
std::cout<<”Operation finished.”<<std::endl;
return 0;
}
Sorry the header files were not displayed when i pasted it. Here it is again
about 2 years ago
You need to install the Boost library properly; in this case, you need to build the Boost Filesystem library(or install the lib package for your OS/distribution). You then need to link to that library when building your application. See the Boost installation posts on the following page for more help:
http://beans.seartipy.com/category/c_boost/
about 1 year ago
Hey, I was wondering.
What would happen if a program tried to acess a folder from an administrator account while being in a non-administrator account?
Would filesystem simply think the dir doent exist? Or would it return a more specific error? Or would it detect the folder as empty?