데이터 베이스 MySQL 튜토리얼 VC判断文件目录是否存在

VC判断文件目录是否存在

Jun 07, 2016 pm 03:32 PM
http 판단 주소 존재하다 문서 목차

原文地址::http://blog.csdn.net/pgshow/article/details/7684822 相关网帖 1、VC中 判断 目录 , 文件 是否 存在 ,创建 目录 的方法 ----http://www.cnblogs.com/kiddo/archive/2007/09/03/879950.html 1. 使用_ access 函数,函数原型为 int _access( const

原文地址::http://blog.csdn.net/pgshow/article/details/7684822

 

 相关网帖

1、VC中判断目录,文件是否存在,创建目录的方法 ----http://www.cnblogs.com/kiddo/archive/2007/09/03/879950.html

 

 

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 // copy );
3. 使用FindFirstFile函数,函数原型为: HANDLE FindFirstFile( LPCTSTR lpFileName, // pointer to name of file to search for LPWIN32_FIND_DATA lpFindFileData // pointer to returned information );
//例子:

BOOL CPubFunc::DirectoryExist(CString Path)
{
 WIN32_FIND_DATA fd;
 BOOL ret = FALSE;
    HANDLE hFind = FindFirstFile(Path, &fd);
    if ((hFind != INVALID_HANDLE_VALUE) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
    {  //目录存在
            ret = TRUE;    
    }
   FindClose(hFind);
 return ret;
}
 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。
//这是MSDN中的例子:
#include
#include
#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 cout cout }
else{
cout cout }
// Search for the presence of a file with a false result.
retval = PathFileExists(lpStr2);
if(retval == 1)
{
cout cout cout }
else{
cout cout }
}

6.使用CFileFind 类....这是一个InternetServices类,在此可以借用一下。也可以用于遍历文件夹(s可指定深度)
BOOL CPubFunc::FileExist(CString FileName)
{
 CFileFind fFind;
 return fFind.FindFile(FileName);
}

//创建目录
 #include
BOOL CreateDirectory(
  LPCTSTR
lpPathName,                         // pointer to directory path string
  LPSECURITY_ATTRIBUTES lpSecurityAttributes  // pointer to security descriptor
);

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 // copy );
3. 使用FindFirstFile函数,函数原型为: HANDLE FindFirstFile( LPCTSTR lpFileName, // pointer to name of file to search for LPWIN32_FIND_DATA lpFindFileData // pointer to returned information );
//例子:

BOOL CPubFunc::DirectoryExist(CString Path)
{
 WIN32_FIND_DATA fd;
 BOOL ret = FALSE;
    HANDLE hFind = FindFirstFile(Path, &fd);
    if ((hFind != INVALID_HANDLE_VALUE) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
    {  //目录存在
            ret = TRUE;    
    }
   FindClose(hFind);
 return ret;
}
 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。
//这是MSDN中的例子:
#include
#include
#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 cout cout }
else{
cout cout }
// Search for the presence of a file with a false result.
retval = PathFileExists(lpStr2);
if(retval == 1)
{
cout cout cout }
else{
cout cout }
}

6.使用CFileFind 类....这是一个InternetServices类,在此可以借用一下。也可以用于遍历文件夹(s可指定深度)
BOOL CPubFunc::FileExist(CString FileName)
{
 CFileFind fFind;
 return fFind.FindFile(FileName);
}

//创建目录
 #include
BOOL CreateDirectory(
  LPCTSTR
lpPathName,                         // pointer to directory path string
  LPSECURITY_ATTRIBUTES lpSecurityAttributes  // pointer to security descriptor
);

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

뜨거운 기사 태그

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

Quark Cloud Disk에서 Baidu Cloud Disk로 파일을 전송하는 방법은 무엇입니까? Quark Cloud Disk에서 Baidu Cloud Disk로 파일을 전송하는 방법은 무엇입니까? Mar 14, 2024 pm 02:07 PM

Quark Cloud Disk에서 Baidu Cloud Disk로 파일을 전송하는 방법은 무엇입니까?

0x80004005 오류 코드가 나타나는 경우 수행할 작업 편집기에서 0x80004005 오류 코드를 해결하는 방법을 알려줍니다. 0x80004005 오류 코드가 나타나는 경우 수행할 작업 편집기에서 0x80004005 오류 코드를 해결하는 방법을 알려줍니다. Mar 21, 2024 pm 09:17 PM

0x80004005 오류 코드가 나타나는 경우 수행할 작업 편집기에서 0x80004005 오류 코드를 해결하는 방법을 알려줍니다.

hiberfil.sys 파일이란 무엇입니까? hiberfil.sys를 삭제할 수 있나요? hiberfil.sys 파일이란 무엇입니까? hiberfil.sys를 삭제할 수 있나요? Mar 15, 2024 am 09:49 AM

hiberfil.sys 파일이란 무엇입니까? hiberfil.sys를 삭제할 수 있나요?

메이투안 주소는 어디에서 변경할 수 있나요? Meituan 주소 수정 튜토리얼! 메이투안 주소는 어디에서 변경할 수 있나요? Meituan 주소 수정 튜토리얼! Mar 15, 2024 pm 04:07 PM

메이투안 주소는 어디에서 변경할 수 있나요? Meituan 주소 수정 튜토리얼!

C++를 사용하여 HTTP 스트리밍을 구현하는 방법은 무엇입니까? C++를 사용하여 HTTP 스트리밍을 구현하는 방법은 무엇입니까? May 31, 2024 am 11:06 AM

C++를 사용하여 HTTP 스트리밍을 구현하는 방법은 무엇입니까?

MySQL에서 .ibd 파일의 역할과 관련 주의사항에 대한 자세한 설명 MySQL에서 .ibd 파일의 역할과 관련 주의사항에 대한 자세한 설명 Mar 15, 2024 am 08:00 AM

MySQL에서 .ibd 파일의 역할과 관련 주의사항에 대한 자세한 설명

Linux '.a' 파일 생성 및 실행 Linux '.a' 파일 생성 및 실행 Mar 20, 2024 pm 04:46 PM

Linux '.a' 파일 생성 및 실행

Golang이 바이너리 파일을 처리할 수 있나요? Golang이 바이너리 파일을 처리할 수 있나요? Mar 20, 2024 pm 04:36 PM

Golang이 바이너리 파일을 처리할 수 있나요?

See all articles