int main() { const std::string filename_from = "tmp.txt"; const std::string filename_to = "tmp2.txt"; //Delete possible old temporary files DeleteFile(filename_from); DeleteFile(filename_to); //Create new file { std::ofstream f(filename_from.c_str()); f << "TMP"; f.close(); } //Only filename_from will exist assert( IsRegularFile(filename_from)); assert(!IsRegularFile(filename_to)); //Rename std::rename(filename_from.c_str(),filename_to.c_str()); //Only filename_to will exist assert(!IsRegularFile(filename_from)); assert( IsRegularFile(filename_to)); }
//From http://www.richelbilderbeek.nl/CppCopyFile.htm void CopyFileStl(const std::string& fileNameFrom, const std::string& fileNameTo) { assert(IsRegularFile(fileNameFrom)); if(IsRegularFile(fileNameTo)) { throw std::logic_error("Cannot copy over an existing file"); } std::ifstream in (fileNameFrom.c_str()); std::ofstream out(fileNameTo.c_str()); out << in.rdbuf(); out.close(); in.close(); }
bool operator()(const std::string& path) { if(IsSymlink(path) && !(mask_ & SYMLINK)) return false; return ( (mask_ & REGULAR_FILE) && IsRegularFile(path) ) || ( (mask_ & DIRECTORY) && IsDirectory(path) ); }
int main(int, char* argv[]) { const std::string tmp_filename = "temp.txt"; //Delete file (in case it exists) std::remove(tmp_filename.c_str()); //Copy file CopyFileStl(argv[0],tmp_filename); assert(IsRegularFile(tmp_filename)); //Clean up temp file by deleting it std::remove(tmp_filename.c_str()); }
// Returns a full path of the next file // Returns nullptr if finished iteration. // Returned value is valid only until we call Next() again. const WCHAR *DirIter::Next() { // when we enter here, currFindData has info for an entry // we haven't processed yet (filled by StartDirIter() or // ourselves at the end) unless foundNext is false currPath.Set(nullptr); while (foundNext && !currPath) { WCHAR *f = currFindData.cFileName; if ((currFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { if (recursive && !IsSpecialDir(f)) { WCHAR *d = path::Join(currDir, f); dirsToVisit.Append(d); } } else if (IsRegularFile(currFindData.dwFileAttributes)) { WCHAR *p = path::Join(currDir, f); currPath.Set(p); } BOOL hasMore = FindNextFile(currFindHandle, &currFindData); if (!hasMore) foundNext = TryNextDir(); } return currPath; }
int main() { //Use the following structure: // - local.txt // - my_folder/in_folder.txt // - my_folder/my_subfolder/in_subfolder.txt const std::string local_filename { "local.txt" }; const std::string folder_name { "my_folder" }; const std::string in_folder_filename { "in_folder.txt" }; const std::string subfolder_name { "my_subfolder" }; const std::string in_subfolder_filename { "in_subfolder.txt" }; //File and folder creation { { const std::string filename { local_filename }; std::ofstream f(filename.c_str()); } assert(IsRegularFile(local_filename)); { const std::string cmd = "mkdir " + folder_name; const int error = std::system(cmd.c_str()); assert(!error); } assert(IsFolder(folder_name)); { const std::string filename { folder_name + GetPathSeperator() + in_folder_filename }; std::ofstream f(filename.c_str()); } assert(IsRegularFile(folder_name + GetPathSeperator() + in_folder_filename)); { const std::string cmd = "mkdir " + folder_name + GetPathSeperator() + subfolder_name; const int error = std::system(cmd.c_str()); assert(!error); } assert(IsFolder(folder_name + GetPathSeperator() + subfolder_name)); { const std::string filename { folder_name + GetPathSeperator() + subfolder_name + GetPathSeperator() + in_subfolder_filename }; std::ofstream f(filename.c_str()); } assert( IsRegularFile( folder_name + GetPathSeperator() + subfolder_name + GetPathSeperator() + in_subfolder_filename ) ); } //Reading of the folder created { const std::vector<std::string> v { GetFoldersInFolder(folder_name) }; std::copy(v.begin(),v.end(),std::ostream_iterator<std::string>(std::cout,"\n")); assert(v.size() == 1); assert( std::count( v.begin(),v.end(), subfolder_name ) == 1 ); } { const std::vector<std::string> v { GetFoldersInFolder(folder_name + GetPathSeperator() + subfolder_name) }; std::copy(v.begin(),v.end(),std::ostream_iterator<std::string>(std::cout,"\n")); assert(v.size() == 0); } }
bool operator()(const std::string& path) const { return IsRegularFile(path) && FileSize(path) > 0; }
//From http://www.richelbilderbeek.nl/CppCopyFile.htm void CopyFileBoost(const std::string& from, const std::string& to) { assert(IsRegularFile(fileNameFrom)); //Boost will check if the copy is made over an existing file boost::filesystem::copy_file(from,to); }
void ribi::Rinside::Test() noexcept { { static bool is_tested{false}; if (is_tested) return; is_tested = true; } { ribi::fileio::FileIo(); } auto& r = Rinside().Get(); const auto f = ribi::fileio::FileIo(); //Read/write of integer { r["x"] = 42; const Rcpp::NumericVector v = r.parseEval("x"); assert(v.size() == 1); assert(v[0] == 42); } //Read/write of integer vector { const Rcpp::NumericVector v = {1,2,3}; r["v"] = v; const Rcpp::NumericVector w = r.parseEval("v"); assert(v == w); } //Read/write of plot with known name { if(f.IsRegularFile("tmp.png")) { f.DeleteFile("tmp.png"); } assert(!f.IsRegularFile("tmp.png")); r.parseEval("png(filename=\"tmp.png\")"); r.parseEval("plot(sin)"); r.parseEval("dev.off()"); assert(f.IsRegularFile("tmp.png")); f.DeleteFile("tmp.png"); } { const std::string newick{"((F:2,G:2):1,H:3);"}; if(f.IsRegularFile("tmp.png")) { f.DeleteFile("tmp.png"); } assert(!f.IsRegularFile("tmp.png")); r.parseEvalQ("library(ape)"); r.parseEvalQ("library(geiger)"); r["newick"] = newick; r.parseEvalQ("phylogeny <- read.tree(text = newick)"); r.parseEvalQ("png(filename=\"tmp.png\")"); r.parseEvalQ("plot(phylogeny)"); r.parseEvalQ("dev.off()"); assert(f.IsRegularFile("tmp.png")); f.DeleteFile("tmp.png"); } //Use any filename { const std::string filename = f.GetTempFileName(".png"); assert(!f.IsRegularFile(filename)); r.parseEval("png(filename=\""+filename+"\")"); r.parseEval("plot(sin)"); r.parseEval("dev.off()"); assert(f.IsRegularFile(filename)); f.DeleteFile(filename); } //Use any filename with a name in the R script { const std::string filename = f.GetTempFileName(".png"); assert(!f.IsRegularFile(filename)); r["png_filename"] = filename; r.parseEval("png(filename=png_filename)"); r.parseEval("plot(sin)"); r.parseEval("dev.off()"); assert(f.IsRegularFile(filename)); f.DeleteFile(filename); } //assert(1==2); }
///Delete a file //From http://www.richelbilderbeek.nl/CppDeleteFile.htm void DeleteFile(const std::string& filename) { std::remove(filename.c_str()); assert(!IsRegularFile(filename)); }