フォルダーまたはファイルが存在するかどうかを確認する Qt C++ メソッド

方法 1. _access 関数はフォルダーまたはファイルが存在するかどうかを判断します

関数プロトタイプ Check_return _CRTIMP int __cdecl _access( In_z const char * _Filename, In int _AccessMode); 述べる
パラメータファイル名 フォルダー パスまたはファイル パス。例: G:\BaiduNetdiskDownload
パラメータモード 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" );
    }
}

方法 2、qt は PathFileExists を使用してファイル フォルダーが存在するかどうかを判断し、CreateDirectory がフォルダーを作成します

2.1 ファイルフォルダー内に関数プロトタイプがあるかどうかを確認する

BOOL PathFileExistsA([in] LPCSTR pszPath);

2.2 フォルダー関数プロトタイプの作成

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

関数が成功すると、戻り値はゼロ以外になります。

例 1

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

例 2

#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