天天看點

C語言庫函數大全及應用執行個體三

函數名: ecvt

功 能: 把一個浮點數轉換為字元串

用 法: char ecvt(double value, int ndigit, int *decpt, int *sign);

程式例: <?xml:namespace prefix="o" ns="urn:schemas-microsoft-com:office:office"?>

#i nclude

#i nclude .h>.h>.h>

int main(void)

{

char *string;

double value;

int dec, sign;

int ndig = 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 = %s dec = %d sign = %d\n",

string, dec, sign);

value = 0.6789e5; /* scientific

notation */

ndig = 5;

printf("string = %s dec = %d\

return 0;

}

函數名: ellipse

功 能: 畫一橢圓

用 法: void far ellipse(int x, int y, int stangle, int endangle,

int xradius, int yradius);

程式例:

#i nclude .h>.h>.h>.h>

/* request auto detection */

int gdriver = DETECT, gmode, errorcode;

int midx, midy;

int stangle = 0, endangle = 360;

int xradius = 100, yradius = 50;

/* initialize graphics, local variables */

initgraph(&gdriver, &gmode, "");

/* read result of initialization */

errorcode = graphresult();

if (errorcode != grOk)

/* an error occurred */

printf("Graphics error: %s\n",

grapherrormsg(errorcode));

printf("Press any key to halt:");

getch();

exit(1);

/* terminate with an error code */

midx = getmaxx() / 2;

midy = getmaxy() / 2;

setcolor(getmaxcolor());

/* draw ellipse */

ellipse(midx, midy, stangle, endangle,

xradius, yradius);

/* clean up */

closegraph();

函數名: enable

功 能: 開放硬體中斷

用 法: void enable(void);

/* ** NOTE:

This is an interrupt service routine. You can NOT compile this program

with Test Stack Overflow turned on and get an executable file which will

operate correctly.

*/

/* The clock tick interrupt */

#define INTR 0X1C

void interrupt ( *oldhandler)(void);

int count=0;

void interrupt handler(void)

/*

disable interrupts during the handling of the interrupt

disable();

/* increase the global counter */

count++;

re enable interrupts at the end of the handler

enable();

/* call the old routine */

oldhandler();

/* save the old interrupt vector */

oldhandler = getvect(INTR);

/* install the new interrupt handler */

setvect(INTR, handler);

/* loop until the counter exceeds 20 */

while (count < 20)

printf("count is %d\n",count);

/* reset the old interrupt handler */

setvect(INTR, oldhandler);

函數名: eof

功 能: 檢測檔案結束

用 法: int eof(int *handle);

#i nclude .h>.h>.h>.h>\stat.h>

int handle;

char msg[] = "This is a test";

char ch;

/* create a file */

handle = open("DUMMY.FIL",

O_CREAT | O_RDWR,

S_IREAD | S_IWRITE);

/* write some data to the file */

write(handle, msg, strlen(msg));

/* seek to the beginning of the file */

lseek(handle, 0L, SEEK_SET);

reads chars from the file until hit EOF

do

read(handle, &ch, 1);

printf("%c", ch);

} while (!eof(handle));

close(handle);

函數名: exec...

功 能: 裝入并運作其它程式的函數

用 法: int execl(char *pathname, char *arg0, arg1, ..., argn, NULL);

int execle(char *pathname, char *arg0, arg1, ..., argn, NULL,

char *envp[]);

int execlp(char *pathname, char *arg0, arg1, .., NULL);

