当前位置:首页 » 文件管理 » 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;
}

热点内容
db2建表空间时怎么配置页大小 发布:2024-11-15 17:58:45 浏览:424
我的世界好玩地铁服务器 发布:2024-11-15 17:48:54 浏览:359
1710小游戏服务器ip 发布:2024-11-15 17:48:01 浏览:663
狂三脚本 发布:2024-11-15 17:31:38 浏览:872
附近存储柜 发布:2024-11-15 17:15:17 浏览:452
王选解决汉字存储问题 发布:2024-11-15 17:15:11 浏览:660
球球大作战安卓为什么不能玩哪些模式 发布:2024-11-15 17:14:26 浏览:996
存储器讲课 发布:2024-11-15 17:14:12 浏览:196
安卓充电头怎么称呼 发布:2024-11-15 17:11:17 浏览:446
猎人手游源码 发布:2024-11-15 17:09:28 浏览:433