visual studio - Clear data inside all files in C++ -
hi guys programming on c++. wish clear data inside of files in current directory. can tell me command files?
that trying doesn't work:
ofs.open("*.*", ios::out | ios::trunc);
the problem is: open("*.*",
fstream can't open files of directory, instead, can iterate each file.
example works on c++17
#include <string> #include <iostream> #include <filesystem> #include <fstream> //namespace fs = std::experimental::filesystem; //for visual studio namespace fs = std:::filesystem; int main() { std::string path = "path_to_directory"; (auto & p : fs::directory_iterator(path)) { if (fs::is_regular_file(p)){ std::fstream fstr; fstr.open(p.path().c_str(), std::ios::out | std::ios::trunc); //do fstr.close() } } }
older compilers(windows):
#include <windows.h> #include <string> #include <fstream> std::wstring path = l"path_to_directory"; path += l"\\*"; win32_find_data data; handle hfind; if ((hfind = findfirstfile(path.c_str(), &data)) != invalid_handle_value) { { if (data.dwfileattributes & file_attribute_archive) { std::fstream fstr; fstr.open(data.cfilename, std::ios::out | std::ios::trunc); //do fstr.close(); } } while (findnextfile(hfind, &data) != 0); findclose(hfind); }
Comments
Post a Comment