简体   繁体   English

带硬盘的C ++ IO

[英]C++ IO with Hard Drive

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, etc. 我想知道是否有任何类型的便携式(Mac和Windows)读取和写入硬盘驱动器的方法超出了iostream.h,特别是获取文件夹中所有文件的列表,移动文件等等功能。

I was hoping that there would be something like SDL around, but so far I haven't been able to find much. 我希望周围会有类似SDL的东西,但到目前为止我还没有找到太多东西。

Any ideas?? 有任何想法吗??

Boost文件系统可能是你想要的吗?

I am a fan of boost::filesystem also. 我也是boost::filesystem的粉丝。 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;
}

There is no native C++ way to traverse a directory structure or list files in a directory in a cross-platform manner. 没有本机C ++方法以跨平台方式遍历目录结构或列出目录中的文件。 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 Apache Portable Runtime

Aaaand my personal favorite - Qt Aaaand我个人的最爱 - 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. 您几乎必须将整个应用程序移植到Qt特定类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM