2012-10-12 3 views
0

제거하는 동안 내 응용 프로그램에서 만든 모든 임시 파일을 삭제하려고합니다. 다음 코드를 사용합니다 :Windows에서 Qt를 사용하여 현재로드 된 파일 삭제

bool DeleteFileNow(QString filenameStr) 
    { 
     wchar_t* filename; 
     filenameStr.toWCharArray(filename); 

     QFileInfo info(filenameStr); 

     // don't do anything if the file doesn't exist! 
     if (!info.exists()) 
      return false; 

     // determine the path in which to store the temp filename 
     wchar_t* path; 
     info.absolutePath().toWCharArray(path); 

     TRACE("Generating temporary name"); 
     // generate a guaranteed to be unique temporary filename to house the pending delete 
     wchar_t tempname[MAX_PATH]; 
     if (!GetTempFileNameW(path, L".xX", 0, tempname)) 
      return false; 

     TRACE("Moving real file name to dummy"); 
     // move the real file to the dummy filename 
     if (!MoveFileExW(filename, tempname, MOVEFILE_REPLACE_EXISTING)) 
     { 
      // clean up the temp file 
      DeleteFileW(tempname); 
      return false; 
     } 

     TRACE("Queueing the OS"); 
     // queue the deletion (the OS will delete it when all handles (ours or other processes) close) 
     return DeleteFileW(tempname) != FALSE; 
    } 

내 응용 프로그램이 충돌합니다. 나는 그 작업을 수행하기위한 몇 가지 누락 된 Windows dll로 인해 생각합니다. Qt 만 사용하여 동일한 작업을 수행하는 다른 방법이 있습니까?

+1

toWCharArray의 문서에 따르면 "배열 (파일의 파일 이름)은 호출자가 할당해야하며 전체 문자열을 저장할 충분한 공간이 있어야합니다". –

+0

@Roku 감사합니다. – ssk

답변

1

로쿠는 이미 QString 및 wchar_t *로 조작하는 데 문제를 말했습니다. QString Class Reference, method toWCharArray :

int QString::toWCharArray (wchar_t * array) const 

이 QString 객체에 포함 된 데이터로 배열을 채 웁니다 설명서를 참조하십시오. 배열은 wchar_t가 2 바이트 (예 : windows)이고 wchar_t가 4 바이트 인 플랫폼 (대부분의 Unix 시스템)에서는 ucs4에서 utf16으로 인코딩됩니다.

배열은 호출자가 할당해야하며 전체 문자열 (문자열과 항상 동일한 길이의 배열 할당)을 저장할 충분한 공간이 있어야합니다.

은 문자열의 실제 길이를 배열로 반환합니다.

0

단순히 Qt를 사용하여 파일을 제거하는 방법을 찾고 있다면, 당신이 원하는 경우 Qt는, 당신을위한 임시 파일의 전체 수명주기를 관리 QTemporaryFile 살펴보고 QFile::remove:

QFile file(fileNameStr); 
file.remove(); // Returns a bool; true if successful 

사용 :

QTemporaryFile tempFile(fileName); 
if (tempFile.open()) 
{ 
    // Do stuff with file here 
} 

// When tempFile falls out of scope, it is automatically deleted. 
+0

Windows 파일 시스템에는 열려있는 파일을 삭제할 수없는 Unix와는 다른 접근 방식이 있습니다. 이 질문은 그것을 극복하는 것에 관한 것입니다. – hyde

관련 문제