Create folder and delete folder in c language

Today, when I was writing code, I encountered the operation of creating and deleting folders. After some search experiments, I screened out two methods and marked them.

Method 1 (General)

#include<stdio.h>
#include <io.h>//_access的头文件

int fun(char*filepath)
{
    
    
    int res;
  //filepath为绝对路径
  //如果文件夹不存在
  if(_access(filepath,0)!=0)
    //创建文件夹
    res = mkdir(filepath);// 返回 0 表示创建成功,-1 表示失败
    //remove(filename) 删除文件
  else
    res = 1;
  return res;
}

int main()
{
    
    
    int flag = fun("d:\\45");
    if(flag==-1)
        printf("创建失败\n");
    else if(flag==0)
        printf("创建成功\n");
    else if(flag==1)
        printf("文件已存在\n");
    return 0;
}
Detailed access

Header file: <io.h>

Function prototype: int _access(const char *pathname, int mode);

Parameters: pathname is the file path or directory path, and mode is the access permission (in different systems, it may be redefined with unavailable macro definitions)

Return value: If the file has the specified access permission, the function returns 0; if the file does not exist or cannot access the specified permission, it returns -1.

Remarks: When pathname is a file, the _access function judges whether the file exists, and judges whether the file can be accessed with the mode specified by the mode value. When pathname is a directory, _access only judges whether the specified directory exists. In Windows NT and Windows 2000, all directories have only read and write permissions.

The value and meaning of mode are as follows:

00 - only check if the file exists

02 - write permissions

04 - read permission

06 - read and write permissions

Method 2 (generally used in mfc)

#include <shlwapi.h>
#pragma comment(lib,"shlwapi.lib")

void fun(char*path)
{
    
    
  if (!PathIsDirectory(path))
    ::CreateDirectory(path, NULL); 
    DeleteFile(filename);删除文件
}  
Explain CreateDirectory in detail

The function of CreateDirectory is to create a new directory. If the underlying filesystem supports security descriptions on files and directories, this function applies the specified security description to the new directory.

Function prototype:

BOOL CreateDirectory(
  LPCTSTR lpPathName,
  LPSECURITY_ATTRIBUTES lpSecurityAttributes
);

Parameters:
pPathName: Long pointer to a null-terminated string specifying the path of the directory to be created. There is a default string size limit for paths of MAX_PATH characters. This limitation is related to how this function resolves paths. The length of the string does not exceed MAX_PATH.

lpSecurityAttributes:忽略,一般设置为NULL。

Return value:
non-zero for success, zero for failure. To get more error information, callGetLastErrorfunction.

Note:
This function is not recursive. It can create unique final directories in a path. That is, if the parent or intermediate directories do not exist, the function will fail with the error message ERROR_PATH_NOT_FOUND.
When calling the PathIsDirectory() function, the following header files and library functions need to be included:

#include "shlwapi.h"
#pragma comment(lib,"shlwapi.lib")

Guess you like

Origin blog.csdn.net/qq_44391957/article/details/117106639