linux的文件 I O操作_实现linux文件i o控制,包含文件i o的读写、定位,读取、写入锁

2. 读写文件

数据块读写:


#include <stdio.h>
size_t fread(void \*ptr, size_t size, size_t nmemb, FILE \*stream);
size_t fwrite(void \*ptr, size_t size, size_t nmemb, FILE \*stream);

fread 从文件流 stream 中读取 nmemb 个元素, 写到 ptr 指向的内存中, 每个元素的大小为 size 个字节。
fwrite 从 ptr 指向的内存中读取 nmemb 个元素, 写到文件流 stream 中, 每个元素 size 个字节。所有的文件读写函数都从文件的当前读写点开始读写, 读写完以后, 当前读写点自动往后移动size*nmemb 个字节。
整块 copy, 速度较快, 但是是二进制操作。

格式化读写


printf、scanf、
int fprintf(FILE *stream, const char *format, ...); 重点
sprintf(buf,”the string is;%s”,str); 重点
int sscanf(char *str, const char *format, …); 重点

fprintf 将格式化后的字符串写入到文件流 stream 中
sprintf 将格式化后的字符串写入到字符串 str 中

单个字符读写

#include <stdio.h>
int fgetc(FILE \*stream);
int fputc(int c, FILE \*stream);
int getc(FILE \*stream);  等同于 fgetc(FILE* stream)
int putc(int c, FILE \*stream);  等同于 fputc(int c, FILE* stream)
int getchar(void);  等同于 fgetc(stdin);
int putchar(int c);  等同于 fputc(int c, stdout);

字符串读写:

char *fgets(char *s, int size, FILE *stream);
int fputs(const char *s, FILE *stream);
int puts(const char *s);  等同于 fputs(const char *s,stdout);
char *gets(char *s);  等同于 fgets(const char *s, int size,
stdin);

文件定位:
文件定位指读取或设置文件当前读写点, 所有的通过文件指针读写数据的
函数, 都是从文件的当前读写点读写数据的。

#include <stdio.h>
int feof(FILE * stream); //通常的用法为 while(!feof(fp))
int fseek(FILE *stream, long offset, int whence);//设置当前读写点到偏移
whence 长度为 offset 处
long ftell(FILE *stream); //用来获得文件流当前的读写位置
void rewind(FILE *stream); //把文件流的读写位置移至文件开头
fseek(fp, 0, SEEK_SET);
feof 判断是否到达文件末尾的下一个( 注意到达文件末尾之后还会做一次)
fseek 设置当前读写点到偏移 whence 长度为 offset 处, whence 可以是:
    SEEK\_SET (文件开头 0)
    SEEK\_CUR (文件当前位置 1)
    SEEK\_END (文件末尾 2)
ftell 获取当前的读写点
rewind 将文件当前读写点移动到文件头

3. 目录相关操作


改变目录或文件的访问权限

#include <sys/stat.h>
int chmod(const char* path, mode_t mode);//mode 形如: 0777
path 参数指定的文件被修改为具有 mode 参数给出的访问权限。

获取、 改变当前目录:

#include <unistd.h> //头文件
char *getcwd(char *buf, size_t size); //获取当前目录, 相当于 pwd 命令
int chdir(const char *path); //修改当前目录, 即切换目录, 相当于 cd 命令

getcwd()函数,倘若参数 buf 为 NULL, getcwd()会依
参数 size 的大小自动配置内存(使用 malloc()), 如果参数 size 也为 0, 则 getcwd()会依工作目录绝对路径的字符串程度来决定所配置的内存大小
chdir()函数: 用来将当前的工作目录改变成以参数 path 所指的目录

创建和删除目录:
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int mkdir(const char *pathname, mode_t mode); //创建目录,mode 是目录权限
int rmdir(const char *pathname); //删除目录
也可用
 #include <unistd.h>
int unlink(const char *pathname);  来删除硬连接数

获取目录信息

#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name); //打开一个目录
struct dirent *readdir(DIR *dir); //读取目录的一项信息, 并返回该项信息的结构体指针
void rewinddir(DIR *dir); //重新定位到目录文件的头部
void seekdir(DIR *dir,off_t offset);//用来设置目录流目前的读取位置
off_t telldir(DIR *dir); //返回目录流当前的读取位置
int closedir(DIR *dir); //关闭目录文件

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *pathname, struct stat *buf); 获取文件状态

读取目录信息的步骤为:
 1. 用 opendir 函数打开目录;
 2. 使用 readdir 函数迭代读取目录的内容, 如果已经读取到目录末尾, 又想重新开始读, 则可以使用 rewinddir 函数将文件指针重新定位到目录文件的起始位置;
 3. 用 closedir 函数关闭目录

1、opendir()用来打开参数 name 指定的目录, 并返回 DIR*形态的目录流
2、readdir()函数用来读取目录的信息, 并返回一个结构体指针, 该指针保存了目录的相关信息。 有错误发生或者读取到目录文件尾则返回 NULL;
3、seekdir()函数用来设置目录流目前的读取位置, 再调用 readdir()函数时, 便可以从此新位置开始读取。 参数 offset 代表距离目录文件开头的偏移量。
4、telldir()函数用来返回目录流当前的读取位置。

