perror()函数的使用

函数定义:

  void perror(const char *s); perror ("open_port");

包含头文件(不可以掉了这个头文件):

#include <stdio.h>//包含perror的头文件

函数说明

  perror ( )用 来 将 上 一 个 函 数 发 生 错 误 的 原 因 输 出 到 标 准 设备 (stderr) 。参数 s 所指的字符串会先打印出,后面再加上错误原因字符串。此错误原因依照全局变量error 的值来决定要输出的字符串。在库函数中有个error变量,每个error值对应着以字符串表示的错误类型。当你调用"某些"函数出错时,该函数已经重新设置了error的值。perror函数只是将你输入的一些信息和现在的error所对应的错误一起输出。

举例:

#include <stdio.h>//包含perror的头文件
#include <process.h>
#include <direct.h>//包含mkdir、rmdir的头文件
#include <iostream>
using namespace std;
int main(void)
{
	int status;
	system("cls");//清屏

	status = mkdir("cam");//创建文件夹,创建成功则返回0,否则返回-1
	cout << status << endl;
	(!status) ? (printf("Directory created\n")) : (perror("Unable to create directory\n"));
	//!status不等于0为真,结果为第一个,否则为第二个

	system("pause");//屏幕暂停

	system("dir"); /*显示创建后当前文件夹下的文件信息*/
	status = rmdir("cam"); /*删除创建的文件夹book*/
	(!status) ? (printf("Directory deleted\n")) :(perror("Unable to delete directory"));
	system("pause");
	return 0;
}


运行结果:


如果我们的文件夹已经存在,仍然使用mkdir函数创建文件夹时,会发生什么呢?(此时perror函数就会发挥作用)


因为cam文件夹已经存在,所以mkdir返回-1,perror除了输出:Unable to create directory,后面紧跟着具体原因:

File exists

  • 19
    点赞
  • 109
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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); }
C函数库手册,按照函数功能来分类 分类函数,所在函数库为ctype.h int isalpha(int ch) 若ch 是字母('A'-'Z','a'-'z')返回非0 值,否则返回0 int isalnum(int ch) 若ch 是字母('A'-'Z','a'-'z')或数字('0'-'9')返回非0 值,否则返回0 ...... 数学函数,所在函数库为math.h、stdlib.h、string.h、float.h int abs(int i) 返回整型参数i 的绝对值 double cabs(struct complex znum) 返回复数znum 的绝对值 double fabs(double x) 返回双精度参数x 的绝对值 ...... 目录函数,所在函数库为dir.h、dos.h int chdir(char *path) 使指定的目录path(如:"C:\\WPS")变成当前的工作目录,成 功返回0 int findfirst(char *pathname,struct ffblk *ffblk,int attrib)查找指定的文件,成功 返回0 ...... 进程函数,所在函数库为stdlib.h、process.h void abort() 此函数通过调用具有出口代码3 的_exit 写一个终止信息于stderr,并异常终止程序。无返回值 int exec…装入和运行其它程序 ...... 转换子程序,函数库为math.h、stdlib.h、ctype.h、float.h char *ecvt(double value,int ndigit,int *decpt,int *sign)将浮点数value 转换成字符串并返回该字符串 char *fcvt(double value,int ndigit,int *decpt,int *sign)将浮点数value 转换成字符串并返回该字符串 ...... 诊断函数,所在函数库为assert.h、math.h void assert(int test) 一个扩展成if 语句那样的宏,如果test 测试失败,就显示一个信息并异常终止程序,无返回值 void perror(char *string) 本函数将显示最近一次的错误信息,格式如下:字符串string:错误信息 ...... 输入输出子程序,函数库为io.h、conio.h、stat.h、dos.h、stdio.h、signal.h int kbhit() 本函数返回最近所敲的按键 int fgetchar() 从控制台(键盘)读一个字符,显示在屏幕上 ...... 接口子程序,所在函数库为:dos.h、bios.h unsigned sleep(unsigned seconds)暂停seconds 微秒(百分之一秒) int unlink(char *filename)删除文件filename unsigned FP_OFF(void far *farptr)本函数用来取远指针farptr 的偏移量 ...... 存贮分配子程序,所在函数库为dos.h、alloc.h、malloc.h、stdlib.h、process.h int allocmem(unsigned size,unsigned *seg)利用DOS 分配空闲的内存,size 为分配内存大小,seg 为分配后的内存指针 int freemem(unsigned seg)释放先前由allocmem 分配的内存,seg 为指定的内存指针 ...... 操作函数,所在函数库为string.h、mem.h mem…操作存贮数组 ...... ......

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值