首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带硬盘的C++ IO

带硬盘的C++ IO
EN

Stack Overflow用户
提问于 2010-05-05 09:43:48
回答 3查看 778关注 0票数 4

我想知道是否有任何类型的便携式(Mac和Windows)方法来读写硬盘驱动器,它超越了iostream.h,特别是像获取文件夹中所有文件的列表,到处移动文件等功能。

我希望有像SDL这样的东西,但到目前为止我还没有找到太多。

有什么想法吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-05-05 10:33:02

没有本机C++方法来以跨平台的方式遍历目录结构或列出目录中的文件。它只是没有内置到语言中。(有很好的理由!)

你最好的选择就是使用代码框架,而且有很多好的选择。

Boost Filesystem

Apache Portable Runtime

Aaaand和我个人最喜欢的- Qt

但是,如果使用它,就很难只使用它的文件系统部分。您几乎必须将整个应用程序移植到Qt特定的类。

票数 3
EN

Stack Overflow用户

发布于 2010-05-05 09:46:12

Boost Filesystem有可能是你要找的人吗?

票数 11
EN

Stack Overflow用户

发布于 2010-05-05 10:10:53

我也是boost::filesystem的粉丝。只需很少的努力就可以写出你想要的东西。下面的示例(只是为了让您感受一下它的样子),要求用户输入路径和文件名,它将获得具有该名称的所有文件的路径,无论它们是在根目录中,还是在该根目录的任何子目录中:

代码语言:javascript
复制
#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;
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2770104

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档