C函数大全R2.docx
《C函数大全R2.docx》由会员分享,可在线阅读,更多相关《C函数大全R2.docx(18页珍藏版)》请在冰豆网上搜索。
![C函数大全R2.docx](https://file1.bdocx.com/fileroot1/2023-4/20/065be041-c0cf-4e73-b607-f0b0a0e45376/065be041-c0cf-4e73-b607-f0b0a0e453761.gif)
C函数大全R2
函数大全(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#%dFile:
%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#%dFile:
%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\nAddressis%p\n",str,str);
str=realloc(str,20);
printf("Stringis%s\nNewaddressis%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;
stat=mkdir(DIRNAME);
if(!
stat)
printf("Directorycreated\n");
else
{
printf("Unabletocreatedirectory\n");
exit
(1);
}
getch();
system("dir/p");
getch();
stat=rmdir(DIRNAME);
if(!
stat)
printf("\nDirectorydeleted\n");
else
{
perror("\nUnabletodeletedirectory\n");
exit
(1);
}
return0;
}