导航:首页 > 文件类型 > vc文件夹文件是否存在

vc文件夹文件是否存在

发布时间:2024-01-15 22:47:07

Ⅰ 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判断文件是否存在

方法一: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

Ⅲ VC环境下如何检测指定文件夹中是否存在某个文件

CreateFile打开,成功当然存在,失败再用GetLastError看原因,根据失败原因判断文件是否存在。

另外,CFileFind应该没什么问题。可参考:
CFileFind finder;
BOOL bWorking = finder.FindFile(_T("*.*"));
while (bWorking)
{
bWorking = finder.FindNextFile();
TRACE(_T("%s\n"), (LPCTSTR)finder.GetFileName());
}

阅读全文

与vc文件夹文件是否存在相关的资料

热点内容
战地五的图标文件在哪里 浏览:553
闪迪卡更改文件系统 浏览:599
参数数据是什么证据 浏览:433
神木论坛app最新版本 浏览:949
住建局175号文件具体内容 浏览:943
手机管家清理大文件怎么恢复 浏览:730
华为t8830应用程序已满怎么删除教程 浏览:815
转储的数据库文件怎么导入 浏览:527
怎么用编程画小花 浏览:65
php文件如何下载文件 浏览:614
javacapsule 浏览:20
extjs按钮垂直居中 浏览:163
ibjsx 浏览:647
直销可编程直流电源哪里买 浏览:952
苹果6qq录音文件听不了 浏览:6
网络线怎么拔 浏览:328
webclip文件有什么危害 浏览:700
创维32e360e怎么看网络电视 浏览:824
js网站加载动画 浏览:411
shelljava 浏览:666

友情链接