21 标准错误

标准输出重定向关闭无数据

下面的代码:

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

int main()
{
    close(1);
    int fd = open("log.txt", O_WRONLY |O_CREAT | O_TRUNC, 0666);
    printf("hello printf\n");  //stdout数据会暂存在缓冲区
 //   fflush(stdout);
    close(fd);  //fd关了,数据无法刷新
     return 0;
}

关闭了1号文件,打开的文件成了1号,本来是行刷新,变为普通文件后就会全缓冲。将文件关了后数据无法刷新
只有主动刷新后才会出现数据
在这里插入图片描述

标准输出和标准错误

标准输出和标准错误正常情况下都是往显示器打印数据,但他们的区别在哪里?
下面的代码:

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

int main()
{
    printf("hello printf 1\n");  //stdout数据会暂存在缓冲区
    fprintf(stdout, "hello fprintf 1\n");
    write(1, "hello write\n", 12);
    std::cout << "cout 1"<<std::endl;   
    
    perror("hello perror 2");
    fprintf(stderr,"hello fprintf 2\n");
    write(2, "hello write 2\n", 12);
    std::cerr << "cerr 2" << std::endl;

     return 0;
}

上面分别向这两个文件输出数据,正常运行没有区别
在这里插入图片描述

但当重定向到文件后就出现了差别
在这里插入图片描述

重定向后只有标准错误的信息打印了出来。1和2都对应的显示器文件,两个是不同的,同一个显示器文件被打开了两次,重定向只是更改了1的位置,2仍然是向显示器打印

一般而言,如果程序运行有问题,使用stderr或者cerr,常规的文本内容,可以用cout,stdout。这样可以将报错和常规输出的分开,单独查看

./test > log.txt 2>err.txt

这样就会将1和2的内容分开重定向到文件

在这里插入图片描述

如果想将两个内容都输出到一个文件可以这样写,将1的内容拷贝给2

./test > log.txt 2>&1

在这里插入图片描述

perror

perror是根据errno的值打印错误信息,上面的默认打印的是成功,errno在它的头文件中

在这里插入图片描述
可以手动修改这个值看看打印变化

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

int main()
{
    errno = 3;
    perror("hello perror 2");

     return 0;
}

在这里插入图片描述

errno会根据设置值打印不同的信息,所以有时候出错系统会设置这个值

自己实现perror

怎么知道每个错误是什么意思,可以参考strerror函数,这函数可以根据不同的错误码显示不同的信息
在这里插入图片描述

void myerror(char* msg)
{
    fprintf(stderr, "%s:%s\n", msg, strerror(errno));
}

int fd = open("log.txt", O_RDONLY);
    if (fd < 0)
    {
        myerror("open");
        return 1;
    }

当这个文件不存在时,会报错
在这里插入图片描述

相关推荐

  1. pyinstaller打包标准流程+错误解决

    2024-04-13 23:56:01       54 阅读
  2. 将Linux 标准输出,错误输出重定向到文件

    2024-04-13 23:56:01       54 阅读
  3. c++学习:iostream输入输出+错误流+标准日志流

    2024-04-13 23:56:01       55 阅读
  4. golang标准错误处理及自定义错误处理示例

    2024-04-13 23:56:01       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-13 23:56:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-13 23:56:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-13 23:56:01       87 阅读
  4. Python语言-面向对象

    2024-04-13 23:56:01       96 阅读

热门阅读

  1. 网络安全工程师必知的100+文件类型

    2024-04-13 23:56:01       29 阅读
  2. jquery 实现倒计时

    2024-04-13 23:56:01       40 阅读
  3. 探索 IT 行业的广阔前景

    2024-04-13 23:56:01       37 阅读
  4. AI是什么?

    2024-04-13 23:56:01       39 阅读
  5. Human Motion Diffusion Model 安装

    2024-04-13 23:56:01       45 阅读
  6. 《程序员的选择逻辑与思考》

    2024-04-13 23:56:01       31 阅读
  7. 4月12日,每日信息差

    2024-04-13 23:56:01       31 阅读