4

iostream.h を超えてハード ドライブに読み書きするポータブル (Mac&Windows) 方法、特にフォルダー内のすべてのファイルのリストの取得、ファイルの移動などの機能があるかどうか疑問に思っていました。

SDLのようなものがあればいいのにと思っていましたが、今のところあまり見つけられていません。

何か案は??

4

3 に答える 3

11

Boost Filesystemは、おそらくあなたが求めているものでしょうか?

于 2010-05-05T01:46:11.603 に答える
4

boost::filesystemもファンです。必要なものを書くのに最小限の労力で済みます。次の例(どのように見えるかを確認するため)では、パスとファイル名を入力するようにユーザーに求めます。ルートディレクトリにあるかどうかに関係なく、その名前のすべてのファイルのパスが取得されます。 、またはそのルートディレクトリの任意のサブディレクトリ:

#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;
}
于 2010-05-05T02:10:53.453 に答える
3

ディレクトリ構造をトラバースしたり、クロスプラットフォームの方法でディレクトリ内のファイルを一覧表示したりするネイティブ C++ の方法はありません。言語に組み込まれていないだけです。(正当な理由で!)

あなたの最善の策は、コード フレームワークを使用することです。優れたオプションがたくさんあります。

ブーストファイルシステム

Apache ポータブル ランタイム

そして私の個人的なお気に入り - Qt

ただし、これを使用する場合、ファイル システム部分だけを使用するのは困難です。アプリケーション全体を Qt 固有のクラスに移植する必要があります。

于 2010-05-05T02:33:01.583 に答える