C语言的perror函数用法

perror() 是 C 语言标准库 <stdio.h> 中提供的函数,用于打印与最近的错误代码相关的错误消息。它的原型如下:

void perror(const char *s);

perror() 函数接受一个字符串参数 s,它可以是自定义的错误提示信息。当调用 perror() 函数时,它会根据全局变量 errno 中保存的错误代码,打印出相应的错误消息。通常情况下,perror() 函数将错误消息输出到标准错误流(stderr)。

perror() 函数的使用通常涉及以下几个步骤:

  1. 在可能出错的操作后检查 errno 的值,errno 是一个宏定义的全局变量,用于表示最近的错误代码。
  2. 如果 errno 的值表示出现了错误,调用 perror() 函数并传入相应的错误提示信息。
  3. perror() 函数将根据 errno 的值输出相应的错误消息。

下面是一个简单的示例,展示了 perror() 函数的用法:

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

int main() {
    FILE *file = fopen("nonexistent_file.txt", "r");
    if (file == NULL) {
        perror("Error");
        return 1;
    }

    // 其他代码...

    return 0;
}

在上述示例中,我们试图打开一个不存在的文件。由于文件打开操作可能会产生错误,我们首先检查 fopen() 函数的返回值是否为 NULL。如果为 NULL,则表示打开文件失败,然后我们调用 perror("Error") 打印错误消息,并返回状态码 1 表示异常退出。

