導航:首頁 > 文件教程 > vc判斷路徑文件

vc判斷路徑文件

發布時間:2022-12-23 05:32:03

❶ VC++如何檢測文件是否存在

VC++ 判斷文件是否存在的方法有:

1. 使用_access函數,函數原型為:
int _access( const char *path, int mode );

2. 使用CreateFile函數,函數原型為:
HANDLE CreateFile( LPCTSTR lpFileName, //pointer to name of the file DWORD dwDesiredAccess, // access (readwrite) modeDWORD dwShareMode, // share mode LPSECURITY_, // 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( LPCTSTRlpFileName // pointer to the name of a file or directory );

5. 使用Shell Lightweight Utility APIs函數。
PathFileExists()專門判斷文件和目錄時否存在的函數文件名可讀性比較強還可以判斷目錄是否存在Header: Declared in Shlwapi.h ,Import Library: Shlwapi.lib 。
方法1:
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
void main( void ) {
if( (_access( "D:\\a.txt", 0 )) != -1 ) {
printf( "File ACCESS.C exists\n" );
if( (_access( "ACCESS.C", 2 )) != -1 )
printf( "File ACCESS.C has write permission\n" ); } }
方法2:
if (INVALID_HANDLE_VALUE != CreateFile("D:\\a.txt", GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) {
AfxMessageBox("File ACCESS.C exists\n"); }
方法3:

#define _WIN32_WINNT 0x0400
#include <windows.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
printf ("Target file is %s.\n", argv[1]);
hFind = FindFirstFile(argv[1], &FindFileData);
if (hFind == INVALID_HANDLE_VALUE) {
printf ("Invalid File Handle. GetLastError reports %d\n", GetLastError ()); return (0); }
else {
printf ("The first file found is %s\n", FindFileData.cFileName); FindClose(hFind);
return (1); } }
方法4:
if (GetFileAttributes("c:\\1.txt") == -1)
MessageBox(0."Invalid File ","hehe",0)
else
MessageBox(0."The first file found ","haha",0)
方法5:
if (INVALID_HANDLE_VALUE != CreateFile("D:\\a.txt", 0, 0, NULL, OPEN_EXISTING, 0, NULL)) {
AfxMessageBox("File exists\n");
}

❷ 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);
}

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

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

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

❹ VC如何檢測當前路徑文件列表

首先通過GetCurrentDirectory獲取當前路徑,
然後通過CFindFile類的FindFile和FindNextFile、GetFileName就可以遍歷當前路徑的所有文件。

❺ VC環境中用C語言查找當前路徑下的所有文件和文件夾的函數是什麼

這是我的TFTP程序中的一個函數,是搜索當前盤符下的所有文件,包括文件的大小,並發送到客戶端,其中就有查找當前路徑下的文件,你自己挑一下,應該能完成你的需求。
void FileList(sockaddr_in sour_addr,char strStartDir[])
{
char sendbuffer[1024];
sockaddr_in destaddr;

int sourlen = 0;
int ret = 0;
int len = 0;
int flen = 0;

fd_set fdr;

unsigned short blocknum = 0;

FILE *file;
char filename[128];

strcpy(filename,strStartDir+2); /*獲取文件名*/

strcat(filename,"\\*");
destaddr.sin_family = AF_INET;
destaddr.sin_port = sour_addr.sin_port;
destaddr.sin_addr.s_addr = inet_addr(desthost);//

WIN32_FIND_DATA FindFileData;
HANDLE hFind;
hFind = FindFirstFile(filename, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("Invalid File Handle");
}
else
{
while(FindNextFile(hFind,&FindFileData))
{
printf(FindFileData.cFileName);
printf("\r\n");
memset(sendbuffer,'\0',1024);

len = filldata(blocknum++,FindFileData.cFileName,strlen(FindFileData.cFileName),sendbuffer,sizeof(sendbuffer));
ret = sendto(serverSock,sendbuffer,len,0,(sockaddr *)&destaddr,sizeof(destaddr));

}
len = fillover(blocknum,"Over",4,sendbuffer,sizeof(sendbuffer));
ret = sendto(serverSock,sendbuffer,len,0,(sockaddr *)&destaddr,sizeof(destaddr));
FindClose(hFind);
return;
}
}

❻ 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;
}

❼ vc判斷文件夾是否存在

VC有這么一個函數
bool PathIsDirectory(CString path)
判斷你輸入的路徑是否為文件夾,當然也可以判斷文件夾是否存在。

❽ vc怎麼查找某一路徑下指定名稱的文件是否存在

FILE *fp = fopen(strPath, "r")

if(fp == NULL)
{
//沒有同名文件
}

❾ 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 就是文件長度

❿ VC++通過絕對路徑判斷是文件還是文件夾的問題

你的錯誤在於,FindFirstFile的第一個參數,這個參數不能是具體文件名,而應該是這樣:

filePath=_T("f:\aaa\*.*");

注意,你要遍歷,那麼必須是*.*或者*.txt之類的字元串,然後在循環中使用FindNextFile來獲取下一個。

閱讀全文

與vc判斷路徑文件相關的資料

熱點內容
文件夾和標簽系統 瀏覽:559
光明日報圖文資料庫在哪裡 瀏覽:940
photoshop臨時文件夾 瀏覽:250
手機里為何找不到微信文件夾 瀏覽:220
cad塊文件另存為dwg 瀏覽:787
photoshop合成兩個pdf文件 瀏覽:354
保密文件如何外借 瀏覽:763
手機無線密碼怎麼查看 瀏覽:821
如何刪除己下載未安裝的文件 瀏覽:806
招商銀行app怎麼登陸二類卡 瀏覽:508
谷歌瀏覽器的debug調試工具 瀏覽:163
哪個數控編程是免費的 瀏覽:368
蘋果7plus玫瑰金報價 瀏覽:800
杭州自行車系統微信號 瀏覽:352
網路語偷豬是什麼意思 瀏覽:309
三星i9300線刷教程圖解 瀏覽:248
編程競賽特訓哪裡好 瀏覽:28
提取驅動文件 瀏覽:102
如何壓縮wps文件 瀏覽:314
ps轉ico文件 瀏覽:303

友情鏈接