C语言函数大全L篇.docx
《C语言函数大全L篇.docx》由会员分享,可在线阅读,更多相关《C语言函数大全L篇.docx(7页珍藏版)》请在冰豆网上搜索。
C语言函数大全L篇
L:
函数名:
lfind
功 能:
执行线性搜索
用 法:
void*lfind(void*key,void*base,int*nelem,intwidth,
int(*fcmp)());
程序例:
#include
#include
intcompare(int*x,int*y)
{
return(*x-*y);
}
intmain(void)
{
intarray[5]={35,87,46,99,12};
size_tnelem=5;
intkey;
int*result;
key=99;
result=lfind(&key,array,&nelem,
sizeof(int),(int(*)(constvoid*,constvoid*))compare);
if(result)
printf("Number%dfound\n",key);
else
printf("Number%dnotfound\n",key);
return0;
}
函数名:
line
功 能:
在指定两点间画一直线
用 法:
voidfarline(intx0,inty0,intx1,inty1);
程序例:
#include
#include
#include
#include
intmain(void)
{
/*requestautodetection*/
intgdriver=DETECT,gmode,errorcode;
intxmax,ymax;
/*initializegraphicsandlocalvariables*/
initgraph(&gdriver,&gmode,"");
/*readresultofinitialization*/
errorcode=graphresult();
/*anerroroccurred*/
if(errorcode!
=grOk)
{
printf("Graphicserror:
%s\n",
grapherrormsg(errorcode));
printf("Pressanykeytohalt:
");
getch();
exit
(1);
}
setcolor(getmaxcolor());
xmax=getmaxx();
ymax=getmaxy();
/*drawadiagonalline*/
line(0,0,xmax,ymax);
/*cleanup*/
getch();
closegraph();
return0;
}
函数名:
linerel
功 能:
从当前位置点(CP)到与CP有一给定相对距离的点画一直线
用 法:
voidfarlinerel(intdx,intdy);
程序例:
#include
#include
#include
#include
intmain(void)
{
/*requestautodetection*/
intgdriver=DETECT,gmode,errorcode;
charmsg[80];
/*initializegraphicsandlocalvariables*/
initgraph(&gdriver,&gmode,"");
/*readresultofinitialization*/
errorcode=graphresult();
if(errorcode!
=grOk)
{
printf("Graphicserror:
%s\n",
grapherrormsg(errorcode));
printf("Pressanykeytohalt:
");
getch();
exit
(1);
}
/*movetheC.P.tolocation(20,30)*/
moveto(20,30);
/*createandoutputa
messageat(20,30)*/
sprintf(msg,"(%d,%d)",getx(),gety());
outtextxy(20,30,msg);
/*drawalinetoapointarelative
distanceawayfromthecurrent
valueofC.P. */
linerel(100,100);
/*createandoutputamessageatC.P.*/
sprintf(msg,"(%d,%d)",getx(),gety());
outtext(msg);
/*cleanup*/
getch();
closegraph();
return0;
}
函数名:
lock
功 能:
设置文件共享锁
用 法:
intlock(inthandle,longoffset,longlength);
程序例:
#include
#include
#include
#include
#include
#include
intmain(void)
{
inthandle,status;
longlength;
/*MusthaveDOSShare.exeloadedfor*/
/*filelockingtofunctionproperly*/
handle=sopen("c:
\\autoexec.bat",
O_RDONLY,SH_DENYNO,S_IREAD);
if(handle<0)
{
printf("sopenfailed\n");
exit
(1);
}
length=filelength(handle);
status=lock(handle,0L,length/2);
if(status==0)
printf("locksucceeded\n");
else
printf("lockfailed\n");
status=unlock(handle,0L,length/2);
if(status==0)
printf("unlocksucceeded\n");
else
printf("unlockfailed\n");
close(handle);
return0;
}
函数名:
lrotl,_lrotl
功 能:
将无符号长整型数向左循环移位
用 法:
unsignedlonglrotl(unsignedlonglvalue,intcount);
unsignedlong_lrotl(unsignedlonglvalue,intcount);
程序例:
/*lrotlexample*/
#include
#include
intmain(void)
{
unsignedlongresult;
unsignedlongvalue=100;
result=_lrotl(value,1);
printf("Thevalue%lurotatedleftonebitis:
%lu\n",value,result);
return0;
}
函数名:
lsearch
功 能:
线性搜索
用 法:
void*lsearch(constvoid*key,void*base,size_t*nelem,
size_twidth,int(*fcmp)(constvoid*,constvoid*));
程序例:
#include
#include
intcompare(int*x,int*y)
{
return(*x-*y);
}
intmain(void)
{
intarray[5]={35,87,46,99,12};
size_tnelem=5;
intkey;
int*result;
key=99;
result=lfind(&key,array,&nelem,
sizeof(int),(int(*)(constvoid*,constvoid*))compare);
if(result)
printf("Number%dfound\n",key);
else
printf("Number%dnotfound\n",key);
return0;
}
函数名:
lseek
功 能:
移动文件读/写指针
用 法:
longlseek(inthandle,longoffset,intfromwhere);
程序例:
#include
#include
#include
#include
#include
intmain(void)
{
inthandle;
charmsg[]="Thisisatest";
charch;
/*createafile*/
handle=open("TEST.$$$",O_CREAT|O_RDWR,S_IREAD|S_IWRITE);
/*writesomedatatothefile*/
write(handle,msg,strlen(msg));
/*seektothebeginingofthefile*/
lseek(handle,0L,SEEK_SET);
/*readscharsfromthefileuntilwehitEOF*/
do
{
read(handle,&ch,1);
printf("%c",ch);
} while(!
eof(handle));
close(handle);
return0;
}