strerror()

简介

strerror() 函数能够将错误号 errno 转换成对应的错误消息字符串。

示例

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{

	FILE *fp;

	fp = fopen("no_this_file", "r");
	perror("fopen");

	printf("fopen: %s\n", strerror(errno));

	return EXIT_SUCCESS;
}

输出

fopen: No such file or directory
fopen: No such file or directory

strerror(errno) 输出了和 perror() 相同的错误消息。不过使用 strerror() 可以配合 printf() 定制客制化输出,如:

printf("fopen %s: %s\n", file_name, strerror(errno));

而 preeor() 由于不支持可变参数,有时稍显呆板。

上一篇:手把手,从无到有带你用vue进行项目实战 系列一(搭建框架篇)


下一篇:【已解决】 fatal: unable to access 'https://github.com/ranmocc/ranmocc.github.io.git/': OpenSSL