做速卖通要关注的几个网站,东莞做网站怎么样,怎么注册自己的域名,十堰网站建设报价Linux C语言 36-文件处理补充
本节关键字#xff1a;C语言 文件操作#xff0c;文件处理#xff0c;文件创建#xff0c;文件信息#xff0c;文件删除#xff0c;目录创建#xff0c;目录信息#xff0c;目录遍历#xff0c;目录递归遍历 相关C库函数#xff1a;acc…Linux C语言 36-文件处理补充
本节关键字C语言 文件操作文件处理文件创建文件信息文件删除目录创建目录信息目录遍历目录递归遍历 相关C库函数access、mkdir、opendir、chdir、readdir、closedir、rm、stat等
声明时间有限目前仅将相关库函数记录下来后期慢慢完善用法及例程感兴趣的小伙伴可以先关注有更新就会提醒哦~
判断文件是否存在
#include unistd.h
int access(const char *pathname, int mode);创建目录
#include sys/stat.h
#include sys/types.h
int mkdir(const char *pathname, mode_t mode);获取目录的文件描述符
#include sys/types.h
#include dirent.h
int dirfd(DIR *dirp);打开目录
#include sys/types.h
#include dirent.h
DIR *opendir(const char *name);
DIR *fdopendir(int fd);读取/遍历目录
#include dirent.h
struct dirent
{ino_t d_ino; /* inode number */off_t d_off; /* offset to the next dirent */unsigned short d_reclen; /* length of this record */unsigned char d_type; /* type of file; not supportedby all file system types */char d_name[256]; /* filename */
};
int readdir(unsigned int fd, struct old_linux_dirent *dirp, unsigned int count);
struct dirent *readdir(DIR *dirp);
int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);进入子目录
#include unistd.h
int chdir(const char *path);
int fchdir(int fd);关闭目录
#include sys/types.h
#include dirent.h
int closedir(DIR *dirp);删除文件/目录
#include stdio.h
int remove(const char *pathname);目录指针操作
#include sys/types.h
#include dirent.h
void rewinddir(DIR *dirp);#include dirent.h
int scandir(const char *dirp, struct dirent ***namelist,int (*filter)(const struct dirent *),int (*compar)(const struct dirent **, const struct dirent **));
int alphasort(const void *a, const void *b);
int versionsort(const void *s, const void *b);#include dirent.h
void seekdir(DIR *dirp, long offset);#include dirent.h
long telldir(DIR *dirp);例程
#define _SVID_SOURCE
/* print files in current directory in reverse order */
#include dirent.h
int main(void)
{struct dirent **namelist;int n;n scandir(., namelist, 0, alphasort);if (n 0){perror(scandir);}else {while (n--) {printf(%s\n, namelist[n]-d_name);free(namelist[n]);}free(namelist);}return 0;
}获取文件/目录信息
#include sys/types.h
#include sys/stat.h
#include unistd.h
struct stat
{ dev_t st_dev; /* ID of device containing file - 文件所在设备的ID*/ ino_t st_ino; /* inode number - 节点号*/ mode_t st_mode; /* protection - 保护模式?*/ nlink_t st_nlink; /* number of hard links - 链向此文件的连接数(硬连接)*/ uid_t st_uid; /* user ID of owner - 所有者的用户ID*/ gid_t st_gid; /* group ID of owner - 所有者的组ID*/ dev_t st_rdev; /* device ID (if special file) - 设备号针对设备文件*/ off_t st_size; /* total size, in bytes - 文件大小字节为单位*/ blksize_t st_blksize; /* blocksize for filesystem I/O - 系统块的大小*/ blkcnt_t st_blocks; /* number of blocks allocated - 文件所占块数*/ time_t st_atime; /* time of last access - 最后访问时间*/ time_t st_mtime; /* time of last modification -最近修改时间*/ time_t st_ctime; /* time of last status change - 上次状态更改的时间*/
};
int stat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *path, struct stat *buf);S_ISREG(m) is it a regular file?
S_ISDIR(m) directory?
S_ISCHR(m) character device?
S_ISBLK(m) block device?
S_ISFIFO(m) FIFO (named pipe)?
S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.)
S_ISSOCK(m) socket? (Not in POSIX.1-1996.)S_IFMT 0170000 bit mask for the file type bit fields
S_IFSOCK 0140000 socket
S_IFLNK 0120000 symbolic link
S_IFREG 0100000 regular file
S_IFBLK 0060000 block device
S_IFDIR 0040000 directory
S_IFCHR 0020000 character device
S_IFIFO 0010000 FIFO
S_ISUID 0004000 set UID bit
S_ISGID 0002000 set-group-ID bit (see below)
S_ISVTX 0001000 sticky bit (see below)
S_IRWXU 00700 mask for file owner permissions
S_IRUSR 00400 owner has read permission
S_IWUSR 00200 owner has write permission
S_IXUSR 00100 owner has execute permission
S_IRWXG 00070 mask for group permissions
S_IRGRP 00040 group has read permission
S_IWGRP 00020 group has write permission
S_IXGRP 00010 group has execute permission
S_IRWXO 00007 mask for permissions for others (not in group)
S_IROTH 00004 others have read permission
S_IWOTH 00002 others have write permission
S_IXOTH 00001 others have execute permission例程
#include sys/types.h
#include sys/stat.h
#include time.h
#include stdio.h
#include stdlib.h
int main(int argc, char *argv[])
{struct stat sb;if (argc ! 2) {fprintf(stderr, Usage: %s pathname\n, argv[0]);exit(EXIT_FAILURE);}if (stat(argv[1], sb) -1) {perror(stat);exit(EXIT_SUCCESS);}printf(File type: );switch (sb.st_mode S_IFMT) {case S_IFBLK: printf(block device\n); break;case S_IFCHR: printf(character device\n); break;case S_IFDIR: printf(directory\n); break;case S_IFIFO: printf(FIFO/pipe\n); break;case S_IFLNK: printf(symlink\n); break;case S_IFREG: printf(regular file\n); break;case S_IFSOCK: printf(socket\n); break;default: printf(unknown?\n); break;}printf(I-node number: %ld\n, (long) sb.st_ino);printf(Mode: %lo (octal)\n,(unsigned long) sb.st_mode);printf(Link count: %ld\n, (long) sb.st_nlink);printf(Ownership: UID%ld GID%ld\n,(long) sb.st_uid, (long) sb.st_gid);printf(Preferred I/O block size: %ld bytes\n,(long) sb.st_blksize);printf(File size: %lld bytes\n,(long long) sb.st_size);printf(Blocks allocated: %lld\n,(long long) sb.st_blocks);printf(Last status change: %s, ctime(sb.st_ctime));printf(Last file access: %s, ctime(sb.st_atime));printf(Last file modification: %s, ctime(sb.st_mtime));exit(EXIT_SUCCESS);
}读取链接文件
#include unistd.h
ssize_t readlink(const char *path, char *buf, size_t bufsiz);声明时间有限目前仅将相关库函数记录下来后期慢慢完善用法及例程感兴趣的小伙伴可以先关注有更新就会提醒哦~