2017-09-14 17 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() 
      } 
     } 
    } 

이전 컴파일러 (윈도우)에서 작동 :

#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

Namespacename은 허용되지 않습니다. –

+0

C++ 17 컴파일러가 필요합니다. 대신 Linux의 dirent.h 헤더에 opendir()/readdir() 함수가 필요합니다 –