Linux入门基础操作十二(linux下的IO函数)

Linux入门基础操作(ubuntu20.04)(十二)

系统中常用的IO函数(open):

man +章节 +命令:查看相应命令的man文档
在这里插入图片描述
在这里插入图片描述

open函数中的errno是一个全局变量
查看errno第1-34个基础错误定义:
vi /usr/include/asm-generic/errno-bash.h
在这里插入图片描述
在这里插入图片描述
查看errno第35-133个错误定义:
vi /usr/include/asm-generic/errno.h
每个errno值对应着以字符串表示的错误类型
当调用"某些"函数出错时,该函数会重新设置errno的值
查看errno.h文件文档:
vi /usr/include/erron.h
在这里插入图片描述
在这里插入图片描述

perror(全局变量):
头文件:stdio.h
函数定义:*void perror (const char s)
函数说明:

  1. 用来将上一个函数发生错误的原因输出到标准设备
  2. 函数s所指的字符会先打印出,后面再加上错误原因字符串
  3. 此错误原因依照全局变量errno的值来决定要输出的字符串

任何标准C库函数都能对其进行修改(Linux系统函数更可以)

创建MyIO文件夹,创建myopen.c文件:
在这里插入图片描述编辑myopen.c文件内容:

#include <sys/types.h>  //open
#include <sys/stat.h>   //open
#include <fcntl.h>      //open
#include <unistd.h>     //close
#include <stdlib.h>     //exit
#include <stdio.h>      //perror

int main()
{
int fd;
//打开已经存在的文件
fd = open("hello.c",O_RDWR);
//光标放在函数处,切换到命令模式,shift+k打开函数所在的man文档
if (fd == -1)
{
perror("open file");
exit(1);
}

//创建新文件
fd = open("myhello",O_RDWR | O_CREAT,0777);
if (fd == -1)
{
perror("open file");
exit(1);
}
printf("fd = %d\n",fd);
//关闭文件
int ret = close(fd);
printf("ret = %d\n",ret);

}

运行后结果:
在这里插入图片描述
生成的myhello文件权限并不是777而是775,原因是存在掩码umask(可自己设定:umask +数值),本系统默认为0002.

查看文件是否存在(myopen1.c):

#include <sys/types.h>  //open
#include <sys/stat.h>   //open
#include <fcntl.h>      //open
#include <unistd.h>     //close
#include <stdlib.h>     //exit
#include <stdio.h>      //perror

int main()
{
int fd;
//查询文件是否存在
fd = open("myhello",O_RDWR | O_CREAT | O_EXCL,0777);
if (fd == -1)
{
perror("open file");
exit(1);
}
printf("fd = %d\n",fd);
//关闭文件
int ret = close(fd);
printf("ret = %d\n",ret);
}


终端命令:
在这里插入图片描述
将文件截断为0(删除文件内容)(myopen2.c):

#include <sys/types.h>  //open
#include <sys/stat.h>   //open
#include <fcntl.h>      //open
#include <unistd.h>     //close
#include <stdlib.h>     //exit
#include <stdio.h>      //perror

int main()
{
int fd;
//将文件截断为0
fd = open("myhello",O_RDWR | O_TRUNC);
if (fd == -1)
{
perror("open file");
exit(1);
}
printf("fd = %d\n",fd);
//关闭文件
int ret = close(fd);
printf("ret = %d\n",ret);
}


终端命令:
在这里插入图片描述
本来myhello文件中有内容,执行过后myhello文件中的内容被删除

系统中常用的IO函数(read和write):

返回值:
-1:读文件失败
0:文件读完了
大于0:读取的字节数
编辑read_write.c文件:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main()
{
//打开一个已经存在的文件
int fd = open("123.txt",O_RDONLY);
if (fd == -1)
{
perror("open file");
exit(1);
}
//创建一个新文件 --写操作
int fd1 = open("newfile",O_CREAT | O_WRONLY,0777);
if (fd1 == -1)
{
perror("open1 file");
exit(1);
}
// read file
char buf[2048] = {0};
int count = read(fd,buf,sizeof(buf));
if (count == -1)
{
perror("read file");
exit(1);
}
while (count)
{
//将读出的数据写入到另一个文件中
int ret = write(fd1,buf,count);
printf("write bytes %d\n",ret);
// continue read file
count = read(fd,buf,sizeof(buf));
}
// close file
close(fd);
close(fd1);
}

终端命令运行:
在这里插入图片描述
将123.txt文件内容读取并重新写入newfile文件中:

在这里插入图片描述

系统中常用的IO函数(lseek):

lseek:获取文件的长度和指针.
编辑lseek.c文件:

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
int fd = open("aa",O_RDWR);
if (fd == -1)
{
perror("open file");
exit(1);
}
//获取文件长度
int ret = lseek(fd,0,SEEK_END);
printf("file length = %d\n",ret);

//文件拓展
ret = lseek(fd,2000,SEEK_END);
printf("return value %d\n",ret);

//实现文件拓展,需要在最后做一次写操作
write(fd,"a",1);

close(fd);
return(0);
}

终端命令运行:
在这里插入图片描述
获取aa文件的长度,并将指针移到末尾,拓展文件2000个字符
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值