views:

186

answers:

3

I was wondering if there was any kind of portable (Mac&Windows) method of reading and writing to the hard drive which goes beyond iostream.h, in particular features like getting a list of all the files in a folder, moving files around, ect.

I was hoping that there would be something like SDL around, but so far I havn't been able to find much.

Any ideas??

+11  A: 

Could Boost Filesystem possibly be what you're after?

Smashery
link to more recent version (1.42) http://www.boost.org/doc/libs/1_42_0/libs/filesystem/doc/index.htm
Alexander Malakhov
Thanks - I've updated my answer to link to that.
Smashery
+4  A: 

I am a fan of boost::filesystem also. It takes minimal effort to write what you want. The following example(just to give you a feel of how it looks like), asks the user to enter a path and a file name, and it will get paths of all the files with that name no matter whether they are in the root dir, or in any sub dir of that root dir:

#include <iostream>
#include <string>
#include <vector>
#include <boost/filesystem.hpp>
using namespace std;
using namespace boost::filesystem;

void find_file(const path& root,
    const string& file_name,
    vector<path>& found_files)
{
    directory_iterator current_file(root), end_file;
    bool found_file_in_dir = false;
    for( ; current_file != end_file; ++current_file)
    {
        if( is_directory(current_file->status()) )
                find_file(*current_file, file_name, found_files);
        if( !found_file_in_dir && current_file->leaf() == file_name )
        {
                // Now we have found a file with the specified name,
                // which means that there are no more files with the same
                // name in the __same__ directory. What we have to do next,
                // is to look for sub directories only, without checking other files.
                found_files.push_back(*current_file);
                found_file_in_dir = true;
        }
    }
}

int main()
{
    string file_name;
    string root_path;
    vector<path> found_files;

    std::cout << root_path;
    cout << "Please enter the name of the file to be found(with extension): ";
    cin >> file_name;
    cout << "Please enter the starting path of the search: ";
    cin >> root_path;
    cout << endl;

    find_file(root_path, file_name, found_files);
    for( std::size_t i = 0; i < found_files.size(); ++i)
            cout << found_files[i] << endl;
}
AraK
+2  A: 

There is no native C++ way to traverse a directory structure or list files in a directory in a cross-platform manner. It's just not built into the language. (For good reason!)

Your best bet is to go with a code framework, and there are a plethora of good options out there.

Boost Filesystem

Apache Portable Runtime

Aaaand my personal favorite - Qt

Although, if you use this it's difficult to just use the file-system portion of it. You pretty much have to port your entire application over to Qt specific classes.

kurige
Bonus points for 3 possible solutions!
Tomas Cokis