C语言函数大全R.docx
《C语言函数大全R.docx》由会员分享,可在线阅读,更多相关《C语言函数大全R.docx(11页珍藏版)》请在冰豆网上搜索。
![C语言函数大全R.docx](https://file1.bdocx.com/fileroot1/2022-11/25/f65361fe-51fb-441d-9ac1-65352262507d/f65361fe-51fb-441d-9ac1-65352262507d1.gif)
C语言函数大全R
函数名:
raise
功 能:
向正在执行的程序发送一个信号
用 法:
intraise(intsig);
程序例:
#include
intmain(void)
{
inta,b;
a=10;
b=0;
if(b==0)
/*preemptdividebyzeroerror*/
raise(SIGFPE);
a=a/b;
return0;
}
函数名:
rand
功 能:
随机数发生器
用 法:
voidrand(void);
程序例:
#include
#include
intmain(void)
{
inti;
printf("Tenrandomnumbersfrom0to99\n\n");
for(i=0;i<10;i++)
printf("%d\n",rand()%100);
return0;
}
函数名:
randbrd
功 能:
随机块读
用 法:
intrandbrd(structfcb*fcbptr,intreccnt);
程序例:
#include
#include
#include
#include
intmain(void)
{
charfar*save_dta;
charline[80],buffer[256];
structfcbblk;
inti,result;
/*getuserinputfilenamefordta*/
printf("Enterdriveandfilename(nopath-i.e.a:
file.dat)\n");
gets(line);
/*putfilenameinfcb*/
if(!
parsfnm(line,&blk,1))
{
printf("Errorincalltoparsfnm\n");
exit
(1);
}
printf("Drive#%d File:
%s\n\n",blk.fcb_drive,blk.fcb_name);
/*openfilewithDOSFCBopenfile*/
bdosptr(0x0F,&blk,0);
/*saveolddta,andsetnewone*/
save_dta=getdta();
setdta(buffer);
/*setupinfoforthenewdta*/
blk.fcb_recsize=128;
blk.fcb_random=0L;
result=randbrd(&blk,1);
/*checkresultsfromrandbrd*/
if(!
result)
printf("ReadOK\n\n");
else
{
perror("Errorduringread");
exit
(1);
}
/*readindatafromthenewdta*/
printf("Thefirst128charactersare:
\n");
for(i=0;i<128;i++)
putchar(buffer[i]);
/*restorepreviousdta*/
setdta(save_dta);
return0;
}
函数名:
randbwr
功 能:
随机块写
用 法:
intrandbwr(structfcp*fcbptr,intreccnt);
程序例:
#include
#include
#include
#include
intmain(void)
{
charfar*save_dta;
charline[80];
charbuffer[256]="RANDBWRtest!
";
structfcbblk;
intresult;
/*getnewfilenamefromuser*/
printf("Enterafilenametocreate(nopath-ie.a:
file.dat\n");
gets(line);
/*parsethenewfilenametothedta*/
parsfnm(line,&blk,1);
printf("Drive#%d File:
%s\n",blk.fcb_drive,blk.fcb_name);
/*requestDOSservicestocreatefile*/
if(bdosptr(0x16,&blk,0)==-1)
{
perror("Errorcreatingfile");
exit
(1);
}
/*saveolddtaandsetnewdta*/
save_dta=getdta();
setdta(buffer);
/*writenewrecords*/
blk.fcb_recsize=256;
blk.fcb_random=0L;
result=randbwr(&blk,1);
if(!
result)
printf("WriteOK\n");
else
{
perror("Diskerror");
exit
(1);
}
/*requestDOSservicestoclosethefile*/
if(bdosptr(0x10,&blk,0)==-1)
{
perror("Errorclosingfile");
exit
(1);
}
/*resettheolddta*/
setdta(save_dta);
return0;
}
函数名:
random
功 能:
随机数发生器
用 法:
intrandom(intnum);
程序例:
#include
#include
#include
/*printsarandomnumberintherange0to99*/
intmain(void)
{
randomize();
printf("Randomnumberinthe0-99range:
%d\n",random(100));
return0;
}
函数名:
randomize
功 能:
初始化随机数发生器
用 法:
voidrandomize(void);
程序例:
#include
#include
#include
intmain(void)
{
inti;
randomize();
printf("Tenrandomnumbersfrom0to99\n\n");
for(i=0;i<10;i++)
printf("%d\n",rand()%100);
return0;
}
函数名:
read
功 能:
从文件中读
用 法:
intread(inthandle,void*buf,intnbyte);
程序例:
#include
#include
#include
#include
#include
#include
intmain(void)
{
void*buf;
inthandle,bytes;
buf=malloc(10);
/*
LooksforafileinthecurrentdirectorynamedTEST.$$$andattempts
toread10bytesfromit. Tousethisexampleyoushouldcreatethe
fileTEST.$$$
*/
if((handle=
open("TEST.$$$",O_RDONLY|O_BINARY,S_IWRITE|S_IREAD))==-1)
{
printf("ErrorOpeningFile\n");
exit
(1);
}
if((bytes=read(handle,buf,10))==-1){
printf("ReadFailed.\n");
exit
(1);
}
else{
printf("Read:
%dbytesread.\n",bytes);
}
return0;
}
函数名:
realloc
功 能:
重新分配主存
用 法:
void*realloc(void*ptr,unsignednewsize);
程序例:
#include
#include
#include
intmain(void)
{
char*str;
/*allocatememoryforstring*/
str=malloc(10);
/*copy"Hello"intostring*/
strcpy(str,"Hello");
printf("Stringis%s\n Addressis%p\n",str,str);
str=realloc(str,20);
printf("Stringis%s\n Newaddressis%p\n",str,str);
/*freememory*/
free(str);
return0;
}
函数名:
rectangle
功 能:
画一个矩形
用 法:
voidfarrectangle(intleft,inttop,intright,intbottom);
程序例:
#include
#include
#include
#include
intmain(void)
{
/*requestautodetection*/
intgdriver=DETECT,gmode,errorcode;
intleft,top,right,bottom;
/*initializegraphicsandlocalvariables*/
initgraph(&gdriver,&gmode,"");
/*readresultofinitialization*/
errorcode=graphresult();
if(errorcode!
=grOk) /*anerroroccurred*/
{
printf("Graphicserror:
%s\n",grapherrormsg(errorcode));
printf("Pressanykeytohalt:
");
getch();
exit
(1);/*terminatewithanerrorcode*/
}
left=getmaxx()/2-50;
top=getmaxy()/2-50;
right=getmaxx()/2+50;
bottom=getmaxy()/2+50;
/*drawarectangle*/
rectangle(left,top,right,bottom);
/*cleanup*/
getch();
closegraph();
return0;
}
函数名:
registerbgidriver
功 能:
登录已连接进来的图形驱动程序代码
用 法:
intregisterbgidriver(void(*driver)(void));
程序例:
#include
#include
#include
#include
intmain(void)
{
/*requestautodetection*/
intgdriver=DETECT,gmode,errorcode;
/*registeradriverthatwasaddedintographics.lib*/
errorcode=registerbgidriver(EGAVGA_driver);
/*reportanyregistrationerrors*/
if(errorcode<0)
{
printf("Graphicserror:
%s\n",grapherrormsg(errorcode));
printf("Pressanykeytohalt:
");
getch();
exit
(1);/*terminatewithanerrorcode*/
}
/*initializegraphicsandlocalvariables*/
initgraph(&gdriver,&gmode,"");
/*readresultofinitialization*/
errorcode=graphresult();
if(errorcode!
=grOk) /*anerroroccurred*/
{
printf("Graphicserror:
%s\n",grapherrormsg(errorcode));
printf("Pressanykeytohalt:
");
getch();
exit
(1);/*terminatewithanerrorcode*/
}
/*drawaline*/
line(0,0,getmaxx(),getmaxy());
/*cleanup*/
getch();
closegraph();
return0;
}
函数名:
remove
功 能:
删除一个文件
用 法:
intremove(char*filename);
程序例:
#include
intmain(void)
{
charfile[80];
/*promptforfilenametodelete*/
printf("Filetodelete:
");
gets(file);
/*deletethefile*/
if(remove(file)==0)
printf("Removed%s.\n",file);
else
perror("remove");
return0;
}
函数名:
rename
功 能:
重命名文件
用 法:
intrename(char*oldname,char*newname);
程序例:
#include
intmain(void)
{
charoldname[80],newname[80];
/*promptforfiletorenameandnewname*/
printf("Filetorename:
");
gets(oldname);
printf("Newname:
");
gets(newname);
/*Renamethefile*/
if(rename(oldname,newname)==0)
printf("Renamed%sto%s.\n",oldname,newname);
else
perror("rename");
return0;
}
函数名:
restorecrtmode
功 能:
将屏幕模式恢复为先前的imitgraph设置
用 法:
voidfarrestorecrtmode(void);
程序例:
#include
#include
#include
#include
intmain(void)
{
/*requestautodetection*/
intgdriver=DETECT,gmode,errorcode;
intx,y;
/*initializegraphicsandlocalvariables*/
initgraph(&gdriver,&gmode,"");
/*readresultofinitialization*/
errorcode=graphresult();
if(errorcode!
=grOk) /*anerroroccurred*/
{
printf("Graphicserror:
%s\n",grapherrormsg(errorcode));
printf("Pressanykeytohalt:
");
getch();
exit
(1);/*terminatewithanerrorcode*/
}
x=getmaxx()/2;
y=getmaxy()/2;
/*outputamessage*/
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(x,y,"Pressanykeytoexitgraphics:
");
getch();
/*restoresystemtotextmode*/
restorecrtmode();
printf("We'renowintextmode.\n");
printf("Pressanykeytoreturntographicsmode:
");
getch();
/*returntographicsmode*/
setgraphmode(getgraphmode());
/*outputamessage*/
settextjustify(CENTER_TEXT,CENTER_TEXT);
outtextxy(x,y,"We'rebackingraphicsmode.");
outtextxy(x,y+textheight("W"),"Pressanykeytohalt:
");
/*cleanup*/
getch();
closegraph();
return0;
}
函数名:
rewind
功 能:
将文件指针重新指向一个流的开头
用 法:
intrewind(FILE*stream);
程序例:
#include
#include
intmain(void)
{
FILE*fp;
char*fname="TXXXXXX",*newname,first;
newname=mktemp(fname);
fp=fopen(newname,"w+");
fprintf(fp,"abcdefghijklmnopqrstuvwxyz");
rewind(fp);
fscanf(fp,"%c",&first);
printf("Thefirstcharacteris:
%c\n",first);
fclose(fp);
remove(newname);
return0;
}
函数名:
rmdir
功 能:
删除DOS文件目录
用 法:
intrmdir(char*stream);
程序例:
#include
#include
#include
#include
#defineDIRNAME"testdir.$$$"
intmain(void)
{
intstat;