Contents |
#include <fstream>
using namespace std;
...
ofstream file("test.txt");
file << "whatever";
file.close();
#include <fstream>
using namespace std;
...
ifstream file("test.txt");
if(!file) throw Exception("Couldn't open file!");
string line;
while(!file.eof()) {
getline(file, line);
cout << "'" << line << "'" << endl;
}
file.close();
Output full pathname of all files in 'path' and subdirs:
// link with -lboost_filesystem
#include <boost/filesystem/operations.hpp>
#include <iostream>
using namespace boost::filesystem;
using namespace std;
class NoSuchPathException {};
void findFiles(const string& path) throw(NoSuchPathException) {
if(!exists(path)) throw(NoSuchPathException());
// for all dirs
directory_iterator end_itr; // default construction yields past-the-end
for(directory_iterator i(path); i != end_itr; ++i) {
// if dir, recurse
if(is_directory(*i)) findFiles(i->string());
// output file name
else cout << i->string();
}
}