文件IO(Linux文件IO,目录操作函数)
前言
本文介绍Linux系统下自带的文件IO的函数。
Linux文件IO相关函数
open函数
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
//调用实例
//只写的方式打开当前路径文件demo.c 没有创建 有清空
int fd = open("./demo.c", O_WRONLY|O_TRUNC|O_CREAT, 0666); 
功能:打开文件
        参数1:要打开文件的路径
         参数2:打开文件方式
         参数3:文件权限
| O_RDONLY | read only 只读 | 
| O_WRONLY | write only 只写 | 
| O_RDWR | 可读可写 | 
| O_TRUNC | 清空 | 
| O_APPEND | 追加 | 
| O_CREAT | 创建 | 
返回值:非负的文件描述符,没打开一个文件,系统就会在内核空间开辟一块内存。会分配一个非负文件描述符作为每个文件的唯一标识。 成功,int fd 非负的数;失败,-1。
文件权限掩码(umask)
在Linux中,文件权限掩码(umask)是一个权限掩码,用于限制新建文件的默认权限。目的是提高系统安全性,通过限制新建文件的默认权限,确保用户不会意外赋予其他用户过多的权限。

新创建的文件默认权限为 666(rw-rw-rw-)
 新创建的目录默认权限为 777(rwxrwxrwx)
0        代表不掩盖(允许设置该权限)
 1        代表掩盖执行权限
 2        代表掩盖写入权限
 3        代表掩盖读取权限 

close函数
#include <unistd.h>
int close(int fd); 
        功能:关闭文件
         参数:open函数的返回值,打开文件的唯一标识
         返回值:成功,0;失败,-1。 
read函数
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
//调用案例
char buf[128] = {0};    //存储数据
read(fd, buf, sizeof(buf)); 
功能:按照字节来读取文件内容
        参数1:open的返回值,打开文件的唯一标识
         参数2:读取内容存放的首地址
         参数3:能读取的最多字节数
返回值:成功,实际读取的字节数;失败,-1
write函数
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
//调用案例
char buf[128] = "hello world";
write(fd, buf, strlen(buf)); 
功能:按照字节写入内容
        参数1:open的返回值,打开文件的唯一标识
         参数2:要写入内容存放的首地址
         参数3:最多要写入的字节数
返回值:成功,实际写入的字节数;失败,-1。
lseek函数
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
typedef long off_t;
//调用实例
long res = lseek(fd,100,SEEK_SET); //相对文件起始位置向后移动100个字符
lseek(fd,-100,SEEK_END); //相对于文件尾位置向前移动100个字符 
功能:移动文件光标指针的同时,还能获取文件光标指针的位置(兼具fseek和ftell函数的功能)
        参数1:open的返回值,打开文件的唯一标识
         参数2:偏移量,正数向后,负数向前
        参数3:基准位
                 SEEK_SET        文件头
                 SEEK_END        文件尾
                 SEEK_CUR        当前位置 
返回值:成功,返回文件指针所在位置;失败,-1
统计一个文件有多少个字节的方式(3种)
//1.fgetc一个字符一个字符读 然后计数
while((ch = fgetc(fp))!= EOF)count++;
//2.fseek移动文件指针位置到文件尾 再用ftell获取文件指针位置
fseek(fp,0,SEEK_END);
long size = ftell(fp);
//3.lseek移动文件指针位置到末尾获取文件指针位置即可
long size = lseek(fd,0,SEEK_END); 
三个文件描述符
        标准输入:0
         标准输出:1
         标准错误:2
目录操作函数
stat函数
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *pathname, struct stat *statbuf);struct stat {dev_t     st_dev;         /* ID of device containing file */ino_t     st_ino;         /* Inode number */mode_t    st_mode;        /* File type and mode */nlink_t   st_nlink;       /* Number of hard links */uid_t     st_uid;         /* User ID of owner */gid_t     st_gid;         /* Group ID of owner */dev_t     st_rdev;        /* Device ID (if special file) */off_t     st_size;        /* Total size, in bytes */blksize_t st_blksize;     /* Block size for filesystem I/O */blkcnt_t  st_blocks;      /* Number of 512B blocks allocated */
}; 
功能:获取文件属性
        参数1:获取文件的路径
         参数2:用于存放文件属性结构体的首地址
返回值:成功,0;失败,-1
opendir函数
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name); 
        功能:打开目录
         参数:打开文件路径
         返回值:成功,DIR* 非空的打开目录文件的标识,目录流指针;失败,NULL
readdir函数
#include <dirent.h>
struct dirent *readdir(DIR *dirp);struct dirent
{long d_ino;                /* inode number 索引节点号 */off_t d_off;               /* offset to this dirent 在目录文件中的偏移 */unsigned short d_reclen;   /* length of this d_name 文件名长 */unsigned char d_type;      /* the type of d_name 文件类型 */char d_name[NAME_MAX + 1]; /* file name (null-terminated) 文件名,最长255字符 */
} 
        功能:读取目录,每次读取一个文件
         参数:opendir的返回值,目录流指针,目录文件的标识
         返回值:成功,struct dirent * ,用来保存从目录中读取文件的内容;失败,NULL,当目录文件读取到尾巴。
closedir函数
#include <sys/types.h>
#include <dirent.h>
int closedir(DIR *dirp); 
        功能:关闭目录文件
         参数:opendir的返回值
         返回值:成功,0;失败,-1 
案例
#include <dirent.h>
#include <stdio.h>
int main(int argc, const char *argv[])
{// 打开目录DIR *dp = opendir("./Netdisk");if (NULL == dp){perror("opendir failed");return -1;}struct dirent *em = NULL;// 读目录while (1){em = readdir(dp); // 调用一次读取出一个文件if (em == NULL)break; // 读取文件完毕结束循环// 将隐藏文件不打印 隐藏文件的特点是以.开头if (em->d_name[0] == '.')continue;printf("%s ", em->d_name);}printf("\n");// 关闭目录closedir(dp);return 0;
}
 