perror() 函数将根据全局变量 errno 中的值,在错误消息中包含更具体的信息,例如错误类型和错误描述。

  • 7
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《嵌入式系统设计与应用》 课程设计报告 专 业: 班 级:   姓 名: 学 号: 指导教师:      目 录 一、设计目的 3 二、开发环境 3 三、设计任务及要求 3 四、实现过程 3 4.1用户应用程序设计 3 4.2服务器端程序 3 4.3客户端程序 5 4.3 编译与运行结果 7 五、总结 7 一、设计目的 (1)、熟悉并掌握在Linux开发环境下C语言程序设计及编译方法、嵌入式系统; (2)、掌握嵌入式linux下基础网络编程:socket编程 (3)、独立编写客户机/服务器通信程序; 二、开发环境 (1) 编程环境:在Linux开发环境下设计及编译C语言程序。 (2) 硬件设备:PXA270开发板,PC机。 三、设计任务及要求 设计一套可远程调用求和函数并返回客户端的程序。 四、实现过程 4.1用户应用程序设计 1.程序sum.h int sum(); 2.程序sum.c #include <stdio.h> int sum(){ int i=1,sum=0; while(i<=100){ sum=sum+i; i++; } return sum; } 4.2服务器端程序 /******************************* * 服务器端程序 server.c * ********************************/ #include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <unistd.h> #include <netinet/in.h> #include "sum.h" main() { int sockfd,new_fd,numbytes; struct sockaddr_in my_addr; struct sockaddr_in their_addr; int sin_size,sum; char buff[100] ,temp[100]; sum=sum(); itoa(sum, temp, 10); //服务器端建立TCP协议的socked套接字描述符 if((sockfd = socket(AF_INET,SOCK_STREAM,0))==-1) { perror("socket"); exit(1); } printf("socket success!,sockfd=%d\n",sockfd); //服务器端初始化sockaddr结构体,绑定2323端口 my_addr.sin_family = AF_INET; my_addr.sin_port = htons(2323); my_addr.sin_addr.s_addr = INADDR_ANY; bzero(&(my_addr.sin_zero),8); //绑定套接字描述符sockfd if(bind(sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1) { perror("bind"); exit(1); } printf("bind success!\n"); //创建监听套接字描述符sockfd if(listen(sockfd,10)==-1) { perror("listen"); exit(1); } printf("listening....\n"); //服务器阻塞监听套接字,循环等待客户端程序连接 while(1) { sin_size = sizeof(struct sockaddr_in); //如果建立连接,将产生一个全新的套接字 if((new_fd = accept(sockfd,(struct sockaddr *)&their_addr,&sin_size))==-1) { perror("accept"); exit(1); } //生成一个子进程来完成和客户端的会话,父进程继续监听 if(!fork()) { //读取客户端发来的信息 if((numbytes = recv(new_fd,buff,strlen(buff),0))==-1) { perror("recv"); exit(1); } //将从客户端接收到的信息再发回客户端 if(send(new_fd,temp,strlen(buff),0)==-1) perror("send"); /* 本次通信结束 */ close(new_fd); exit(0); } /* 下一个循环 */ // close(new_fd); } cl
C语言函数及相关知识 函数名: abort 功 能: 异常终止一个进程 用 法: void abort(void); 程序例: #include <stdio.h> #include <stdlib.h> int main(void) { printf("Calling abort()\n"); abort(); return 0; /* This is never reached */ } 函数名: abs 功 能: 求整数的绝对值 用 法: int abs(int i); 程序例: #include <stdio.h> #include <math.h> int main(void) { int number = -1234; printf("number: %d absolute value: %d\n", number, abs(number)); return 0; } 函数名: absread, abswirte 功 能: 绝对磁盘扇区读、写数据 用 法: int absread(int drive, int nsects, int sectno, void *buffer); int abswrite(int drive, int nsects, in tsectno, void *buffer); 程序例: /* absread example */ #include <stdio.h> #include <conio.h> #include <process.h> #include <dos.h> int main(void) { int i, strt, ch_out, sector; char buf[512]; printf("Insert a diskette into drive A and press any key\n"); getch(); sector = 0; if (absread(0, 1, sector, &buf) != 0) { perror("Disk problem"); exit(1); } printf("Read OK\n"); strt = 3; for (i=0; i<80; i++) { ch_out = buf[strt+i]; putchar(ch_out); } printf("\n"); return(0); } 函数名: access 功 能: 确定文件的访问权限 用 法: int access(const char *filename, int amode); 程序例: #include <stdio.h> #include <io.h> int file_exists(char *filename); int main(void) { printf("Does NOTEXIST.FIL exist: %s\n", file_exists("NOTEXISTS.FIL") ? "YES" : "NO"); return 0; } int file_exists(char *filename) { return (access(filename, 0) == 0); } 函数名: acos 功 能: 反余弦函数 用 法: double acos(double x); 程序例: #include <stdio.h> #include <math.h> int main(void) { double result; double x = 0.5; result = acos(x); printf("The arc cosine of %lf is %lf\n", x, result); return 0; } 函数名: allocmem 功 能: 分配DOS存储段 用 法: int allocmem(unsigned size, unsigned *seg); 程序例: #include <dos.h> #include <alloc.h> #include <stdio.h> int main(void) { unsigned int size, segp; int stat; size = 64; /* (64 x 16) = 1024 bytes */ stat = allocmem(size, &segp); if (stat == -1) printf("Allocated memory at segment: %x\n", segp); else printf("Failed: maximum number of paragraphs available is %u\n", stat); return 0; } 函数名: arc 功 能: 画一弧线 用 法: void far arc(int x, int y, int stangle, int endangle, int radius); 程序例: #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> int main(void) { /* request auto detection */ int gdriver = DETECT, gmode, errorcode; int midx, midy; int stangle = 45, endangle = 135; int radius = 100; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, ""); /* read result of initialization */ errorcode = graphresult(); /* an error occurred */ if (errorcode != grOk) { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* terminate with an error code */ } midx = getmaxx() / 2; midy = getmaxy() / 2; setcolor(getmaxcolor()); /* draw arc */ arc(midx, midy, stangle, endangle, radius); /* clean up */ getch(); closegraph(); return 0; } 函数名: asctime 功 能: 转换日期和时间为ASCII码 用 法: char *asctime(const struct tm *tblock); 程序例: #include <stdio.h> #include <string.h> #include <time.h> int main(void) { struct tm t; char str[80]; /* sample loading of tm structure */ t.tm_sec = 1; /* Seconds */ t.tm_min = 30; /* Minutes */ t.tm_hour = 9; /* Hour */ t.tm_mday = 22; /* Day of the Month */ t.tm_mon = 11; /* Month */ t.tm_year = 56; /* Year - does not include century */ t.tm_wday = 4; /* Day of the week */ t.tm_yday = 0; /* Does not show in asctime */ t.tm_isdst = 0; /* Is Daylight SavTime; does not show in asctime */ /* converts structure to null terminated string */ strcpy(str, asctime(&t)); printf("%s\n", str); return 0; } 函数名: asin 功 能: 反正弦函数 用 法: double asin(double x); 程序例: #include <stdio.h> #include <math.h> int main(void) { double result; double x = 0.5; result = asin(x); printf("The arc sin of %lf is %lf\n", x, result); return(0); } 函数名: assert 功 能: 测试一个条件并可能使程序终止 用 法: void assert(int test); 程序例: #include <assert.h> #include <stdio.h> #include <stdlib.h> struct ITEM { int key; int value; }; /* add item to list, make sure list is not null */ void additem(struct ITEM *itemptr) { assert(itemptr != NULL); /* add item to list */ } int main(void) { additem(NULL); return 0; } 函数名: atan 功 能: 反正切函数 用 法: double atan(double x); 程序例: #include <stdio.h> #include <math.h> int main(void) { double result; double x = 0.5; result = atan(x); printf("The arc tangent of %lf is %lf\n", x, result); return(0); } 函数名: atan2 功 能: 计算Y/X的反正切值 用 法: double atan2(double y, double x); 程序例: #include <stdio.h> #include <math.h> int main(void) { double result; double x = 90.0, y = 45.0; result = atan2(y, x); printf("The arc tangent ratio of %lf is %lf\n", (y / x), result); return 0; } 函数名: atexit 功 能: 注册终止函数 用 法: int atexit(atexit_t func); 程序例: #include <stdio.h> #include <stdlib.h> void exit_fn1(void) { printf("Exit function #1 called\n"); } void exit_fn2(void) { printf("Exit function #2 called\n"); } int main(void) { /* post exit function #1 */ atexit(exit_fn1); /* post exit function #2 */ atexit(exit_fn2); return 0; } 函数名: atof 功 能: 把字符串转换成浮点数 用 法: double atof(const char *nptr); 程序例: #include <stdlib.h> #include <stdio.h> int main(void) { float f; char *str = "12345.67"; f = atof(str); printf("string = %s float = %f\n", str, f); return 0; } 函数名: atoi 功 能: 把字符串转换成长整型数 用 法: int atoi(const char *nptr); 程序例: #include <stdlib.h> #include <stdio.h> int main(void) { int n; char *str = "12345.67"; n = atoi(str); printf("string = %s integer = %d\n", str, n); return 0; } 函数名: atol 功 能: 把字符串转换成长整型数 用 法: long atol(const char *nptr); 程序例: #include <stdlib.h> #include <stdio.h> int main(void) { long l; char *str = "98765432"; l = atol(lstr); printf("string = %s integer = %ld\n", str, l); return(0); }
飞思卡尔光电组原理图包括:CCD传感器、按键电路、LCD液晶屏显示电路、电源部分、电机驱动、速度检测、控制器等。 见截图: *********************************************************************************************/ ** 舵机PWM调速频率为300HZ。 ** 电机PWM调速频率为10KHZ。 ** 舵机角度控制精确到1.5度是合适的。 ** 舵机控制一般采用PD控制。(非线性设计) *************************************位置型PD控制 ******************************************** 位置型PD控制 perror[2]=(width[2]-width[4])/2; out_p=Kp*perror[2]/20 //计算比例项,20为控制周期 out_d[2]=Kd*(perror[2]-perror[1])/20 //计算微分项 out[2]=out_p+out_d[2]+center_conter; //由于舵机转角受限,所以注意输出量的限制 perror[1]=perror[2]; //保存上次的误差 out_d[1]=out_d[2]; //保存上次微分环节的值 其中Kp是比例系数;out[2]是舵机的调节量;Kd是微分系数;peeror[2]是本次信号采集计算出的误差;perror[1]是上次信号采集计算出的误差;center_conter是舵机的中心位置的计数值。 非线性PD算法 其中out_p为比例项输出;当-20<perror u65f6作为死区,不进行调节;当-100<perror[2]<-20或者20<perror[2]100时,比例系数Kp2=1.33。这样即实现了系统在小偏差时缓慢的调节,也可以保证当遇到弯道,产生较大的偏差时,舵机能够迅速动作。具体的软件实现如下: if(perror[2]>-20&&perror[2]20&&perror[2]-100&&perror[2]<-20) out_p = perror[2]/2+10; if(perror[2]>=100) out_p = perror[2]*4/3-100; if(perror[2]<=-100) out_p = perror[2]*4/3+100; } 微分部分的非线性由类似方法实现。 ***********************************************PID调节经验************************************ (1) 确定比例系数Kp 确定比例系数Kp 时,首先去掉PID 的积分项和微分项,可以令Ti=0、Td=0,使之成为 纯比例调节。输入设定为系统允许输出最大值的60%~70%,比例系数Kp 由0 开始逐渐增 大,直至系统出现振荡;再反过来,从此时的比例系数Kp 逐渐减小,直至系统振荡消失。 记录此时的比例系数Kp,设定PID 的比例系数Kp 为当前值的60%~70%。 (2) 确定积分时间常数Ti 比例系数Kp 确定之后,设定一个较大的积分时间常数Ti,然后逐渐减小Ti,直至系统出现 振荡,然后再反过来,逐渐增大Ti,直至系统振荡消失。记录此时的Ti,设定PID 的积分 时间常数Ti 为当前值的150%~180%。 (3) 确定微分时间常数Td 微分时间常数Td 一般不用设定,为0 即可,此时PID 调节转换为PI 调节。如果需要设定, 则与确定Kp 的方法相同,取不振荡时其值的30%。 (4) 系统空载、带载联调 对 PID 参数进行微调,直到满足性能要求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值