Qt C++ 判断文件夹或者文件是否存在的方法

方法一、 _access函数判断文件夹或者文件是否存在

函数原型 Check_return _CRTIMP int __cdecl _access(In_z const char * _Filename, In int _AccessMode); 备注
参数 filename 文件夹路径或者文件路径。例如G:\BaiduNetdiskDownload
参数 mode 00 判断是否存在
02 判断写入权限
04 判断读取权限
06 判断执行权限

成功返回 0,否则返回 -1。

所属头文件:

#include "io.h"

实例:

// crt_access.c
// compile with: /W1
// This example uses _access to check the file named
// crt_ACCESS.C to see if it exists and if writing is allowed.

#include  <io.h>
#include  <stdio.h>
#include  <stdlib.h>

int main( void )
{
    
    
    // Check for existence.
    if( (_access( "crt_ACCESS.C", 0 )) != -1 )
    {
    
    
        printf_s( "File crt_ACCESS.C exists.\n" );

        // Check for write permission.
        // Assume file is read-only.
        if( (_access( "crt_ACCESS.C", 2 )) == -1 )
            printf_s( "File crt_ACCESS.C does not have write permission.\n" );
    }
}

方法二、qt 使用PathFileExists判断文件文件夹是否存在,CreateDirectory创建文件夹

2.1 判断文件文件夹是否存在函数原型

BOOL PathFileExistsA([in] LPCSTR pszPath);

2.2 创建文件夹函数原型

BOOL CreateDirectory([in] LPCTSTR lpPathName, [in, optional] LPSECURITY_ATTRIBUTES lpSecurityAttributes);

如果该函数成功,则返回值为非零值。

示例一

QString filePath = "G:/BaiduNetdiskDownload";
    if (!PathFileExists(filePath.toStdWString().c_str()))
    {
    
    
		if (!CreateDirectory(filePath.toStdWString().c_str(), NULL))
        {
    
    
            return;//创建目录失败;
        }
    }

示例二

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

OUTPUT
==============
Search for the file path of : C:\TEST\file.txt
The file requested "C:\TEST\file.txt" is a valid file
The return from function is : 1

The file requested "C:\TEST\file.doc" is not a valid file
The return from function is : 0

猜你喜欢

转载自blog.csdn.net/qq_42815643/article/details/129428490