我想知道是否有任何一種便攜式(Mac & Windows)讀取和寫入超出iostream.h的硬盤驅動器的方法,特別是獲取所有文件列表在一個文件夾中,移動文件等等。帶硬盤驅動器的C++ IO
我希望能有像SDL一樣的東西,但到目前爲止我一直無法找到太多東西。
任何想法??
我想知道是否有任何一種便攜式(Mac & Windows)讀取和寫入超出iostream.h的硬盤驅動器的方法,特別是獲取所有文件列表在一個文件夾中,移動文件等等。帶硬盤驅動器的C++ IO
我希望能有像SDL一樣的東西,但到目前爲止我一直無法找到太多東西。
任何想法??
沒有本地C++方法來遍歷目錄中的一個目錄結構或列表文件中跨平臺的方式。它只是沒有內置到語言中。 (有充分的理由!)
你最好打賭是去一個代碼框架,並有很多很好的選擇。
Aaaand我個人的最愛 - Qt
儘管如此,如果你使用這個很難只是使用它的文件系統部分。你幾乎必須將你的整個應用程序移植到Qt特定的類。
難道Boost Filesystem可能是你在追求什麼?
鏈接到更新的版本(1.42)http://www.boost.org/doc/libs/1_42_0/libs/filesystem/doc/index.htm – 2010-05-05 02:04:34
謝謝 - 我已經更新了我的答案以鏈接到那個。 – Smashery 2010-05-05 05:38:12
我也是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;
}
3種可能解決方案的獎勵積分! – Tomas 2010-05-07 14:51:14
那麼,「這是不是有原因內置」可能是暫時的。我認爲boost.FileSystem會在TR2中進入標準版,但自從...之後沒有聽到任何聲音...... – rubenvb 2011-01-26 20:31:03