C language extensions directory and file operations


This section extend some more knowledge directory and file operations, because such knowledge comes to operating time, so after a time on the section operation introduction.

A, access library functions

access function is used to determine the current operating system user access to the file or directory.

Include the header file:

#include <unistd.h>

Function declaration:

int access(const char *pathname, int mode);

Parameter Description:

pathname file or directory name, can be a file or directory of the current directory, you can list the full path.

It requires access mode determination. Unistd.h predefined header file is as follows:

#define R_OK 4     // R_OK 只判断是否有读权限
#define W_OK 2    // W_OK 只判断是否有写权限
#define X_OK 1     // X_OK 判断是否有执行权限
#define F_OK 0     // F_OK 只判断是否存在

return value:

When the conditions are satisfied when the mode is returned pathname 0, -1 is not satisfied.

In the actual development, access function is mainly used to determine whether a file or directory exists.

Two, stat library functions

1, stat structure

struct stat structure to store state information for files and directories, as follows:

struct stat
{
  dev_t st_dev;   // device 文件的设备编号
  ino_t st_ino;   // inode 文件的i-node
  mode_t st_mode;   // protection 文件的类型和存取的权限
  nlink_t st_nlink;   // number of hard links 连到该文件的硬连接数目, 刚建立的文件值为1.
  uid_t st_uid;   // user ID of owner 文件所有者的用户识别码
  gid_t st_gid;   // group ID of owner 文件所有者的组识别码
  dev_t st_rdev;  // device type 若此文件为设备文件, 则为其设备编号
  off_t st_size;  // total size, in bytes 文件大小, 以字节计算
  unsigned long st_blksize;  // blocksize for filesystem I/O 文件系统的I/O 缓冲区大小.
  unsigned long st_blocks;  // number of blocks allocated 占用文件区块的个数, 每一区块大小为512 个字节.
  time_t st_atime;  // time of lastaccess 文件最近一次被存取或被执行的时间, 一般只有在用mknod、 utime、read、write 与tructate 时改变.
  time_t st_mtime;  // time of last modification 文件最后一次被修改的时间, 一般只有在用mknod、 utime 和write 时才会改变
  time_t st_ctime;  // time of last change i-node 最近一次被更改的时间, 此参数会在文件所有者、组、 权限被更改时更新
};

Struct stat structure member variables are more for the programmer to focus on st_mode, st_size and st_mtime members on it. Note st_mtime time is an integer expression requires the programmer to write code conversion format.

Many members st_mode value, or determined using the following two macros.

S_ISREG (st_mode) 是否为一般文件 
S_ISDIR (st_mode) 是否为目录 

2, stat library functions

Include the header file:

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

Function declaration:

int stat(const char *path, struct stat *buf);

stat function to get the specified file or directory path information and saves the information to a structure in buf, successful implementation returns 0, -1 failure.

Example (book145.c)

/*
 * 程序名:book145.c,此程序演示目录和文件的存取权限和状态信息
 * 作者:C语言技术网(www.freecplus.net) 日期:20190525
*/
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>

// 本程序运行要带一个参数,即文件或目录名
int main(int argc,char *argv[])
{
  if (argc != 2)  { printf("请指定目录或文件名。\n"); return -1; }

  if (access(argv[1],F_OK) != 0) { printf("文件或目录%s不存在。\n",argv[1]); return -1; }

  struct stat ststat;

  // 获取文件的状态信息
  if (stat(argv[1],&ststat) != 0) return -1;

  if (S_ISREG(ststat.st_mode)) printf("%s是一个文件。\n",argv[1]);
  if (S_ISDIR(ststat.st_mode)) printf("%s是一个目录。\n",argv[1]);
}

running result

Here Insert Picture Description

Three, utime library functions

utime function is used to modify the file access time, and modification time.

Include the header file:

#include <utime.h>

Function declaration:

int utime(const char *filename, const struct utimbuf *times);

Function Description: utime () is used to modify the inode access time parameter filename file belongs. If the parameter times a null pointer (NULL),
all of the file access time and change the time will be set to the current time. Utimbuf structure is defined as follows:

struct utimbuf
{
  time_t actime;
  time_t modtime;
};

Return Value: returns 0 if executed successfully, a failure -1.

Four, rename library functions

rename function to rename a file or directory, mv command is equivalent to the operating system, the programmer, the program rarely rename a directory, but rename files is frequently used functions.

Include the header file:

#include <stdio.h>

Function declaration:

int rename(const char *oldpath, const char *newpath);

Parameter Description:

Formerly known as oldpath file or directory.

Newpath the new name of the file or directory.

Return value: 0 successful -1- failure.

Five, remove libraries

remove function is used to delete a file or directory, the equivalent of an operating system of the rm command.

Include the header file:

#include <stdio.h>

Function declaration:

int remove(const char *pathname);

Parameter Description:

File or directory name pathname to be deleted.

Return value: 0 successful -1- failure.

Sixth, after-school job

1) continue to enrich your library, create a directory with mkdir when, for example, mkdir ( "/ tmp / aaa / bbb / ccc", 0755) ;, if the parent directory (/ tmp / aaa or / tmp / aaa does not exist / bbb does not exist), it is unable to create / tmp / aaa / bbb / ccc directory, this is not a programmer want, we want to write a function, if the parent directory does not exist, create a parent directory, one level create, declare the function as follows:

int MKDIR(const char *pathname);

2) to prepare a document to obtain a function of time, the following statement:

// 获取文件的时间,即modtime
void FileMTime(const char *in_FullFileName,char *out_ModTime);

out_ModTime format is "yyyy-mm-dd hh24: mi: ss".

3) write a function to obtain the file size, the following statement:

// 获取文件的大小,返回字节数
int FileSize(const char *in_FullFileName);

4) rename function has a shortage, if the target filename directory newpath the parent directory does not exist, the function returns failure, rewrite a new RENAME function, if newpath the parent directory does not exist, create newpath parent directory, and then rename, function declaration as follows:

int RENAME(const char *oldpath, const char *newpath);

5) The second parameter is a function utime structure utimbuf, inconvenient to use in actual development, we hope time format is a string, such as: "2019-02-08 12:05:02", utime override a function, the same function and utime, but the second parameter to time string format function declaration is as follows:

int UTIME(const char *filename, const char *strtime);

Seven, copyright notice

C Language Technology Network original article, reproduced please indicate the source link to the article, the author and original.
Source: C Language Technology Network (www.freecplus.net)
Author: Ethics code Agriculture

If the article typos, or content errors, or other suggestions and comments, please correct me message, thank you very much! ! !

Published an original article · won praise 2 · Views 6738

Guess you like

Origin blog.csdn.net/u010806950/article/details/105043839