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:
-
#include<boost/filesystem/operations.hpp>
-
#include<iostream>
-
#include<string>
-
namespace bfs=boost::filesystem;
1. Simple program to demonstrate the use of file creation and removal operations:
-
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."<<std::endl;
-
}
Pay attention to the following four Boost Filesystem functions used:
-
bfs::create_directory(bfs::path(name));
-
bfs::rename(name, new_name);
-
bfs::remove(bfs::path(name));
-
bfs::copy_file(name, new_name);
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:
-
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."<<std::endl;
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:
-
bfs::path p("folder");
-
bfs::directory_iterator dir_iter(p), dir_end;
-
for(;dir_iter != dir_end; ++dir_iter)
-
{
-
std::cout<<(*dir_iter).leaf();
-
}
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:
-
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."<<std::endl;
-
exit(-1);
-
}
-
bfs::directory_iterator dir_iter(p), dir_end;
-
for(;dir_iter != dir_end; ++dir_iter)
-
{
-
std::cout<<"Removing file: "<<dir_iter->leaf();
-
bfs::remove(*dir_iter);
-
}
-
std::cout<<"Operation Completed."<<std::endl;
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.
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?
QuoteComment by TaSharon — November 2, 2006 @ 10:10 pm
There is some mistakes in your code: the order of choice 2 and choice 3.
2. Remove File\n3. Rename File
case '2':
Quotestd::cout>name;
std::cout>new_name;
bfs::rename(name, new_name);
break;
case '3':
std::cout>name;
bfs::remove(bfs::path(name));
break;
Comment by Yanling Zhi — May 12, 2007 @ 10:22 am
Thanks for the correction, I have updated the post.
QuoteComment by tabrez — May 12, 2007 @ 5:22 pm
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()".
QuoteComment by brad — July 3, 2007 @ 12:50 pm
I got it to work if you use remove_all('path_to_dir'), is that the only/best way to go about it?
QuoteComment by brad — July 4, 2007 @ 2:42 am
hello every one ,
Quotecan any one please help me where to get tutorials of boost filesystem ???
i wan't able to get it naywhere
mohan gupta
Comment by mohan gupta — April 23, 2008 @ 4:52 pm
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:
QuoteBeyond the C++ Standard Library: An Introduction to Boost
Comment by tabrez — April 25, 2008 @ 1:26 pm
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:
Quote/vobs/src9005/SS_TestSNMP/src/SNMP_Delivery/myExample/dir/dir.cpp:16: undefined reference to `boost::filesystem::path::path(char const*)'
dir.o(.text+0x173):/vobs/src9005/SS_TestSNMP/src/SNMP_Delivery/myExample/dir/dir.cpp:17: undefined reference to `boost::filesystem::create_directory(boost::filesystem::path const&)'
Comment by Anjali — May 9, 2008 @ 5:14 pm
#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
QuoteComment by Anjali — May 9, 2008 @ 5:16 pm
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/
QuoteComment by tabrez — May 11, 2008 @ 2:04 pm