【Linux】缓冲区再理解

一、标准流

c语言程序运行起来就默认打开3个流

标准输出流 - stdout(屏幕打印,这里屏幕就是编译器调试控制台)

标准输入流 - stdin(键盘)

标准错误流 - stderr

这几个流类型都是FILE*

fputs向一般文件(磁盘)或者硬件进行写入

#include <stdio.h>

int main()
{
   
  const char* s = "hello world\n";
  fputs(s, stdout);

  return 0;
}

image-20220427161737029


image-20220427161948608

stderr也是可以输出到屏幕上的

但重定向输出到txt文件中就什么都没有,因为> 是输出重定向的符号

./test > test.txt 2>&1
    //原本1指向显示器,重定向指向文件,再将2拷贝到1里面,2也指向文件了

二、linux系统提供操作系统调用接口

打开

image-20220427172523423

关闭

image-20220427173037908

#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);
       //打开文件的文件名  以读、写等方式打开 以什么模式(权限)    //返回文件描述符,   错误返回-1
int creat(const char *pathname, mode_t mode);

#include <unistd.h>
int close(int fd);

1. open创建文件

open的返回值是操作系统给予的,实际上是操作系统内部一个数组的下标

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


int main()
{
   
  int fd = open("./test.txt", O_WRONLY | O_CREAT, 0644);
  close(fd);

  return 0;
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凛音Rinne

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值