c语言库函数大全a-b
- 格式:doc
- 大小:53.50 KB
- 文档页数:17
C语言文件操作函数13.1C语言文件1,两种文件存取方式(输入,输出方式)顺序存取直接存取2,数据的两种存放形式文本文件二进制文件13.2文件指针定义文件类型指针变量的一般形式:FILE *指针变量名;例如:FILE *fp1,*fp2;13.3打开文件在使用文件之前,需打开文件.在C里使用fopen函数打开文件.格式为:fopen(文件名,文件使用方式);此函数返回一个指向FILE类型的指针.如:FILE *fp;fp=fopen("file_1","r");如果调用成功,fp就指向file_1,否则返回为NULL,所以为了保证文件的正确使用,要进行测试.采用如下语句:If((fp=fopen("file_1","r"))==NULL){printf("Cannot open this file\n");exit(0);}最常用的文件使用方式及其含义如下:1,"r".为读而打开文本文件.(不存在则出错) 2,"rb".为读而打开二进制文件.3,"w".为写而打开文本文件.(若不存在则新建,反之,则从文件起始位置写,原内容将被覆盖) 4,"wb".为写而打开二进制文件.5,"a".为在文件后面添加数据而打开文本文件.(若不存在,则新建;反之,在原文件后追加)6,"ab".为在文件后面添加数据而打开一个二进制文件.最常用的文件使用方式及其含义如下:7,"r+".为读和写而打开文本文件.(读时,从头开始;在写数据时,新数据只覆盖所占的空间,其后不变) 8,"rb+".为读和写而打开二进制文件.只是在随后的读写时,可以由位置函数设置读和写的起始位置. 9,"w+".首先建立一个新文件,进行写操作,随后可以从头开始读.(若文件存在,原内容将全部消失) 10,"wb+".功能与"w+"同.只是在随后的读写时,可以由位置函数设置读和写的起始位置.最常用的文件使用方式及其含义如下:11,"a+".功能与"a"相同;只是在文件尾部添加新的数据后,可以从头开始读.12,"ab+".功能与"a+"相同;只是在文件尾部添加新数据之后,可以由位置函数设置开始读的起始位置. 13.4关闭文件当文件的读写操作完成之后,使用fclose函数关闭文件.格式如下:fclose(文件指针)如:fclose(fp);13.5调用fgetc和fputc函数进行输入和输出1,调用fputc函数输出一个字符功能是:将字符ch写到文件指针fp所指的文件中去.当输出成功,putc函数返回所输出的字符;否则,返回一个EOF值.EOF是在stdio.h库函数文件中定义的符号常量,其值等于-1.13.5调用fgetc和fputc函数进行输入和输出例如:把从键盘输入的文本按原样输出到名为file_1.dat文件中,用字符@作为键盘输入结束标志. #includeVoid main(){FILE *fpout;char ch;if(fpout=fpopen("file_1","w")==NULL){printf("Cannot open this file!\n");exit(0);}ch=getchar();while(ch!='@'){ fputc(ch,fpout); ch=getchar(); }fclose(fpout);}2.调用fgetc函数输入一个字符功能是:从pf指定的文件中读如一个字符,并把它作为函数值返回.例如:把一个已存在磁盘上的file_1.dat文本文件中的内容,原样输出到终端屏幕上.#includevoid main(){FILE *fpin;char ch;if((fpin=fopen("file_1.dat","r"))==NULL){ printf("Cann't open this file!\n");exit(0);} ch=fgetc(fpin);while (ch!=EOF){ putchar(ch); ch=fgetc(fpin);}fclose(fpin);}13.6判断文件结束函数feofEOF可以作为文本文件的结束标志,但不能作为二进制文件的结束符.feof函数既可以判断二进制文件,又可以判断文本文件.例:编写程序,用于把一个文本文件(源)复制到另一个文件(目的)中,源文件名和目的文件名由命令行输入.命令形式如下:可执行程序名源文件名目的文件名#includevoid filecopy(FILE* ,FILE *);void main(int argc,char *argv[]){FILE *fpin,*fpout;if(argc==3){ fpin=fopen(argv[1],"r");fpout=fopen(argv[2],"w");filecopy(fpin,fpout);fclose(fpin);fclose(fpout);}else if(argc>3)printf("The file names too many!!\n";elseprintf("There are no file names for input or output!!\n );}void filecopy(FILE *fpin,FILE *fpout){char ch;ch=getc(fpin);while(!feof(fpin)){putc(ch,fpout); ch=getc(fpin);}}13.7fscanf函数和fprintf函数1,fscanf函数fscanf只能从文本文件中按格式输入,和scanf函数相似,只不过输入的对象是磁盘上文本文件中的数据.调用形式为:fscanf(文件指针,格式控制字符串,输入项表)例如:fscanf(fp,"%d%d",&a,&b);fscanf(stdin,"%d%d",&a,&b);等价于scanf("%d%d",&a,&b);3.fprintf函数fprintf函数按格式将内存中的数据转换成对应的字符,并以ASCII代码形式输出到文本文件中.Fprintf 函数和printf函数相似,只是将输出的内容按格式存放到磁盘的文本文件中.调用形式如下:fprintf(文件指针,格式控制字符串,输出项表)如:fprintf(fp,"%d %d",x,y);以下语句 fprintf(stdout,"%d %d",x,y)13.8fgets函数和fputs函数1,fgets函数fgets函数用来从文件中读入字符串.调用形式如下: fgets(str,n,fp);函数功能是:从fp所指文件中读入n-1个字符放入str 为起始地址的空间内;如果在未读满n-1个字符时,则遇到换行符或一个EOF结束本次读操作,并已str作为函数值返回. 13.8fgets函数和fputs函数2,fputs函数fput函数把字符串输出到文件中.函数调用形式如下: fputs(str,fp);注意:为了便于读入,在输出字符串时,应当人为的加诸如"\n"这样的字符串.#include <stdio.h>#include <iostream>int main(int argc, char *argv[]){char arr[10] ;char *ap = "hello!" ;FILE *fp ;if ((fp = fopen("hello.txt", "wt+")) == NULL) {printf("error!") ;exit(1) ;}fputs(ap, fp) ;rewind(fp) ; //fgets(arr, 10, fp) ;printf("%s\n", arr) ;fclose(fp) ;return 0 ;}13.9fread函数和fwrite函数例如有如下结构体:struct st{char num[8];float mk[5];}pers[30];以下循环将把这30个元素中的数据输出到fp所指文件中.for(i=0;i<30;i++)fwrite(&pers[i],sizeof(struct st),1,fp);13.9fread函数和fwrite函数以下语句从fp所指的文件中再次将每个学生数据逐个读入到pers数组中.i=0;fread(&pers[i],sizeof(struct st),1,fp);while(!feof(fp)){ i++;fread(&pers[i],sizeof(struct st),1,fp);}13.10文件定位函数1,fseek函数fseek函数用来移动文件位置指针到指定的位置上,接着的读或写操作将从此位置开始.函数的调用形式如下:fseek(pf,offset,origin)pf:文件指针offset:以字节为单位的位移量,为长整形.origin:是起始点,用来指定位移量是以哪个位置为基准的.1,fseek函数位移量的表示方法标识符数字代表的起始点SEEK_SET 0 文件开始SEEK_END 2 文件末尾SEEK_CUR 1 文件当前位置假设pf已指向一个二进制文件,则;fseek(pf,30L,SEEK_SET)fseek(pf,-10L*sizeof(int),SEEK_END)对于文本文件,位移量必须是0;如:fseek(pf,0L,SEEK_SET)fseek(pf,0L,SEEK_END)2. ftell函数ftell函数用以获得文件当前位置指针的位置,函数给出当前位置指针相对于文件开头的字节数.如;long t;t=ftell(pf);当函数调用出错时,函数返回-1L.我们可以通过以下方式来测试一个文件的长度: fseek(fp,0L,SEEK_END);t=ftell(fp);3.rewind函数调用形式为:rewind(pf);函数没有返回值.函数的功能是使文件的位置指针回到文件的开头.13.10文件应用在磁盘上的test.txt文件中放有10个不小于2的正整数,用函数调用方式编写程序.要求实现:1,在被调函数prime中,判断和统计10个整数中的素数以及个数.2,在主函数中将全部素数追加到磁盘文件test.txt的尾部,同时输出到屏幕上.#include#includeInt prime(int a[],int n){int I,j,k=0,flag=0;for(i=0;i { for(j=2;j if(a[i]%j==0){ flag=0; break;}else flag=1;if(flag){a[k]=a[i];k++;}}return k; }void main(){int n,I,a[10];FILE *fp;fp=fopen("test1-2.txt","r+");for(n=0;n<10;n++)fscanf(fp,"%d",&a[n]);n=prime(a,n);fseek(fp,o,2);for(i=0;i {printf("%3d",a[i]);fprintf(fp,"%3d",a[i]);}fclose(fp);}文件操作函数全clearerr(清除文件流的错误旗标)相关函数feof表头文件#include<stdio.h>定义函数void clearerr(FILE * stream);函数说明clearerr()清除参数stream指定的文件流所使用的错误旗标。
常用C语言标准库函数C语言编译系统提供了众多的预定义库函数和宏。
用户在编写程序时,可以直接调用这些库函数和宏。
这里选择了初学者常用的一些库函数,简单介绍了各函数的用法和所在的头文件。
1.测试函数Isalnum原型:int isalnum(int c)功能:测试参数c是否为字母或数字:是则返回非零;否则返回零头文件:ctype.hIsapha原型:int isapha(int c)功能:测试参数c是否为字母:是则返回非零;否则返回零头文件:ctype.hIsascii原型:int isascii(int c)功能:测试参数c是否为ASCII码(0x00~0x7F):是则返回非零;否则返回零头文件:ctype.hIscntrl原型:int iscntrl(int c)功能:测试参数c是否为控制字符(0x00~0x1F、0x7F):是则返回非零;否则返回零头文件:ctype.hIsdigit原型:int isdigit(int c)功能:测试参数c是否为数字:是则返回非零;否则返回零。
头文件:ctype.hIsgraph原型:int isgraph(int c)功能:测试参数c是否为可打印字符(0x21~0x7E):是则返回非零;否则返回零头文件:ctype.hIslower原型:int islower(int c)功能:测试参数c是否为小写字母:是则返回非零;否则返回零 头文件:ctype.hIsprint原型:int isprint(int c)功能:测试参数c是否为可打印字符(含空格符0x20~0x7E):是则返回非零;否则返回零头文件:ctype.hIspunct原型:int ispunct(int c)功能:测试参数c是否为标点符号:是则返回非零;否则返回零 头文件:ctype.hIsupper原型:int isupper(inr c)功能:测试参数c是否为大写字母:是则返回非零;否则返回零Isxdigit原型:int isxdigit(int c)功能:测试参数c是否为十六进制数:是则返回非零;否则返回零2.数学函数abs原型:int abs(int i)功能:返回整数型参数i的绝对值头文件:stdlib.h,math.hacos原型:double acos(double x)功能:返回双精度参数x的反余弦三角函数值头文件:math.hasin原型:double asin(double x)功能:返回双精度参数x的反正弦三角函数值头文件:math.hatan原型:double atan(double x)功能:返回双精度参数的反正切三角函数值头文件:math.hatan2原型:double atan2(double y,double x)功能:返回双精度参数y和x由式y/x所计算的反正切三角函数值 头文件:math.hcabs原型:double cabs(struct complex znum)功能:返回一个双精度数,为计算出复数znum的绝对值。
Keil C51库函数参考C51强大功能及其高效率的重要体现之一在于其丰富的可直接调用的库函数,多使用库函数使程序代码简单,结构清晰,易于调试和维护,下面介绍C51的库函数系统。
第一节本征库函数(intrinsic routin e s)和非本征证库函数C51提供的本征函数是指编译时直接将固定的代码插入当前行,而不是用AC ALL和L CALL语句来实现,这样就大大提供了函数访问的效率,而非本征函数则必须由A CALL及LCALL调用。
C51的本征库函数只有9个,数目虽少,但都非常有用,列如下:_crol_,_cror_:将char型变量循环向左(右)移动指定位数后返回_iror_,_irol_:将int型变量循环向左(右)移动指定位数后返回_lrol_,_lror_:将long型变量循环向左(右)移动指定位数后返回_nop_:相当于插入N OP_testb it_:相当于JBC bitvar测试该位变量并跳转同时清除。
_chkfl oat_:测试并返回源点数状态。
使用时,必须包含#incluc le <intrin s.h>一行。
如不说明,下面谈到的库函数均指非本征库函数。
第二节几类重要库函数1. 专用寄存器i nclud e文件例如8031、8051均为REG51.h其中包括了所有8051的SFR及其位定义,一般系统都必须包括本文件。
2. 绝对地址in clude文件abs acc.h该文件中实际只定义了几个宏,以确定各存储空间的绝对地址。
3. 动态内存分配函数,位于stdl ib.h中4. 缓冲区处理函数位于“string.h”中其中包括拷贝比较移动等函数如:memccp y memchr memcmp memcpy memmov e memset这样很方便地对缓冲区进行处理。
功能: 异常终止一个进程用法: void abort(void)函数名: abs功能: 求整数的绝对值用法: int abs(int i)函数名: absread, abswirte功能: 绝对磁盘扇区读、写数据用法: int absread(int drive, int nsects, int sectno, void *buffer) int abswrite(int drive, int nsects, in tsectno, void *buffer函数名: access功能: 确定文件的访问权限用法: int access(const char *filename, int amode)函数名: acos功能:反余弦函数用法: double acos(double x)函数名: allocmem功能: 分配DOS存储段用法:int allocmem(unsigned size, unsigned *seg)函数名: arc功能: 画一弧线用法:void far arc(int x, int y, int stangle, int endangle, int radius)函数名: asctime功能: 转换日期和时间为ASCII码用法:char *asctime(const struct tm *tblock)函数名: asin功能:反正弦函数用法: double asin(double x)函数名: assert功能: 测试一个条件并可能使程序终止用法:void assert(int test)函数名: atan功能: 反正切函数用法: double atan(double x)功能: 计算Y/X的反正切值用法: double atan2(double y, double x)函数名:atexit功能: 注册终止函数用法: int atexit(atexit_t func)函数名: atof功能: 把字符串转换成浮点数用法:double atof(const char *nptr)函数名: atoi功能: 把字符串转换成长整型数用法: int atoi(const char *nptr)函数名: atol功能: 把字符串转换成长整型数用法: long atol(const char *nptr)函数名: bar功能: 画一个二维条形图用法: void far bar(int left, int top, int right, int bottom)函数名: bar3d功能: 画一个三维条形图用法:void far bar3d(int left, int top, int right, int bottom,int depth, int topflag)函数名: bdos功能: DOS系统调用用法: int bdos(int dosfun, unsigned dosdx, unsigned dosal)函数名:bdosptr功能:DOS系统调用用法: int bdosptr(int dosfun, void *argument, unsigned dosal)函数名:bioscom功能: 串行I/O通信用法:int bioscom(int cmd, char abyte, int port)函数名:biosdisk功能: 软硬盘I/O用法:int biosdisk(int cmd, int drive, int head, int track, int sectorint nsects, void *buffer)函数名:biosequip功能: 检查设备用法:int biosequip(void)函数名:bioskey功能: 直接使用BIOS服务的键盘接口用法:int bioskey(int cmd)函数名:biosmemory功能: 返回存储块大小用法:int biosmemory(void)函数名:biosprint功能: 直接使用BIOS服务的打印机I/O用法:int biosprint(int cmd, int byte, int port)函数名:biostime功能: 读取或设置BIOS时间用法: long biostime(int cmd, long newtime)函数名: brk功能: 改变数据段空间分配用法:int brk(void *endds)函数名:bsearch功能: 二分法搜索用法:void *bsearch(const void *key, const void *base, size_t *nelem, size_t width, int(*fcmp)(const void *, const *))函数名: cabs功能: 计算复数的绝对值用法: double cabs(struct complex z);函数名:calloc功能:分配主存储器用法:void *calloc(size_t nelem, size_t elsize);函数名: ceil功能: 向上舍入用法: double ceil(double x);函数名: cgets功能: 从控制台读字符串用法: char *cgets(char *str)函数名:chdir功能: 改变工作目录用法: int chdir(const char *path);函数名:_chmod, chmod功能: 改变文件的访问方式用法: int chmod(const char *filename, int permiss);函数名:chsize功能: 改变文件大小用法: int chsize(int handle, long size);函数名: circle功能: 在给定半径以(x, y)为圆心画圆用法: void far circle(int x, int y, int radius);函数名: cleardevice功能: 清除图形屏幕用法: void far cleardevice(void);函数名:clearerr功能: 复位错误标志用法:void clearerr(FILE *stream);函数名: clearviewport功能: 清除图形视区用法: void far clearviewport(void);函数名:_close, close功能: 关闭文件句柄用法:int close(int handle);函数名: clock功能:确定处理器时间用法: clock_t clock(void);函数名:closegraph功能: 关闭图形系统用法: void far closegraph(void);函数名:clreol功能: 在文本窗口中清除字符到行末用法:void clreol(void)函数名:clrscr功能: 清除文本模式窗口用法:void clrscr(void);函数名: coreleft功能: 返回未使用内存的大小用法:unsigned coreleft(void);函数名: cos功能: 余弦函数用法:double cos(double x);函数名:cosh功能: 双曲余弦函数用法: dluble cosh(double x);函数名: country功能: 返回与国家有关的信息用法: struct COUNTRY *country(int countrycode, struct country *country); 函数名: cprintf功能: 送格式化输出至屏幕用法:int cprintf(const char *format[, argument, ...]);函数名: cputs功能: 写字符到屏幕用法: void cputs(const char *string);函数名: _creat creat功能: 创建一个新文件或重写一个已存在的文件用法: int creat (const char *filename, int permiss)函数名:creatnew功能: 创建一个新文件用法:int creatnew(const char *filename, int attrib);函数名: cscanf功能: 从控制台执行格式化输入用法:int cscanf(char *format[,argument, ...]);函数名: ctime功能: 把日期和时间转换为字符串用法:char *ctime(const time_t *time);功能: 设置Ctrl-Break处理程序用法: void ctrlbrk(*fptr)(void);函数名: delay功能: 将程序的执行暂停一段时间(毫秒)用法: void delay(unsigned milliseconds);函数名: delline功能: 在文本窗口中删去一行用法: void delline(void);函数名:detectgraph功能: 通过检测硬件确定图形驱动程序和模式用法: void far detectgraph(int far *graphdriver, int far *graphmode); 函数名: difftime功能: 计算两个时刻之间的时间差用法: double difftime(time_t time2, time_t time1);函数名: disable功能: 屏蔽中断用法:void disable(void);函数名: div功能: 将两个整数相除, 返回商和余数用法:div_t (int number, int denom);函数名: dosexterr功能: 获取扩展DOS错误信息用法:int dosexterr(struct DOSERR *dblkp);函数名: dostounix功能: 转换日期和时间为UNIX时间格式用法: long dostounix(struct date *dateptr, struct time *timeptr);函数名: drawpoly功能: 画多边形用法: void far drawpoly(int numpoints, int far *polypoints);函数名:dup功能: 复制一个文件句柄用法: int dup(int handle);函数名:dup2功能: 复制文件句柄用法: int dup2(int oldhandle, int newhandle);功能: 把一个浮点数转换为字符串用法: char ecvt(double value, int ndigit, int *decpt, int *sign);函数名: ellipse功能: 画一椭圆用法:void far ellipse(int x, int y, int stangle, int endangle,int xradius, int yradius);函数名: enable功能: 开放硬件中断用法: void enable(void);函数名: eof功能: 检测文件结束用法: int eof(int *handle);函数名: exec...功能: 装入并运行其它程序的函数用法: int execl(char *pathname, char *arg0, arg1, ..., argn, NULL); int execle(char *pathname, char *arg0, arg1, ..., argn, NULL,char *envp[]);int execlp(char *pathname, char *arg0, arg1, .., NULL);int execple(char *pathname, char *arg0, arg1, ..., NULL,char *envp[]);int execv(char *pathname, char *argv[]);int execve(char *pathname, char *argv[], char *envp[]);int execvp(char *pathname, char *argv[]);int execvpe(char *pathname, char *argv[], char *envp[]);函数名:exit功能: 终止程序用法: void exit(int status);函数名: exp功能: 指数函数用法: double exp(double x);函数名: gcvt功能: 把浮点数转换成字符串用法: char *gcvt(double value, int ndigit, char *buf);函数名: geninterrupt功能: 产生一个软中断函数名: getarccoords功能: 取得最后一次调用arc的坐标用法: void far getarccoords(struct arccoordstype far *arccoords); 函数名: getaspectratio功能: 返回当前图形模式的纵横比用法: void far getaspectratio(int far *xasp, int far *yasp);函数名: getbkcolor功能: 返回当前背景颜色用法: int far getbkcolor(void);函数名: getc功能: 从流中取字符用法: int getc(FILE *stream);函数名: getcbrk功能: 获取Control_break设置用法: int getcbrk(void);函数名: getch功能: 从控制台无回显地取一个字符用法: int getch(void);函数名: getchar功能: 从stdin流中读字符用法: int getchar(void);函数名: getche功能: 从控制台取字符(带回显)用法: int getche(void);函数名: getcolor功能: 返回当前画线颜色用法: int far getcolor(void);函数名: getcurdir功能: 取指定驱动器的当前目录用法: int getcurdir(int drive, char *direc);函数名: getcwd功能: 取当前工作目录用法: char *getcwd(char *buf, int n);函数名: getdate功能: 取DOS日期函数名: getdefaultpalette功能: 返回调色板定义结构用法: struct palettetype *far getdefaultpalette(void);函数名: getdisk功能: 取当前磁盘驱动器号用法: int getdisk(void);函数名: getdrivername功能: 返回指向包含当前图形驱动程序名字的字符串指针用法: char *getdrivename(void);函数名: getdta功能: 取磁盘传输地址用法: char far *getdta(void);函数名: getenv功能: 从环境中取字符串用法: char *getenv(char *envvar);函数名: getfat, getfatd功能: 取文件分配表信息用法: void getfat(int drive, struct fatinfo *fatblkp);函数名: getfillpattern功能: 将用户定义的填充模式拷贝到内存中用法: void far getfillpattern(char far *upattern);函数名: getfillsettings功能: 取得有关当前填充模式和填充颜色的信息用法: void far getfillsettings(struct fillsettingstype far *fillinfo); 函数名: getftime功能: 取文件日期和时间用法: int getftime(int handle, struct ftime *ftimep);函数名: getgraphmode功能: 返回当前图形模式用法: int far getgraphmode(void);函数名: getftime功能: 取文件日期和时间用法: int getftime(int handle, struct ftime *ftimep);函数名: getgraphmode功能: 返回当前图形模式用法: int far getgraphmode(void);函数名: getimage功能: 将指定区域的一个位图存到主存中用法: void far getimage(int left, int top, int right, int bottom,void far *bitmap);函数名: getlinesettings功能: 取当前线型、模式和宽度用法: void far getlinesettings(struct linesettingstype far *lininfo): 函数名: getmaxx功能: 返回屏幕的最大x坐标用法: int far getmaxx(void);函数名: getmaxy功能: 返回屏幕的最大y坐标用法: int far getmaxy(void);函数名: getmodename功能: 返回含有指定图形模式名的字符串指针用法: char *far getmodename(int mode_name);函数名: getmoderange功能: 取给定图形驱动程序的模式范围用法: void far getmoderange(int graphdriver, int far *lomode,int far *himode);函数名: getpalette功能: 返回有关当前调色板的信息用法: void far getpalette(struct palettetype far *palette);函数名: getpass功能: 读一个口令用法: char *getpass(char *prompt);函数名: getpixel功能: 取得指定像素的颜色用法: int far getpixel(int x, int y);函数名: gets功能: 从流中取一字符串用法: char *gets(char *string);函数名: gettext功能: 将文本方式屏幕上的文本拷贝到存储区用法: int gettext(int left, int top, int right, int bottom, void *destin);函数名: gettextinfo功能: 取得文本模式的显示信息用法: void gettextinfo(struct text_info *inforec);函数名: gettextsettings功能: 返回有关当前图形文本字体的信息用法: void far gettextsettings(struct textsettingstype far *textinfo); 函数名: gettime功能: 取得系统时间用法: void gettime(struct time *timep);函数名: getvect功能: 取得中断向量入口用法: void interrupt(*getvect(int intr_num));函数名: getverify功能: 返回DOS校验标志状态用法: int getverify(void);函数名: getviewsetting功能: 返回有关当前视区的信息用法: void far getviewsettings(struct viewporttype far *viewport); 函数名: getw功能: 从流中取一整数用法: int getw(FILE *strem);函数名: getx功能: 返回当前图形位置的x坐标用法: int far getx(void);函数名: gety功能: 返回当前图形位置的y坐标用法: int far gety(void);函数名: gmtime功能: 把日期和时间转换为格林尼治标准时间(GMT)用法: struct tm *gmtime(long *clock);函数名: gotoxy功能: 在文本窗口中设置光标用法: void gotoxy(int x, int y);函数名: gotoxy功能: 在文本窗口中设置光标用法: void gotoxy(int x, int y);函数名: graphdefaults功能: 将所有图形设置复位为它们的缺省值用法: void far graphdefaults(void);函数名: grapherrormsg功能: 返回一个错误信息串的指针用法: char *far grapherrormsg(int errorcode);函数名: graphresult功能: 返回最后一次不成功的图形操作的错误代码用法: int far graphresult(void);函数名: _graphfreemem功能: 用户可修改的图形存储区释放函数用法: void far _graphfreemem(void far *ptr, unsigned size);函数名: _graphgetmem功能: 用户可修改的图形存储区分配函数用法: void far *far _graphgetmem(unsigned size);函数名: harderr功能: 建立一个硬件错误处理程序用法: void harderr(int (*fptr)());函数名: hardresume功能: 硬件错误处理函数用法: void hardresume(int rescode);函数名: highvideo功能: 选择高亮度文本字符用法: void highvideo(void);函数名: hypot功能: 计算直角三角形的斜边长用法: double hypot(double x, double y);函数名: imagesize功能: 返回保存位图像所需的字节数用法: unsigned far imagesize(int left, int top, int right, int bottom); 函数名: initgraph功能: 初始化图形系统用法: void far initgraph(int far *graphdriver, int far *graphmode函数名: inport功能: 从硬件端口中输入用法: int inp(int protid);函数名: insline功能: 在文本窗口中插入一个空行用法: void insline(void);函数名: installuserdriver功能: 安装设备驱动程序到BGI设备驱动程序表中用法: int far installuserdriver(char far *name, int (*detect)(void));函数名: installuserfont功能: 安装未嵌入BGI系统的字体文件(CHR)用法: int far installuserfont(char far *name);函数名: int86功能: 通用8086软中断接口用法: int int86(int intr_num, union REGS *inregs, union REGS *outregs) 函数名: int86x功能: 通用8086软中断接口用法: int int86x(int intr_num, union REGS *insegs, union REGS *outregs, 函数名: intdos功能: 通用DOS接口用法: int intdos(union REGS *inregs, union REGS *outregs);函数名: intdosx功能: 通用DOS中断接口用法: int intdosx(union REGS *inregs, union REGS *outregs,struct SREGS *segregs);函数名: intr功能: 改变软中断接口用法: void intr(int intr_num, struct REGPACK *preg);函数名: ioctl功能: 控制I/O设备用法: int ioctl(int handle, int cmd[,int *argdx, int argcx]);函数名: isatty功能: 检查设备类型用法: int isatty(int handle);函数名: itoa功能: 把一整数转换为字符串用法: char *itoa(int value, char *string, int radix);函数名: kbhit功能: 检查当前按下的键用法: int kbhit(void);函数名: keep功能: 退出并继续驻留用法: void keep(int status, int size);函数名: kbhit功能: 检查当前按下的键用法: int kbhit(void);函数名: keep功能: 退出并继续驻留用法: void keep(int status, int size);函数名: labs用法: long labs(long n);函数名: ldexp功能: 计算value*2的幂用法: double ldexp(double value, int exp);函数名: ldiv功能: 两个长整型数相除, 返回商和余数用法: ldiv_t ldiv(long lnumer, long ldenom);函数名: lfind功能: 执行线性搜索用法: void *lfind(void *key, void *base, int *nelem, int width,int (*fcmp)());函数名: line功能: 在指定两点间画一直线用法: void far line(int x0, int y0, int x1, int y1);函数名: linerel功能: 从当前位置点(CP)到与CP有一给定相对距离的点画一直线用法: void far linerel(int dx, int dy);函数名: localtime功能: 把日期和时间转变为结构用法: struct tm *localtime(long *clock);函数名: lock功能: 设置文件共享锁用法: int lock(int handle, long offset, long length);函数名: log功能: 对数函数ln(x)用法: double log(double x);函数名: log10功能: 对数函数log用法: double log10(double x);函数名: longjump功能: 执行非局部转移用法: void longjump(jmp_buf env, int val);函数名: lowvideo功能: 选择低亮度字符用法: void lowvideo(void);函数名: lrotl, _lrotl功能: 将无符号长整型数向左循环移位用法: unsigned long lrotl(unsigned long lvalue, int count);unsigned long _lrotl(unsigned long lvalue, int count);函数名: lsearch功能: 线性搜索用法: void *lsearch(const void *key, void *base, size_t *nelem,size_t width, int (*fcmp)(const void *, const void *));函数名: lseek功能: 移动文件读/写指针用法: long lseek(int handle, long offset, int fromwhere);main()主函数每一C 程序都必须有一main() 函数, 可以根据自己的爱好把它放在程序的某个地方。
C51的常用库函数详解C51语言的编译器中包含有丰富的库函数,使用库函数可以大大简化用户程序设计的工作量,提高编程效率。
每个库函数都在相应的头文件中给出了函数原型声明,在使用时,必须在源程序的开始处使用预处理命令#include将有关的头文件包含进来。
C51库函数中类型的选择考虑到了8051单片机的结构特性,用户在自己的应用程序中应尽可能地使用最小的数据类型,以最大限度地发挥8051单片机的性能,同时可减少应用程序的代码长度。
下面将C51的库函数分类列出并详细介绍其用法。
1 字符函数字符函数的原型声明包含在头文件CTYPE.H中。
常用的一些字符函数介绍如下。
1.1 检查英文字母函数检查英文字母函数用于检查形参字符是否为英文字母,其函数原型如下:bit isalpha(char c);其中,c为待判断的字符,如果是英文字母则返回1,否则返回0。
程序示例如下:1.2 检查字母数字函数检查字母数字函数用于检查形参字符是否为英文字母或数字字符,其函数原型如下:bit isalnum(char c);1.3 检查控制字符函数检查控制字符函数用于检查形参字符是否为控制字符,其函数原型:bit iscntrl (char c);其中,c为待判断的字符。
控制字符其取值范围为0x00~0xlF之间或等于0x7F,如果是,则返回1,否则返回0。
1.4 十进制数字检查函数十进制数字检查函数用于检查形参字符是否为十进制数字,其函数原型如下:bit isdigit (char c);其中,c为待判断的字符,如果是十进制数字则返回1,否则返回0。
1.5 可打印字符检查函数可打印字符检查函数用于检查形参字符是否为可打印字符,其函数原型如下:bit isgraph (char c);其中,c为待判断的字符。
可打印字符的取值范围为0x21~0x7C,不包含空格,如果是可打印字符则返回1,否则返回0。
1.6 包含空格的可打印字符检查函数包含空格的可打印字符检查函数用于检查形参字符是否为可打印字符以及空格,其函数原型如下:bit isprint (char c);其中,c为待判断字符。
c语言数学函数大全及详解C语言提供了一系列的数学函数,这些函数包含在`math.h` 头文件中。
以下是一些常见的C 语言数学函数及其简要说明:1. fabs:-函数原型:`double fabs(double x);`-描述:返回`x` 的绝对值。
2. sqrt:-函数原型:`double sqrt(double x);`-描述:返回`x` 的平方根。
3. pow:-函数原型:`double pow(double x, double y);`-描述:返回`x` 的`y` 次方。
4. exp:-函数原型:`double exp(double x);`-描述:返回自然对数的底`e` 的`x` 次方。
5. log:-函数原型:`double log(double x);`-描述:返回`x` 的自然对数。
6. sin, cos, tan:-函数原型:- `double sin(double x);`- `double cos(double x);`- `double tan(double x);`-描述:分别返回`x` 的正弦、余弦和正切值。
这些函数中`x` 的单位是弧度。
7. asin, acos, atan:-函数原型:- `double asin(double x);`- `double acos(double x);`- `double atan(double x);`-描述:分别返回`x` 的反正弦、反余弦和反正切值。
返回的值是弧度。
8. sinh, cosh, tanh:-函数原型:- `double sinh(double x);`- `double cosh(double x);`- `double tanh(double x);`-描述:分别返回`x` 的双曲正弦、双曲余弦和双曲正切值。
9. ceil:-函数原型:`double ceil(double x);`-描述:返回不小于`x` 的最小整数值。
c语言库函数大全a-b2009-11-27 10:07函数名: abort功能: 异常终止一个进程用法: void abort(void);程序例:#include <stdio.h>#include <stdlib.h>int main(void){printf("Calling abort()\n");abort();return 0; /* 实际上这句不会被执行 */}函数名: 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);elseprintf("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 terminatedstring */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);}函数名: bar功能: 画一个二维条形图用法: void far bar(int left, int top, int right, int bottom); 程序例:#include <graphics.h>#include <stdlib.h>#include <stdio.h>#include <conio.h>int main(void){/* request auto detection */int gdriver = DETECT, gmode, errorcode;/* initialize graphics and local variables */initgraph(&gdriver, &gmode, "");/* read result of initialization */errorcode = graphresult();if (errorcode != grOk) /* an error occurred */{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;/* loop through the fill patterns */for (i=SOLID_FILL; i<USER_FILL; i++){/* set the fill style */setfillstyle(i, getmaxcolor());/* draw the bar */bar(midx-50, midy-50, midx+50,midy+50);getch();}/* clean up */closegraph();return 0;}函数名: bar3d功能: 画一个三维条形图用法: void far bar3d(int left, int top, int right, int bottom, int depth, int topflag);程序例:#include <graphics.h>#include <stdlib.h>#include <stdio.h>#include <conio.h>int main(void){/* request auto detection */int gdriver = DETECT, gmode, errorcode;/* initialize graphics, local variables */initgraph(&gdriver, &gmode, "");/* read result of initialization */errorcode = graphresult();if (errorcode != grOk) /* an error occurred */{printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:");getch();exit(1); /* terminate with error code */}midx = getmaxx() / 2;midy = getmaxy() / 2;/* loop through the fill patterns */for (i=EMPTY_FILL; i<USER_FILL; i++){/* set the fill style */setfillstyle(i, getmaxcolor());/* draw the 3-d bar */bar3d(midx-50, midy-50, midx+50, midy+50, 10, 1);getch();}/* clean up */closegraph();return 0;}函数名: bdos功能: DOS系统调用用法: int bdos(int dosfun, unsigned dosdx, unsigned dosal); 程序例:#include <stdio.h>#include <dos.h>/* Get current drive as 'A', 'B', ... */char current_drive(void){char curdrive;/* Get current disk as 0, 1, ... */curdrive = bdos(0x19, 0, 0);return('A' + curdrive);}int main(void){printf("The current drive is %c:\n", current_drive());return 0;}函数名: bdosptr功能: DOS系统调用用法: int bdosptr(int dosfun, void *argument, unsigned dosal); 程序例:#include <string.h>#include <stdio.h>#include <dir.h>#include <dos.h>#include <errno.h>#include <stdlib.h>#define BUFLEN 80int main(void){char buffer[BUFLEN];int test;printf("Enter full pathname of a directory\n");gets(buffer);test = bdosptr(0x3B,buffer,0);if(test){printf("DOS error message: %d\n", errno);/* See errno.h for error listings */exit (1);}getcwd(buffer, BUFLEN);printf("The current directory is: %s\n", buffer);return 0;}函数名: bioscom功能: 串行I/O通信用法: int bioscom(int cmd, char abyte, int port);程序例:#include <bios.h>#include <conio.h>#define COM1 0#define DATA_READY 0x100#define TRUE 1#define FALSE 0#define SETTINGS ( 0x80 | 0x02 | 0x00 | 0x00)int main(void){int in, out, status, DONE = FALSE;bioscom(0, SETTINGS, COM1);cprintf("... BIOSCOM [ESC] to exit ...\n");while (!DONE){status = bioscom(3, 0, COM1);if (status & DATA_READY)if ((out = bioscom(2, 0, COM1) & 0x7F) != 0)putch(out);if (kbhit()){if ((in = getch()) == '\x1B')DONE = TRUE;bioscom(1, in, COM1);}}return 0;}函数名: biosdisk功能: 软硬盘I/O用法: int biosdisk(int cmd, int drive, int head, int track, int sector int nsects, void *buffer);程序例:#include <bios.h>#include <stdio.h>int main(void){int result;char buffer[512];printf("Testing to see if drive a: is ready\n");result = biosdisk(4,0,0,0,0,1,buffer);result &= 0x02;(result) ? (printf("Drive A: Ready\n")) :(printf("Drive A: Not Ready\n"));return 0;}函数名: biosequip功能: 检查设备用法: int biosequip(void);程序例:#include <bios.h>#include <stdio.h>int main(void){int result;char buffer[512];printf("Testing to see if drive a: is ready\n");result = biosdisk(4,0,0,0,0,1,buffer);result &= 0x02;(result) ? (printf("Drive A: Ready\n")) :(printf("Drive A: Not Ready\n"));return 0;}函数名: bioskey功能: 直接使用BIOS服务的键盘接口用法: int bioskey(int cmd);程序例:#include <stdio.h>#include <bios.h>#include <ctype.h>#define RIGHT 0x01#define LEFT 0x02#define CTRL 0x04#define ALT 0x08int main(void){int key, modifiers;/* function 1 returns 0 until a key is pressed */while (bioskey(1) == 0);/* function 0 returns the key that is waiting */key = bioskey(0);/* use function 2 to determine if shift keys were used */modifiers = bioskey(2);if (modifiers){printf("[");if (modifiers & RIGHT) printf("RIGHT");if (modifiers & LEFT) printf("LEFT");if (modifiers & CTRL) printf("CTRL");if (modifiers & ALT) printf("ALT");printf("]");}/* print out the character read */if (isalnum(key & 0xFF))printf("'%c'\n", key);elseprintf("%#02x\n", key);return 0;}函数名: biosmemory功能: 返回存储块大小用法:int biosmemory(void);程序例:#include <stdio.h>#include <bios.h>int main(void){int memory_size;memory_size = biosmemory(); /* returns value up to 640K */ printf("RAM size = %dK\n",memory_size);return 0;}函数名: biosprint功能: 直接使用BIOS服务的打印机I/O用法: int biosprint(int cmd, int byte, int port);程序例:#include <stdio.h>#include <conio.h>#include <bios.h>int main(void){#define STATUS 2 /* printer status command */#define PORTNUM 0 /* port number for LPT1 */int status, abyte=0;printf("Please turn off your printer. Press any key to continue\n"); getch();status = biosprint(STATUS, abyte, PORTNUM);if (status & 0x01)printf("Device time out.\n");if (status & 0x08)printf("I/O error.\n");if (status & 0x10)printf("Selected.\n");if (status & 0x20)printf("Out of paper.\n");if (status & 0x40)printf("Acknowledge.\n");if (status & 0x80)printf("Not busy.\n");return 0;}函数名: biostime功能: 读取或设置BIOS时间用法: long biostime(int cmd, long newtime);程序例:#include <stdio.h>#include <bios.h>#include <time.h>#include <conio.h>int main(void){long bios_time;clrscr();cprintf("The number of clock ticks since midnight is:\r\n");cprintf("The number of seconds since midnight is:\r\n");cprintf("The number of minutes since midnight is:\r\n");cprintf("The number of hours since midnight is:\r\n");cprintf("\r\nPress any key to quit:");while(!kbhit()){bios_time = biostime(0, 0L);gotoxy(50, 1);cprintf("%lu", bios_time);gotoxy(50, 2);cprintf("%.4f", bios_time / CLK_TCK);gotoxy(50, 3);cprintf("%.4f", bios_time / CLK_TCK / 60);gotoxy(50, 4);cprintf("%.4f", bios_time / CLK_TCK / 3600);}return 0;}函数名: brk功能: 改变数据段空间分配用法: int brk(void *endds);程序例:#include <stdio.h>#include <alloc.h>int main(void){char *ptr;printf("Changing allocation with brk()\n");ptr = malloc(1);printf("Before brk() call: %lu bytes free\n", coreleft());brk(ptr+1000);printf(" After brk() call: %lu bytes free\n", coreleft());return 0;}函数名: bsearch功能: 二分法搜索用法: void *bsearch(const void *key, const void *base, size_t *nelem, size_t width, int(*fcmp)(const void *, const *));程序例:#include <stdlib.h>#include <stdio.h>#define NELEMS(arr) (sizeof(arr) / sizeof(arr[0]))int numarray[] = {123, 145, 512, 627, 800, 933};int numeric (const int *p1, const int *p2){return(*p1 - *p2);}int lookup(int key){int *itemptr;/* The cast of (int(*)(const void *,const void*))is needed to avoid a type mismatch error atcompile time */itemptr = bsearch (&key, numarray, NELEMS(numarray), sizeof(int), (int(*)(const void *,const void *))numeric); return (itemptr != NULL);}int main(void){if (lookup(512))printf("512 is in the table.\n");elseprintf("512 isn't in the table.\n");return 0;}。