int execple(char *pathname, char *arg0, arg1, ..., NULL,

int execv(char *pathname, char *argv[]);

int execve(char *pathname, char *argv[], char *envp[]);

int execvp(char *pathname, char *argv[]);

int execvpe(char *pathname, char *argv[], char *envp[]);

/* execv example */

void main(int argc, char *argv[])

int i;

printf("Command line arguments:\n");

for (i=0; i

printf("[%2d] : %s\n", i, argv[i]); ;>

printf("About to exec child with arg1 arg2 ...\n");

execv("CHILD.EXE", argv);

perror("exec error");

函數名: exit

功 能: 終止程式

用 法: void exit(int status);

int status;

printf("Enter either 1 or 2\n");

status = getch();

/* Sets DOS errorlevel */

exit(status - '0');

/* Note: this line is never reached */

函數名: exp

功 能: 指數函數

用 法: double exp(double x);

#i nclude .h>.h>

double result;

double x = 4.0;

result = exp(x);

printf("'e' raised to the power \

of %lf (e ^ %lf) = %lf\n",

x, x, result);

double fabs(double x);

傳回雙精度x的絕對值。

void far *farcalloc(unsigned long nunits,unsigned long unitsz);

堆中給含有nu從遠nits個元素的,每個元素占用unitsz個位元組長的數組配置設定存貯區。

成功是傳回指向新配置設定的記憶體塊的指針;若存貯空間不夠,傳回NULL。

unsigned long farcoreleft(void);

傳回遠堆中未用存貯區的大小。

void farfree(void far *block);

釋放遠堆中以前所配置設定記憶體塊。

void far *farmalloc(unsigned long nbytes);

從遠堆配置設定長nbytes位元組的記憶體塊,傳回新位址。

void far *farrealloc(void far *oldblock,unsigned long nbytes);

調整已配置設定的記憶體塊的大小為nbytes。需要的話,可把塊中的内容複制到新位置。要注意:所有的可用的RAM可被配置設定,大于64K的塊可被配置設定。

遠指針用于存取被配置設定的塊。傳回重新配置設定的記憶體塊的位址。若存貯塊重新配置設定失敗,傳回NULL。

struct fcb {

char fcb_drive; /* 0 = default, 1 = A, 2 = B */

char fcb_name[8]; /* File name */

char fcb_ext[3]; /* File extension */

short fcb_curblk; /* Current block number */

short fcb_recsize; /* Logical record size in bytes */

long fcb_filsize; /* File size in bytes */

short fcb_date; /* Date file was last written */

char fcb_resv[10]; /* Reserved for DOS */

char fcb_currec; /* Current record in block */

long fcb_random; /* Random record number */

};

int fclose(FILE *stream);

關閉一個流。

成功傳回0;失敗是傳回EOF。

int fcloseall(void);

關閉所有打開的流,除了stdin,stdout,stdprn,stderr和stdaux。

char *fcvt(double value,int ndig,int *dec,int *sign);

把浮點數轉換成字元串,把浮點數value轉換成長度為ndig的以空字元終結的字元串,傳回一個指向這個字元串的指針,相對于串的開始處,

小數點的位置,由dec間接存貯,dec若為負值,表示小數點在傳回的字元串的左邊。傳回的字元串本身不帶小數點。如果value的符号為負,由sign指向的值非零;否則它是零。

FILE *fdopen(int handle,char *type);

把流與一個檔案描述字相聯系地打開。fdopen使流stream與一個從creat,dup,dup2或open得到的檔案描述字相聯系。流的類型type必須與打開檔案描述字handle的模式相比對。

類型字元串type可以是下列值之一:

r,打開用于隻讀;

w,建立用于寫;

a,打開用于寫在原有内容後面,檔案不存在時建立用于寫;

r+,打開已存在的檔案用于更新(讀和寫);

a+,添加打開,檔案不存在時建立,在末尾更新。成功時傳回新打開的流。出錯時傳回NULL。

int feof(FILE *stream);

測試所給stream的檔案尾标記的宏。

若檢測到檔案尾标記EOF或Ctrl-z傳回非零值;否則,傳回0。

int ferror(FILE *stream);

測試給定流讀寫錯誤的宏。

若檢測到給定流上的錯誤傳回非0值。

struct ffblk {

char ff_reserved[21];

char ff_attrib;

unsigned ff_ftime;

unsigned ff_fdate;

long ff_fsize;

char ff_name[13];

int fflush(FILE *stream);

清除輸入流的緩沖區,使它仍然打開,并把輸出流的緩沖區的内容寫入它所聯系的檔案中。成功時傳回0,出錯時傳回EOF。

int fgetc(FILE *stream);

從流中讀取下一個字元。

成功是傳回輸入流中的下一個字元;至檔案結束或出錯時傳回EOF。

int fgetchar(void);

從标準輸入流中讀取字元,時定義為getc(stdin)的宏。

傳回輸入流stdin中的下一個字元,它已被轉換成為無符号擴充的整形值。遇到出錯或檔案結束時傳回EOF。

int fgetpos(FILE stream,fpos_t *pos);

取得目前檔案指針。

fgetpos把與stream相聯系的檔案指針的位置儲存在pos所指的地方。

其中,類型fpos_t在stdio.h中定義為

typeddf long fpos_t;

成功時傳回0;失敗時,傳回非0值。

char *fgets(char *s,int n,FILE *stream);

成行讀。

從流stream讀n-1個字元,或遇換行符'\n'為止,把讀出的内容,存入s中。與gets不同,fgets在s未尾保留換行符。一個空位元組被加入到s,用來标記串的結束。

成功時傳回s所指的字元串;在出錯或遇到檔案結束時傳回NULL。

long filelength(int handle);

傳回與handle相聯系的檔案長度的位元組數,出錯時傳回-1L。

int fileno(FILE *stream);

傳回與stream相聯系的檔案描述字。

enum fill_patterns { /* Fill patterns for get/setfillstyle */

0 EMPTY_FILL, /* fills area in background color */

1 SOLID_FILL, /* fills area in solid fill color */

2 LINE_FILL, /* --- fill */

3 LTSLASH_FILL, /* /// fill */

4 SLASH_FILL, /* /// fill with thick lines */

5 BKSLASH_FILL, /* \\\ fill with thick lines */

6 LTBKSLASH_FILL, /* \\\ fill */

7 HATCH_FILL, /* light hatch fill */

8 XHATCH_FILL, /* heavy cross hatch fill */

9 INTERLEAVE_FILL, /* interleaving line fill */

10 WIDE_DOT_FILL, /* Widely spaced dot fill */

11 CLOSE_DOT_FILL, /* Closely spaced dot fill */

12 USER_FILL /* user defined fill */

void far fillellipse(int x,int y,int xradius,int yradius);

畫一填充橢圓。

以(x,y)為中心,以xradius和yradius為水準和垂直半軸,用目前顔色畫邊線,畫一橢圓,用目前填充顔色和填充方式填充。

int findfirst(const char *pathname,struct ffblk *ffblk,int attrib);

搜尋磁盤目錄。開始通過DOS系統調用0x4E對磁盤目錄進行搜尋。pathname中可含有要找的盤區路徑檔案名。

檔案名中可含有通配符(如*或?)。如果找到了比對的檔案,把檔案目錄資訊填入ffblk結構。

attrib是MS-DOS的檔案屬性位元組,用于在搜尋過程中選擇符合條件的檔案。

attrib可以是在dos.h中定義的下列可取值之一:FA_RDONLY,隻讀;FA_HIDDEN隐藏;FA_SYSTEM系統檔案;FA_LABEL卷标;FA_DIREC,目錄;FA_ARCH,檔案.可參考>.

結構ffblk的格式如下:

struct ffblk{

char ff_reserved[21}; /*由DOS保留*/

char ff_attrib; /*屬性查找*/

int ff_ftime; /*檔案時間*/

int f_fdate; /*檔案日期*/

long ff_fsize; /*檔案大小*/

char ff_name[13}; /*找到的檔案名*/

在成功的地找到了與搜尋路徑pathname相比對的檔案名後傳回0;否則傳回-1。

int findnext(xtruct ffblk *ffblk);繼續按findfirst的pathname搜尋磁盤目錄。

成功地找到了與搜尋路徑pathname相比對的後續檔案名後傳回0;否則傳回-1。

void far floodfill(int x,int y, int border);

填充一個有界的區域。

double floor(double x);

傳回〈=x的用雙精度浮點數表示的最大整數。

int flushall(void);

清除所有緩沖區。

清除所有與打開輸入流相聯系的緩沖區,并把所有和打開輸出流相聯系的緩沖區的内容寫入到各自的檔案中,跟在flushall後面的讀操作,從輸入檔案中讀新資料到緩沖區中。

傳回一個表示打開輸入流和輸出流總數的整數。

繼續閱讀