當前位置:首頁 » 文件管理 » vc判斷文件夾是否存在

vc判斷文件夾是否存在

發布時間: 2022-04-29 22:02:56

A. vc判斷文件是否存在

方法一:PathFileExists(FilePath); 返回true則存在,返回false則不存在,注意要加上以下代碼:

#include<shlwapi.h>
#pragmacomment(lib,"Shlwapi.lib")

方法二:CFile::GetStatus(WMSIniFilePath,filestatus),返回true則存在,返回false則不存在

參數:

rStatus:

A reference to a user-supplied CFileStatus structure that will receive the status information. The CFileStatus structure has the following fields:

CTime m_ctimeThe date and time the file was created.

CTime m_mtimeThe date and time the file was last modified.

CTime m_atimeThe date and time the file was last accessed for reading.

LONG m_sizeThe logical size of the file in bytes, as reported by the DIR command.

BYTE m_attributeThe attribute byte of the file.

char m_szFullName[_MAX_PATH]The absolute filename in the Windows character set.

lpszFileName:

A string in the Windows character set that is the path to the desired file. The path can be relative or absolute, but cannot contain a network name.

參考:http://msdn.microsoft.com/zh-cn/aa270504

B. VC判斷文件是否存在

推薦實例例: if(::GetFileAttributes(m_filename)==-1){//文件不存在}else{//文件存在} 1. 使用_access函數,函數原型為 int _access( const char *path, int mode ); 2. 使用CreateFile函數,函數原型為: HANDLE CreateFile( LPCTSTR lpFileName, // pointer to name of the file DWORD dwDesiredAccess, // access (read-write) mode DWORD dwShareMode, // share mode LPSECURITY_ATTRIBUTES lpSecurityAttributes, // pointer to security attributes DWORD dwCreationDisposition, // how to create DWORD dwFlagsAndAttributes, // file attributes HANDLE hTemplateFile // handle to file with attributes to // ); 3. 使用FindFirstFile函數,函數原型為: HANDLE FindFirstFile( LPCTSTR lpFileName, // pointer to name of file to search for LPWIN32_FIND_DATA lpFindFileData // pointer to returned information ); 4. 使用GetFileAttributes函數,函數原型如下: DWORD GetFileAttributes( LPCTSTR lpFileName // pointer to the name of a file or directory ); 5. 使用Shell Lightweight Utility APIs函數 PathFileExists()專門判斷文件和目錄時否存在的函數文件名可讀性比較強還可以判斷目錄是否存在 Header: Declared in Shlwapi.h Import Library: Shlwapi.lib 以上的各種方法供參考,函數具體用法需參見MSDN

C. VC怎麼通過絕對路徑確定一個文件或者文件夾是否存在,如果是文件存在,怎麼獲得該文件的大小

#include "io.h"

access("路徑+文件名", 0) == -1 的時候文件不存在,否則就存在

FILE *stream= fopen("filenmae", "rb");
long curpos, length;
curpos = ftell(stream);
fseek(stream, 0L, SEEK_END);
length = ftell(stream);
fseek(stream, curpos, SEEK_SET);
fclose(stream);

length 就是文件長度

D. VC 怎樣判斷一個文件夾是否存在

PathFileExists()函數, 例子如下:

#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"

void main( void )
{
// Valid file path name (file is there).
char buffer_1[] = "C:\\TEST\\file.txt";
char *lpStr1;
lpStr1 = buffer_1;

// Invalid file path name (file is not there).
char buffer_2[] = "C:\\TEST\\file.doc";
char *lpStr2;
lpStr2 = buffer_2;

// Return value from "PathFileExists".
int retval;

// Search for the presence of a file with a true result.
retval = PathFileExists(lpStr1);
if(retval == 1)
{
cout << "Search for the file path of : " << lpStr1 << endl;
cout << "The file requested \"" << lpStr1 << "\" is a valid file" << endl;
cout << "The return from function is : " << retval << endl;
}

else{
cout << "\nThe file requested " << lpStr1 << " is not a valid file" << endl;
cout << "The return from function is : " << retval << endl;
}

// Search for the presence of a file with a false result.
retval = PathFileExists(lpStr2);
if(retval == 1)
{
cout << "\nThe file requested " << lpStr2 << "is a valid file" << endl;
cout << "Search for the file path of : " << lpStr2 << endl;
cout << "The return from function is : " << retval << endl;
}

else{
cout << "\nThe file requested \"" << lpStr2 << "\" is not a valid file" << endl;
cout << "The return from function is : " << retval << endl;
}
}

E. vc 判斷目錄是否存在 創建

注意,目錄不能自動遞歸創建,必須一級一級的創建

BOOLIsDirExist(LPCTSTRszDir)
{
HANDLEhFile=::CreateFile(szDir,FILE_READ_ATTRIBUTES,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,NULL);
if(INVALID_HANDLE_VALUE==hFile)
{
returnFALSE;
}
::CloseHandle(hFile);
returnTRUE;
}


BOOLCreateDir(LPCTSTRszDir)
{
returnCreateDirectory(szDir,NULL);
}

F. VC如何獲取系統目錄並檢測文件是否存在

#include "windows.h"

int main( int nargc, char** pArgv )
{
WCHAR szPath[MAX_PATH];
// 獲得系統目錄
GetWindowsDirectoryW( szPath, sizeof(szPath) );
// 格式化文件路徑
wcscat( szPath, L"\\123.txt" );

WIN32_FIND_DATA wfd;
if ( INVALID_HANDLE_VALUE != FindFirstFileW( szPath, &wfd ) )
{
DeleteFileW( szPath ); // 如果存在則刪除
}

return 0;
}

補充:
原來你是用寬字元,按以上修改。

G. vc中如何判斷文件路徑是否存在

HANDLE WINAPI FindFirstFile(
__in LPCTSTR lpFileName,
__out LPWIN32_FIND_DATA lpFindFileData
);

參數是路徑,判斷返回的句柄是否有效。

H. VC 如何判斷某目錄下是否還有文件夾的存在

寫個例子給你參考下吧
#include<stdio.h>
#include<string.h>
#include<io.h>
intExistSubFoloder(char*path)
{
struct_finddata_tfind_data;
intfhandle=_findfirst(path,&find_data);
if(fhandle!=-1)
{
while(_findnext(fhandle,&find_data)==0)
{
if(strcmp(find_data.name,".."))
{
if((find_data.attrib&_A_SUBDIR)==_A_SUBDIR)
{
//printf("%s ",find_data.name);
return1;
}
}
}
}
_findclose(fhandle);
return0;
}
intmain(void)
{
if(ExistSubFoloder("C:\123\*.*"))
{
printf("存在子目錄! ");
}
else
{
printf("子目錄,不存在! ");
}
return0;
}

熱點內容
價錢低高配置的有哪些車 發布:2024-11-15 19:34:53 瀏覽:380
androidgps定位開發 發布:2024-11-15 19:34:52 瀏覽:334
如何掃碼添加伺服器地址 發布:2024-11-15 19:31:48 瀏覽:278
sql語句復制資料庫 發布:2024-11-15 19:28:02 瀏覽:837
演算法的薪資 發布:2024-11-15 19:15:22 瀏覽:322
ubuntu可以重新編譯嗎 發布:2024-11-15 19:09:10 瀏覽:649
access資料庫表的創建 發布:2024-11-15 18:51:18 瀏覽:141
怎麼搭建信令伺服器 發布:2024-11-15 18:48:03 瀏覽:578
如何解鎖安卓手機鎖屏圖 發布:2024-11-15 18:47:48 瀏覽:966
c語言水仙花數編程 發布:2024-11-15 18:45:50 瀏覽:852