接下来看一下相关结构体的内容:
1、目录信息结构体:
struct dirent
{
    ino_t d_ino; /\* inode number( 此目录进入点的 inode) \*/
    off_t d_off; /\* offset to the next dirent( 目录开头到进入点的位移 \*/
    unsigned short d_reclen; /\* length of this record( 目录名的长度) \*/
    unsigned char d_type; /\* type of file( 所指的文件类型) \*/
    char d_name[256]; /\* filename( 文件名) \*/
};
2、文件状态及相关信息结构体:
struct stat {
    dev_t st_dev; /\*如果是设备, 返回设备表述符, 否则为 0\*/
    ino_t st_ino; /\* i 节点号 \*/
    mode_t st_mode; /\* 文件类型 \*/
    nlink_t st_nlink; /\* 链接数 \*/
    uid_t st_uid; /\* 属主 ID \*/
    gid_t st_gid; /\* 组 ID \*/
    dev_t st_rdev; /\* 设备类型\*/
    off_t st_size; /\* 文件大小, 字节表示 \*/
    blksize_t st_blksize; /\* 块大小\*/
    blkcnt_t st_blocks; /\* 块数 \*/
    time_t st_atime; /\* 最后访问时间\*/
    time_t st_mtime; /\* 最后修改时间\*/
    time_t st_ctime; /\* 最后权限修改时间 \*/
};

接下来举一个实例:  以树形结构的形式输出指定目录下面的所有文件
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
void printdir(char *dir, int depth)
{
    DIR *dp = opendir(dir);
    if(NULL == dp)
    {
        fprintf(stderr,"cannot open directory: %s\n", dir);
        return;
    } 
    chdir(dir);
    struct dirent *entry;
    struct stat statbuf;
    while((entry = readdir(dp)) != NULL)
    {
        stat(entry->d_name,&statbuf);
        if(S_ISDIR(statbuf.st_mode))
        {
            if(strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0)
            continue;
            printf("%\*s%s/\n",depth,"",entry->d_name);
            printdir(entry->d_name,depth+4);
        }
        else
            printf("%\*s%s\n",depth,"",entry->d_name);
            //printf(“%\*s” ,4,” \*” ); 该函数表示输出“\_\_\_\*” ,前面输出 3 个空格。
            //如果是 printf(“%\*s” ,4,“\*\*” );则表示输出“\_\_\*\*” , 前面输出 2 个空格。
    } 
    chdir("..");
    closedir(dp);
}

int main(int argc, char* argv[])
{
    char *topdir, pwd[2]=".";
    if (argc < 2)
        topdir=pwd;
    else
        topdir=argv[1];
    printf("Directory scan of %s\n",topdir);
    printdir(topdir,0);
    printf("done.\n");
    exit(0);
}

4、标准输入/输出流


在进程一开始运行, 就自动打开了三个对应设备的文件, 它们是标准输入、 输出、 错误流, 分别用全局文件指针 stdin、 stdout、 stderr 表示, stdin 具有可读属性, 缺省情况下是指从键盘的读取输入, stdout 和 stderr 具有可写属性, 缺省情况下是指向屏幕输出数据。
示例:

#include <stdio.h>
#include <unistd.h>
int main()
{
    char szBuf[32];
    printf("Input string:"); //向屏幕输出一字符串
    fgets(szBuf,sizeof(szBuf),stdin);//从键盘读入一行字符串
    fprintf(stdout,"The string is:%s",szBuf);//向屏幕输出一行字符串
    return 0;
}

3、基于文件描述符的文件操作(非缓冲)

1、文件描述符简述
内核为每个进程维护一个已打开文件的记录表, 文件描述符是一个较小的正整数( 0—1023) , 它代表记录表的一项, 通过文件描述符和一组基于文件描述符的文件操作函数, 就可以实现对文件的读、 写、 创建、 删除等操作。 常用基于文件描述符的函数有 open( 打开) 、 creat( 创建) 、 close( 关闭) 、 read( 读取) 、 write( 写入) 、 ftruncate( 改变文件大小) 、 lseek( 定位) 、 fsync( 同步) 、 fstat( 获取文件状态) 、 fchmod( 权限) 、 flock( 加锁) 、 fcntl( 控制文件属性) 、
dup( 复制) 、 dup2、 select 和 ioctl。 基于文件描述符的文件操作并非 ANSI C 的函数。

:如果不清楚某个函数的具体实现形式, 可以通过下面的方式查询
man 函数名 查看该函数的帮助。

2、操作函数

**打开、 创建和关闭文件**
open 和 creat 都能打开和创建函数, 原型为

#include <sys/types.h> //头文件
#include <sys/stat.h>
#include <fcntl.h>

int open(const char *pathname, int flags); //文件名 打开方式


![img](https://img-blog.csdnimg.cn/img_convert/49f2dcd2a892ad87adc8c352a703816d.png)
![img](https://img-blog.csdnimg.cn/img_convert/68898e3711267ffe239dc08f474e0d92.png)
![img](https://img-blog.csdnimg.cn/img_convert/9006f3a7599ed8a4b3f55d9cf842730c.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!**
加入社区》https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0
 int flags); //文件名 打开方式


[外链图片转存中...(img-4gLWS0Ik-1725706647481)]
[外链图片转存中...(img-1gFaH420-1725706647482)]
[外链图片转存中...(img-CXyV9DE2-1725706647482)]

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上运维知识点,真正体系化!**
加入社区》https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值