2017-09-14 7 views
0

私はC++でプログラミングしています。私は現在のディレクトリ内のすべてのファイルの中のすべてのデータをクリアします。誰かが私にすべてのファイルを取得するよう指示することはできますか?C++のすべてのファイル内のデータをクリアします

ofs.open("*.*", ios::out | ios::trunc); 

問題は次のとおりです:open("*.*",

答えて

1

fstreamのではなく、ディレクトリのすべてのファイルを開くことができない、あなたがそれぞれを繰り返すことができ、私がしようとしていますが、それは動作しません何です

ファイル。
は、この例では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"; 
     for (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 something 
       fstr.close() 
      } 
     } 
    } 

古いコンパイラ(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) { 
    do { 
     if (data.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) { 
      std::fstream fstr; 
      fstr.open(data.cFileName, std::ios::out | std::ios::trunc); 
      //do something 
      fstr.close(); 
     } 
    } while (FindNextFile(hFind, &data) != 0); 
    FindClose(hFind); 
} 
+0

おかげでチームメイトが、それはまったく機能しません。.. HTTPS://i.imgurを。 com/li6azNC.png –

+0

名前空間名は使用できません。 –

+0

C++ 17コンパイラが必要です。代わりに、linuxのdirent.hヘッダにopendir()/ readdir()関数が必要です –

関連する問題