1

特定のディレクトリにあるもののリストを返す関数を探しています。私が得た最も近いものはこれを使用することです:

system("dir");

しかし、これは作業ディレクトリの内容を印刷するだけであり、他の場所にCDを挿入することはできません。

私はWindowsを使用していますが、クロスプラットフォームにする予定はないので、心配する必要はありません。

4

1 に答える 1

2

このページから直接コピーした次の例を見てください。それはboost::filesystemすべての主要なシステムで動作するように使用します。

int main(int argc, char* argv[])
{
    path p (/* Specify a directory here */);

    try
    {
        if (exists(p))    // does p actually exist?
        {
            if (is_regular_file(p))        // is p a regular file?   
                cout << p << " size is " << file_size(p) << '\n';
            else if (is_directory(p))      // is p a directory?
            {
                cout << p << " is a directory containing:\n";

                copy(directory_iterator(p), directory_iterator(), // directory_iterator::value_type
                  ostream_iterator<directory_entry>(cout, "\n")); // is directory_entry, which is
                                                                  // converted to a path by the
                                                                  // path stream inserter
            }
            else
                cout << p << " exists, but is neither a regular file nor a directory\n";
        }
        else
            cout << p << " does not exist\n";
    }

    catch (const filesystem_error& ex)
    {
        cout << ex.what() << '\n';
    }

    return 0;
}
于 2012-05-14T04:09:06.350 に答える