C ++ IO with hard drive

I was wondering if there is any portable (Mac & Windows) way to read and write to the hard drive that goes beyond iostream.h, in particular, such functions as getting a list of all the files in a folder, moving files around, etc. d.

I was hoping there would be something like SDL around, but so far I have not been able to find much.

Any ideas?

+4
source share
3 answers

There is no C ++ built-in method for moving directory structures or list files in a directory in a cross-platform manner. It is simply not built into the language. (For good reason!)

It is best to go with a code framework, and there are many good options.

Boost file system

Apache portable runtime

Aaaand is my personal favorite - Qt

Although, if you use this, it's hard to just use part of the file system. You pretty much have to port your entire application to Qt classes.

+3
source

Can a Boost Fileystem be what you are after?

+11
source

I am also a fan of boost::filesystem . It takes minimal effort to write what you want. The following example (just to make you feel like it looks), asks the user to enter the path and file name, and he will get the paths of all files with this name, regardless of whether they are in the root directory, or in any subdirectory of this root directory:

 #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
source

Source: https://habr.com/ru/post/1308840/


All Articles