C语言F.docx
《C语言F.docx》由会员分享,可在线阅读,更多相关《C语言F.docx(30页珍藏版)》请在冰豆网上搜索。
C语言F
返回>>C语言函数大全
函数名:
fabs
功 能:
返回浮点数的绝对值
用 法:
doublefabs(doublex);
程序例:
#include
#include
intmain(void)
{
float number=-1234.0;
printf("number:
%f absolutevalue:
%f\n",
number,fabs(number));
return0;
}
函数名:
farcalloc
功 能:
从远堆栈中申请空间
用 法:
voidfar*farcalloc(unsignedlongunits,unsignedlingunitsz);
程序例:
#include
#include
#include
#include
intmain(void)
{
charfar*fptr;
char*str="Hello";
/*allocatememoryforthefarpointer*/
fptr=farcalloc(10,sizeof(char));
/*copy"Hello"intoallocatedmemory*/
/*
Note:
movedataisusedbecauseyou
mightbeinasmalldatamodel,in
whichcaseanormalstringcopyroutine
cannotbeusedsinceitassumesthe
pointersizeisnear.
*/
movedata(FP_SEG(str),FP_OFF(str),
FP_SEG(fptr),FP_OFF(fptr),
strlen(str));
/*displaystring(notetheFmodifier)*/
printf("Farstringis:
%Fs\n",fptr);
/*freethememory*/
farfree(fptr);
return0;
}
函数名:
farcoreleft
功 能:
返回远堆中未作用存储区大小
用 法:
longfarcoreleft(void);
程序例:
#include
#include
intmain(void)
{
printf("Thedifferencebetweenthe\
highestallocatedblockinthe\
far\n");
printf("heapandthetopofthefarheap\
is:
%lubytes\n",farcoreleft());
return0;
}
函数名:
farfree
功 能:
从远堆中释放一块
用 法:
voidfarfree(void);
程序例:
#include
#include
#include
#include
intmain(void)
{
charfar*fptr;
char*str="Hello";
/*allocatememoryforthefarpointer*/
fptr=farcalloc(10,sizeof(char));
/*copy"Hello"intoallocatedmemory*/
/*
Note:
movedataisusedbecauseyoumightbeinasmalldatamodel,
inwhichcaseanormalstringcopyroutinecan'tbeusedsinceit
assumesthepointersizeisnear.
*/
movedata(FP_SEG(str),FP_OFF(str),
FP_SEG(fptr),FP_OFF(fptr),
strlen(str));
/*displaystring(notetheFmodifier)*/
printf("Farstringis:
%Fs\n",fptr);
/*freethememory*/
farfree(fptr);
return0;
}
函数名:
farmalloc
功 能:
从远堆中分配存储块
用 法:
voidfar*farmalloc(unsignedlongsize);
程序例:
#include
#include
#include
#include
intmain(void)
{
charfar*fptr;
char*str="Hello";
/*allocatememoryforthefarpointer*/
fptr=farmalloc(10);
/*copy"Hello"intoallocatedmemory*/
/*
Note:
movedataisusedbecausewemight
beinasmalldatamodel,inwhichcase
anormalstringcopyroutinecannotbe
usedsinceitassumesthepointersize
isnear.
*/
movedata(FP_SEG(str),FP_OFF(str),
FP_SEG(fptr),FP_OFF(fptr),
strlen(str));
/*displaystring(notetheFmodifier)*/
printf("Farstringis:
%Fs\n",fptr);
/*freethememory*/
farfree(fptr);
return0;
}
函数名:
farrealloc
功 能:
调整远堆中的分配块
用 法:
voidfar*farrealloc(voidfar*block,unsignedlongnewsize);
程序例:
#include
#include
intmain(void)
{
charfar*fptr;
fptr=farmalloc(10);
printf("Firstaddress:
%Fp\n",fptr);
fptr=farrealloc(fptr,20);
printf("Newaddress :
%Fp\n",fptr);
farfree(fptr);
return0;
}
函数名:
fclose
功 能:
关闭一个流
用 法:
intfclose(FILE*stream);
程序例:
#include
#include
intmain(void)
{
FILE*fp;
charbuf[11]="0123456789";
/*createafilecontaining10bytes*/
fp=fopen("DUMMY.FIL","w");
fwrite(&buf,strlen(buf),1,fp);
/*closethefile*/
fclose(fp);
return0;
}
函数名:
fcloseall
功 能:
关闭打开流
用 法:
intfcloseall(void);
程序例:
#include
intmain(void)
{
intstreams_closed;
/*opentwostreams*/
fopen("DUMMY.ONE","w");
fopen("DUMMY.TWO","w");
/*closetheopenstreams*/
streams_closed=fcloseall();
if(streams_closed==EOF)
/*issueanerrormessage*/
perror("Error");
else
/*printresultoffcloseall()function*/
printf("%dstreamswereclosed.\n",streams_closed);
return0;
}
函数名:
fcvt
功 能:
把一个浮点数转换为字符串
用 法:
char*fcvt(doublevalue,intndigit,int*decpt,int*sign);
程序例:
#include
#include
#include
intmain(void)
{
char*string;
doublevalue;
intdec,sign;
intndig=10;
clrscr();
value=9.876;
string=ecvt(value,ndig,&dec,&sign);
printf("string=%s dec=%d\
sign=%d\n",string,dec,sign);
value=-123.45;
ndig=15;
string=ecvt(value,ndig,&dec,&sign);
printf("string=%sdec=%dsign=%d\n",
string,dec,sign);
value=0.6789e5;/*scientific
notation*/
ndig=5;
string=ecvt(value,ndig,&dec,&sign);
printf("string=%s dec=%d\
sign=%d\n",string,dec,sign);
return0;
}
函数名:
fdopen
功 能:
把流与一个文件句柄相接
用 法:
FILE*fdopen(inthandle,char*type);
程序例:
#include
#include
#include
#include
intmain(void)
{
inthandle;
FILE*stream;
/*openafile*/
handle=open("DUMMY.FIL",O_CREAT,
S_IREAD|S_IWRITE);
/*nowturnthehandleintoastream*/
stream=fdopen(handle,"w");
if(stream==NULL)
printf("fdopenfailed\n");
else
{
fprintf(stream,"Helloworld\n");
fclose(stream);
}
return0;
}
函数名:
feof
功 能:
检测流上的文件结束符
用 法:
intfeof(FILE*stream);
程序例:
#include
intmain(void)
{
FILE*stream;
/*openafileforreading*/
stream=fopen("DUMMY.FIL","r");
/*readacharacterfromthefile*/
fgetc(stream);
/*checkforEOF*/
if(feof(stream))
printf("Wehavereachedend-of-file\n");
/*closethefile*/
fclose(stream);
return0;
}
函数名:
ferror
功 能:
检测流上的错误
用 法:
intferror(FILE*stream);
程序例:
#include
intmain(void)
{
FILE*stream;
/*openafileforwriting*/
stream=fopen("DUMMY.FIL","w");
/*forceanerrorconditionbyattemptingtoread*/
(void)getc(stream);
if(ferror(stream)) /*testforanerroronthestream*/
{
/*displayanerrormessage*/
printf("ErrorreadingfromDUMMY.FIL\n");
/*resettheerrorandEOFindicators*/
clearerr(stream);
}
fclose(stream);
return0;
}
函数名:
fflush
功 能:
清除一个流
用 法:
intfflush(FILE*stream);
程序例:
#include
#include
#include
#include
voidflush(FILE*stream);
intmain(void)
{
FILE*stream;
charmsg[]="Thisisatest";
/*createafile*/
stream=fopen("DUMMY.FIL","w");
/*writesomedatatothefile*/
fwrite(msg,strlen(msg),1,stream);
clrscr();
printf("Pressanykeytoflush\
DUMMY.FIL:
");
getch();
/*flushthedatatoDUMMY.FILwithout\
closingit*/
flush(stream);
printf("\nFilewasflushed,Pressanykey\
toquit:
");
getch();
return0;
}
voidflush(FILE*stream)
{
intduphandle;
/*flushthestream'sinternalbuffer*/
fflush(stream);
/*makeaduplicatefilehandle*/
duphandle=dup(fileno(stream));
/*closetheduplicatehandletoflush\
theDOSbuffer*/
close(duphandle);
}
函数名:
fgetc
功 能:
从流中读取字符
用 法:
intfgetc(FILE*stream);
程序例:
#include
#include
#include
intmain(void)
{
FILE*stream;
charstring[]="Thisisatest";
charch;
/*openafileforupdate*/
stream=fopen("DUMMY.FIL","w+");
/*writeastringintothefile*/
fwrite(string,strlen(string),1,stream);
/*seektothebeginningofthefile*/
fseek(stream,0,SEEK_SET);
do
{
/*readacharfromthefile*/
ch=fgetc(stream);
/*displaythecharacter*/
putch(ch);
}while(ch!
=EOF);
fclose(stream);
return0;
}
函数名:
fgetchar
功 能:
从流中读取字符
用 法:
intfgetchar(void);
程序例:
#include
intmain(void)
{
charch;
/*prompttheuserforinput*/
printf("Enteracharacterfollowedby\
:
");
/*readthecharacterfromstdin*/
ch=fgetchar();
/*displaywhatwasread*/
printf("Thecharacterreadis:
'%c'\n",
ch);
return0;
}
函数名:
fgetpos
功 能:
取得当前文件的句柄
用 法:
intfgetpos(FILE*stream);
程序例:
#include
#include
intmain(void)
{
FILE*stream;
charstring[]="Thisisatest";
fpos_tfilepos;
/*openafileforupdate*/
stream=fopen("DUMMY.FIL","w+");
/*writeastringintothefile*/
fwrite(string,strlen(string),1,stream);
/*reportthefilepointerposition*/
fgetpos(stream,&filepos);
printf("Thefilepointerisatbyte\
%ld\n",filepos);
fclose(stream);
return0;
}
函数名:
fgets
功 能:
从流中读取一字符串
用 法:
char*fgets(char*string,intn,FILE*stream);
程序例:
#include
#include
intmain(void)
{
FILE*stream;
charstring[]="Thisisatest";
charmsg[20];
/*openafileforupdate*/
stream=fopen("DUMMY.FIL","w+");
/*writeastringintothefile*/
fwrite(string,strlen(string),1,stream);
/*seektothestartofthefile*/
fseek(stream,0,SEEK_SET);
/*readastringfromthefile*/
fgets(msg,strlen(string)+1,stream);
/*displaythestring*/
printf("%s",msg);
fclose(stream);
return0;
}
函数名:
filelength
功 能:
取文件长度字节数
用 法:
longfilelength(inthandle);
程序例:
#include
#incl