err -x perror() strerror()

perror()原型:
#include <stdio.h>
void perror(const char *s);
其中,perror()的参数s 是用户提供的字符串。当调用perror()时,它输出这个字符串,后面跟着一个冒号和空格,然后是基于当前errno的值进行的错误类型描述。

strerror()原型:
#include <string.h>
char * strerror(int errnum);
这个函数将errno的值作为参数,并返回一个描述错误的字符串

 1 #include<stdio.h>
 2 #include <string.h>
 3 #include <errno.h>
 4
 5 int main(int argc,char **argv)
 6 {
 7 char path[]="./first.c";
 8 char newpath[] = "./second.c";
 9 char newpathnot[] = "./gong/suo.c";
10 extern int errno;
11
12 if( rename(path,newpathnot) == 0)
13 {
14 printf("the file %s was moved to %s.",path,newpathnot);
15 }
16 else
17 {
18 printf("Can‘t move the file %s.\n",path);
19 printf("errno:%d\n",errno);
20 printf("ERR:%s\n",strerror(errno));
21 perror("Err");
22 }
23
24 if(rename(path,newpath) == 0)
25 printf("the file %s was moved to %s.\n",path,newpath);
26 else
27 {
28 printf("Can‘t move the file %s.\n",path);
29 printf("errno:%d\n",errno);
30 printf("ERR:%s\n",strerror(errno));
31 }
32 return 0;
33 }
34
35
36 gcc rename.c -o rename
37 ./rename
38
39 Can‘t move the file ./first.c.
40 errno:2
41 ERR:No such file or directory
42 Err: No such file or directory
43 the file ./first.c was moved to ./second.c

strerror()方法与perror()的用法十分相似。

先谈谈perror()的用法,这个方法用于将上一条语句(方法)执行后的错误打印到标准输出上。一般情况下(没有使用重定向的话),就是输出到控制台上。但是,如果我需要了解另外一个进程的某一个方法执行的错误,或者更briefly,我就希望将错误打印到一个文件里面,perror()就不太合适了!为了实现我刚刚说到的要求,我们首先要将错误放到一个字符串里面。这个时候,strerror()就合适了!
strerror(errno)
首先,系统会根据上一条语句的执行错误情况,将errno赋值.。关于这点,我们首先明白两点。第一,errno是一个系统变量,是不需要我们赋值或者声明的。第二,errno是一个int类型的变量,而且其中的值对应一种特定错误类型然后,关于streorror()本身,可以这么理解。顾名思义,streorror=string+error,就是将errno值翻译成描述错误类型的string语句!

时间: 2024-08-16 06:35:38

err -x perror() strerror()的相关文章

perror strerror使用方法

1. 简介 很多系统函数在错误返回时将错误原因记录在libc定义的全局变量errno中,每种错误原因对应一个错误码. errno在头文件errno.h中声明,是一个整型变量,所有错误码都是正整数.然后我们可以用perror或 strerror函数将errno解释成字符串 perror #include <stdio.h> void perror(const char *s); 基于errno的当前值,在标准出错上产生一条出错信息,然后返回.它首先输出由msg指向的字符串,然后是一个冒号,一个空

Linux/Unix C编程之的perror函数,strerror函数,errno

#include <stdio.h> // void perror(const char *msg); #include <string.h> // char *strerror(int errnum); #include <errno.h> //errno ? errno 是错误代码,在 errno.h头文件中: perror是错误输出函数,输出格式为:msg:errno对应的错误信息(加上一个换行符): strerror?是通过参数 errnum (就是errno)

strerror 函数

收藏 75 23 strerror编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 通过标准错误的标号,获得错误的描述字符串 ,将单纯的错误标号转为字符串描述,方便用户查找错误. 外文名 strerror 语    言 C语言 参    数 错误标号(即error) 返回值 描述字符串(char *) 目录 1函数名 2函数作用 3头文件 4函数原型 5举例 1函数名编辑 strerror, _strerror, _wcserror, __wcserror 2函数作

TCP随笔

目录 前言 正文 time_wait和rst fin与连接关闭 nagel和ack延迟算法 滑动窗口与拥塞控制 文末 总结 测试代码 前言 网上已经有大量关于tcp的文章,感觉作为一名技术人员,不写一篇tcp相关的文章,对职业生涯来说是一种遗憾,但是又不想单纯造一篇轮子,带着这种矛盾的心态,一拖再拖,最后还是造了一篇轮子.本文不适合完全的小白,适合有一定开发经验的初级开发. 文中一,二两节主要对rst(异常情况)及fin(关闭信号)及相关的一些socket操作,以及各种情况时,调用相关的函数会得

epoll 的两种用法

struct epoll_event 的data成员是一个union类型 里面要么就是fd,要么就是ptr 最好不要一会用ptr一会用fd 最好坚持从头到尾的使用一个 第一种只是使用epoll_event.data.fd #include <stdlib.h>#include <netinet/in.h>#include <arpa/inet.h>#include <stdio.h>#include <unistd.h>#include <

Linux/UNIX线程控制

线程控制 线程属性 调用pthread_create函数的例子中,传入的参数都是空指针,而不是指向pthread_attr_t结果的指针.可以用pthread_attr_t结构修改线程默认属性,并把这些属性与创建的线程联系起来.可以使用pthread_attr_init函数初始化pthreaad_attr_t结构.调用pthread_attr_init以后,pthread_attr_t结构所包含的内容就是操作系统实现支持的线程所有属性的默认值.如果要修改其中个别属性的值,需要调用其他的函数.pt

Linux多播问题(No such device)解决方法

多播的测试代码如下: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <string.h> #include <errno.h> #i

tcgetattr函数与tcsetattr函数控制终端

6.4.4  使用tcgetattr函数与tcsetattr函数控制终端 为了便于通过程序来获得和修改终端参数,Linux还提供了tcgetattr函数和tcsetattr函数.tcgetattr用于获取终端的相关参数,而tcsetattr函数用于设置终端参数.这两个函数的具体信息如表6.2所示. 表6.2   tcgetattr函数和tcsetattr函数 头文件 <termios.h> <unistd.h> 函数形式 int tcgetattr(int fd, struct t

OpenCL 操作context

本程序主要测试: context = clCreateContext(NULL, 1, &device, NULL, NULL, &err); 创建一个context clRetainContext(context);//Context的reference +1 clReleaseContext(context);//Context的reference -1 #include <stdio.h> #include <stdlib.h> #include <st