vc判断文件是不是存在

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vc判断文件是不是存在相关的知识,希望对你有一定的参考价值。

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

#include <shlwapi.h>
#pragma comment(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_ctime   The date and time the file was created.
 

CTime m_mtime   The date and time the file was last modified.

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

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

BYTE m_attribute   The 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

参考技术A 虽然现在的函数更新很快,但有种东西叫经典,它含有权威

VC++如何检测文件是不是存在

Delphi 里面 uses了 SysUtils 以后

if FileExists(text1.Text) then
begin
//
end;

这段代码在 VC++里面怎么实现? 如果要引用什么头文件,应该引用什么? 在哪里引用??

VC++中检测文件是否存在有很多种方法,比较通用的是GetFileAttributes函数,在控制台下增加#include<windows.h>,MFC和其它窗口程序下可以直接使用:

if(GetFileAttributes(FileName)<0)//==-1

    //文件不存在!

else
    //存在

这种方法是windows下推荐使用的,并可以用于目录(文件夹)的判断(返值为FILE_ATTRIBUTE_DIRECTORY),不会因为文件访问权限等问题造成误判。


此外还有很多判断方法,各有优势和适用范围,需要注意的是,各个方法判断的“文件存在和不存在”可能包括同名目录、用户权限、文件夹不存在或禁止访问等问题,应该根据具体需求使用适合的方法:

    CFile::Open或OpenFile,无法以读取方式打开

    _access函数,通过判断文件是否能够访问,返回文件是否存在

    CFileFind类或FindFirstFile函数。这个也是很不错的方法,不但能够获取比较详细的文件存在信息,还能使用递归搜索子目录。

    使用Shell函数SHGetFileInfo判断文件是否存在,这个方法可以获取更完整的文件信息,不仅能判断是否存在,还能直接获取文件的shell信息,方便进一步处理。

参考技术A VC++中检测文件是否存在有很多种方法,比较通用的是GetFileAttributes函数,在控制台下增加#include<windows.h>,MFC和其它窗口程序下可以直接使用:
if(GetFileAttributes(FileName)<0)//==-1

//文件不存在!

else
//存在这种方法是windows下推荐使用的,并可以用于目录(文件夹)的判断(返值为FILE_ATTRIBUTE_DIRECTORY),不会因为文件访问权限等问题造成误判。
此外还有很多判断方法,各有优势和适用范围,需要注意的是,各个方法判断的“文件存在和不存在”可能包括同名目录、用户权限、文件夹不存在或禁止访问等问题,应该根据具体需求使用适合的方法:
1.
CFile::Open或OpenFile,无法以读取方式打开
2.
_access函数,通过判断文件是否能够访问,返回文件是否存在
3.
CFileFind类或FindFirstFile函数。这个也是很不错的方法,不但能够获取比较详细的文件存在信息,还能使用递归搜索子目录。
4.
使用Shell函数SHGetFileInfo判断文件是否存在,这个方法可以获取更完整的文件信息,不仅能判断是否存在,还能直接获取文件的shell信息,方便进一步处理。
参考技术B vc++中检测文件是否存在有很多种方法,比较通用的是getfileattributes函数,在控制台下增加#include
,mfc和其它窗口程序下可以直接使用:
if(getfileattributes(filename)<0)//==-1

//文件不存在!

else
//存在这种方法是windows下推荐使用的,并可以用于目录(文件夹)的判断(返值为file_attribute_directory),不会因为文件访问权限等问题造成误判。
此外还有很多判断方法,各有优势和适用范围,需要注意的是,各个方法判断的“文件存在和不存在”可能包括同名目录、用户权限、文件夹不存在或禁止访问等问题,应该根据具体需求使用适合的方法:
1.
cfile::open或openfile,无法以读取方式打开
2.
_access函数,通过判断文件是否能够访问,返回文件是否存在
3.
cfilefind类或findfirstfile函数。这个也是很不错的方法,不但能够获取比较详细的文件存在信息,还能使用递归搜索子目录。
4.
使用shell函数shgetfileinfo判断文件是否存在,这个方法可以获取更完整的文件信息,不仅能判断是否存在,还能直接获取文件的shell信息,方便进一步处理。
参考技术C 给你一个函数:
#include <sys/stat.h>
//文件是否存在
bool FileExist(const char* FileName)

struct stat my_stat;
return (stat(FileName, &my_stat) == 0);


这样调用:
if(FileExist("这里写文件名"))

//这里写如果文件存在的处理代码
本回答被提问者和网友采纳
参考技术D 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_ATTRIBUTESlpSecurityAttributes, // pointer to security attributes DWORD dwCreationDisposition, //how to create DWORD dwFlagsAndAttributes, // file attributes HANDLE hTemplateFile //handle to file with attributes to // copy );

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判断文件是不是存在的主要内容,如果未能解决你的问题,请参考以下文章

vc判断文件是不是存在

vc 判断目录是不是存在 创建

用vc++如何判断一个字符串是不是是汉字?

VC++如何检测文件是不是存在

如何在vc++中判断计算机是不是安装c运行时库

在VC里如何检查一个文件是不是存在