C语言库函数F类字母.docx
《C语言库函数F类字母.docx》由会员分享,可在线阅读,更多相关《C语言库函数F类字母.docx(31页珍藏版)》请在冰豆网上搜索。
![C语言库函数F类字母.docx](https://file1.bdocx.com/fileroot1/2023-1/31/d223ad34-1daf-4fd8-988e-45d3dd80d758/d223ad34-1daf-4fd8-988e-45d3dd80d7581.gif)
C语言库函数F类字母
C语言库函数(F类字母)
函数名:
fabs
功能:
返回浮点数的绝对值
用法:
doublefabs(doublex);
程序例:
#include
#include
intmain(void)
{
floatnumber=-1234.0;
printf("number:
%fabsolutevalue:
%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=%sdec=%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=%sdec=%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
#include
#include
#include
intmain(void)
{
inthandle;
charbuf[11]="0123456789";
/*createafilecontaining10bytes*/
handle=open("DUMMY.FIL",O_CREAT);
write(handle,buf,strlen(buf));
/*displaythesizeofthefile*/
printf("filelengthinbytes:
%ld\n",
filelength(handle));
/*closethefile*/
close(handle);
return0;
}