`
wangleide414
  • 浏览: 590425 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

Linux C语言常用函数 02

 
阅读更多

 

函数名: detectgraph 

功 能通过检测硬件确定图形驱动程序和模式 

用 法: void far detectgraph(int far *graphdriver, int far *graphmode); 

程序例

#include ; 

#include ; 

#include ; 

#include ; 

/* names of the various cards supported */ 

char *dname[] = { "requests detection", 

"a CGA", 

"an MCGA", 

"an EGA", 

"a 64K EGA", 

"a monochrome EGA", 

"an IBM 8514", 

"a Hercules monochrome", 

"an AT&T 6300 PC", 

"a VGA", 

"an IBM 3270 PC" 

}; 

int main(void) 

/* returns detected hardware info. */ 

int gdriver, gmode, errorcode; 

/* detect graphics hardware available */ 

detectgraph(&gdriver, &gmode); 

/* read result of detectgraph call */ 

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 */ 

/* display the information detected */ 

clrscr(); 

printf("You have %s video display \ 

card.\n", dname[gdriver]); 

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

getch(); 

return 0; 

函数名: difftime 

功 能计算两个时刻之间的时间差 

用 法: double difftime(time_t time2, time_t time1); 

程序例

#include ; 

#include ; 

#include ; 

#include ; 

int main(void) 

time_t first, second; 

clrscr(); 

first = time(NULL); /* Gets system 

time */ 

delay(2000); /* Waits 2 secs */ 

second = time(NULL); /* Gets system time 

again */ 

printf("The difference is: %f \ 

seconds\n",difftime(second,first)); 

getch(); 

return 0; 

函数名: disable 

功 能屏蔽中断 

用 法: void disable(void); 

程序例

/***NOTE: This is an interrupt service 

routine. You cannot compile this program 

with Test Stack Overflow turned on and 

get an executable file that operates 

correctly. */ 

#include ; 

#include ; 

#include ; 

#define INTR 0X1C /* The clock tick 

interrupt */ 

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++; 

/* reenable interrupts at the end of the 

handler */ 

enable(); 

/* call the old routine */ 

oldhandler(); 

int main(void) 

/* save the old interrupt vector */ 

oldhandler = getvect(INTR); 

/* install the new interrupt handler */ 

setvect(INTR, handler); 

/* loop until the counter exceeds 20 */ 

while (count 函数名: div 

功 能将两个整数相除返回商和余数 

用 法: div_t (int number, int denom); 

程序例

#include ; 

#include ; 

div_t x; 

int main(void) 

x = div(10,3); 

printf("10 div 3 = %d remainder %d\n", x.quot, x.rem); 

return 0; 

函数名: dosexterr 

功 能获取扩展DOS错误信息 

用 法: int dosexterr(struct DOSERR *dblkp); 

程序例

#include ; 

#include ; 

int main(void) 

FILE *fp; 

struct DOSERROR info; 

fp = fopen("perror.dat","r"); 

if (!fp) perror("Unable to open file for 

reading"); 

dosexterr(&info); 

printf("Extended DOS error \ 

information:\n"); 

printf(" Extended error: \ 

%d\n",info.exterror); 

printf(" Class: \ 

%x\n",info.class); 

printf(" Action: \ 

%x\n",info.action); 

printf(" Error Locus: \ 

%x\n",info.locus); 

return 0; 

函数名: dostounix 

功 能转换日期和时间为UNIX时间格式 

用 法: long dostounix(struct date *dateptr, struct time *timeptr); 

程序例

#include ; 

#include ; 

#include ; 

#include ; 

int main(void) 

time_t t; 

struct time d_time; 

struct date d_date; 

struct tm *local; 

getdate(&d_date); 

gettime(&d_time); 

t = dostounix(&d_date, &d_time); 

local = localtime(&t); 

printf("Time and Date: %s\n", \ 

asctime(local)); 

return 0; 

函数名: drawpoly 

功 能画多边形 

用 法: void far drawpoly(int numpoints, int far *polypoints); 

程序例

#include ; 

#include ; 

#include ; 

#include ; 

int main(void) 

/* request auto detection */ 

int gdriver = DETECT, gmode, errorcode; 

int maxx, maxy; 

/* our polygon array */ 

int poly[10]; 

/* initialize graphics and 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(); 

/* terminate with an error code */ 

exit(1); 

maxx = getmaxx(); 

maxy = getmaxy(); 

poly[0] = 20; /* 1st vertext */ 

poly[1] = maxy / 2; 

poly[2] = maxx - 20; /* 2nd */ 

poly[3] = 20; 

poly[4] = maxx - 50; /* 3rd */ 

poly[5] = maxy - 20; 

poly[6] = maxx / 2; /* 4th */ 

poly[7] = maxy / 2; 

/* 

drawpoly doesn't automatically close 

the polygon, so we close it. 

*/ 

poly[8] = poly[0]; 

poly[9] = poly[1]; 

/* draw the polygon */ 

drawpoly(5, poly); 

/* clean up */ 

getch(); 

closegraph(); 

return 0; 

函数名: dup 

功 能复制一个文件句柄 

用 法: int dup(int handle); 

程序例

#include ; 

#include ; 

#include ; 

#include ; 

void flush(FILE *stream); 

int main(void) 

FILE *fp; 

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

/* create a file */ 

fp = fopen("DUMMY.FIL", "w"); 

/* write some data to the file */ 

fwrite(msg, strlen(msg), 1, fp); 

clrscr(); 

printf("Press any key to flush \ 

DUMMY.FIL:"); 

getch(); 

/* flush the data to DUMMY.FIL without 

closing it */ 

flush(fp); 

printf("\nFile was flushed, Press any \ 

key to quit:"); 

getch(); 

return 0; 

void flush(FILE *stream) 

int duphandle; 

/* flush TC's internal buffer */ 

fflush(stream); 

/* make a duplicate file handle */ 

duphandle = dup(fileno(stream)); 

/* close the duplicate handle to flush the 

DOS buffer */ 

close(duphandle); 

函数名: dup2 

功 能复制文件句柄 

用 法: int dup2(int oldhandle, int newhandle); 

程序例

#include ; 

#include ; 

#include ; 

#include ; 

int main(void) 

#define STDOUT 1 

int nul, oldstdout; 

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

/* create a file */ 

nul = open("DUMMY.FIL", O_CREAT | O_RDWR, 

S_IREAD | S_IWRITE); 

/* create a duplicate handle for standard 

output */ 

oldstdout = dup(STDOUT); 

/* 

redirect standard output to DUMMY.FIL 

by duplicating the file handle onto the 

file handle for standard output. 

*/ 

dup2(nul, STDOUT); 

/* close the handle for DUMMY.FIL */ 

close(nul); 

/* will be redirected into DUMMY.FIL */ 

write(STDOUT, msg, strlen(msg)); 

/* restore original standard output 

handle */ 

dup2(oldstdout, STDOUT); 

/* close duplicate handle for STDOUT */ 

close(oldstdout); 

return 0; 

函数名: ecvt 

功 能把一个浮点数转换为字符串 

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

程序例

#include ; 

#include ; 

#include ; 

int main(void) 

char *string; 

double value; 

int dec, sign; 

int ndig = 10; 

clrscr(); 

value = http://linux.chinaunix.net/techdoc/develop/2006/07/30/9.876; 

string = ecvt(value, ndig, &dec, &sign); 

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

sign = %d\n", string, dec, sign); 

value = http://linux.chinaunix.net/techdoc/develop/2006/07/30/-123.45; 

ndig= 15; 

string = ecvt(value,ndig,&dec,&sign); 

printf("string = %s dec = %d sign = %d\n", 

string, dec, sign); 

value = http://linux.chinaunix.net/techdoc/develop/2006/07/30/0.6789e5; /* scientific 

notation */ 

ndig = 5; 

string = ecvt(value,ndig,&dec,&sign); 

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

sign = %d\n", string, dec, sign); 

return 0; 

函数名: ellipse 

功 能画一椭圆 

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

int xradius, int yradius); 

程序例

#include ; 

#include ; 

#include ; 

#include ; 

int main(void) 

/* 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 */ 

getch(); 

closegraph(); 

return 0; 

函数名: 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. 

*/ 

#include ; 

#include ; 

#include ; 

/* 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(); 

int main(void) 

/* save the old interrupt vector */ 

oldhandler = getvect(INTR); 

/* install the new interrupt handler */ 

setvect(INTR, handler); 

/* loop until the counter exceeds 20 */ 

while (count 函数名: eof 

功 能检测文件结束 

用 法: int eof(int *handle); 

程序例

#include ; 

#include ; 

#include ; 

#include ; 

#include ; 

int main(void) 

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); 

return 0; 

函数名: 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, 

char *envp[]); 

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 */ 

#include ; 

#include ; 

#include ; 

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

int i; 

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

for (i=0; i函数名: exit 

功 能终止程序 

用 法: void exit(int status); 

程序例

#include ; 

#include ; 

#include ; 

int main(void) 

int status; 

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

status = getch(); 

/* Sets DOS errorlevel */ 

exit(status - '0'); 

/* Note: this line is never reached */ 

return 0; 

函数名: exp 

功 能指数函数 

用 法: double exp(double x); 

程序例

#include ; 

#include ; 

int main(void) 

double result; 

double x = 4.0; 

result = exp(x); 

printf("'e' raised to the power \ 

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

x, x, result); 

return 0; 

 

 

CODE:

 

[Copy to clipboard]

 

函数名: exp 

功 能指数函数 

用 法: double exp(double x); 

程序例

#include ; 

#include ; 

int main(void) 

double result; 

double x = 4.0; 

result = exp(x); 

printf("'e' raised to the power \ 

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

x, x, result); 

return 0; 

函数名: fabs 

功 能返回浮点数的绝对值 

用 法: double fabs(double x); 

程序例

#include ; 

#include ; 

int main(void) 

float number = -1234.0; 

printf("number: %f absolute value: %f\n", 

number, fabs(number)); 

return 0; 

函数名: farcalloc 

功 能从远堆栈中申请空间 

用 法: void far *farcalloc(unsigned long units, unsigned ling unitsz); 

程序例

#include ; 

#include ; 

#include ; 

#include ; 

int main(void) 

char far *fptr; 

char *str = "Hello"; 

/* allocate memory for the far pointer */ 

fptr = farcalloc(10, sizeof(char)); 

/* copy "Hello" into allocated memory */ 

/* 

Note: movedata is used because you 

might be in a small data model, in 

which case a normal string copy routine 

can not be used since it assumes the 

pointer size is near. 

*/ 

movedata(FP_SEG(str), FP_OFF(str), 

FP_SEG(fptr), FP_OFF(fptr), 

strlen(str)); 

/* display string (note the F modifier) */ 

printf("Far string is: %Fs\n", fptr); 

/* free the memory */ 

farfree(fptr); 

return 0; 

函数名: farcoreleft 

功 能返回远堆中未作用存储区大小 

用 法: long farcoreleft(void); 

程序例

#include ; 

#include ; 

int main(void) 

printf("The difference between the\ 

highest allocated block in the\ 

far\n"); 

printf("heap and the top of the far heap\ 

is: %lu bytes\n", farcoreleft()); 

return 0; 

函数名: farfree 

功 能从远堆中释放一块 

用 法: void farfree(void); 

程序例

#include ; 

#include ; 

#include ; 

#include ; 

int main(void) 

char far *fptr; 

char *str = "Hello"; 

/* allocate memory for the far pointer */ 

fptr = farcalloc(10, sizeof(char)); 

/* copy "Hello" into allocated memory */ 

/* 

Note: movedata is used because you might be in a small data model, 

in which case a normal string copy routine can't be used since it 

assumes the pointer size is near. 

*/ 

movedata(FP_SEG(str), FP_OFF(str), 

FP_SEG(fptr), FP_OFF(fptr), 

strlen(str)); 

/* display string (note the F modifier) */ 

printf("Far string is: %Fs\n", fptr); 

/* free the memory */ 

farfree(fptr); 

return 0; 

函数名: farmalloc 

功 能从远堆中分配存储块 

用 法: void far *farmalloc(unsigned long size); 

程序例

#include ; 

#include ; 

#include ; 

#include ; 

int main(void) 

char far *fptr; 

char *str = "Hello"; 

/* allocate memory for the far pointer */ 

fptr = farmalloc(10); 

/* copy "Hello" into allocated memory */ 

/* 

Note: movedata is used because we might 

be in a small data model, in which case 

a normal string copy routine can not be 

used since it assumes the pointer size 

is near. 

*/ 

movedata(FP_SEG(str), FP_OFF(str), 

FP_SEG(fptr), FP_OFF(fptr), 

strlen(str)); 

/* display string (note the F modifier) */ 

printf("Far string is: %Fs\n", fptr); 

/* free the memory */ 

farfree(fptr); 

return 0; 

函数名: farrealloc 

功 能调整远堆中的分配块 

用 法: void far *farrealloc(void far *block, unsigned long newsize); 

程序例

#include ; 

#include ; 

int main(void) 

char far *fptr; 

fptr = farmalloc(10); 

printf("First address: %Fp\n", fptr); 

fptr = farrealloc(fptr,20); 

printf("New address : %Fp\n", fptr); 

farfree(fptr); 

return 0; 

函数名: fclose 

功 能关闭一个流 

用 法: int fclose(FILE *stream); 

程序例

#include ; 

#include ; 

int main(void) 

FILE *fp; 

char buf[11] = "0123456789"; 

/* create a file containing 10 bytes */ 

fp = fopen("DUMMY.FIL", "w"); 

fwrite(&buf, strlen(buf), 1, fp); 

/* close the file */ 

fclose(fp); 

return 0; 

函数名: fcloseall 

功 能关闭打开流 

用 法: int fcloseall(void); 

程序例

#include ; 

int main(void) 

int streams_closed; 

/* open two streams */ 

fopen("DUMMY.ONE", "w"); 

fopen("DUMMY.TWO", "w"); 

/* close the open streams */ 

streams_closed = fcloseall(); 

if (streams_closed == EOF) 

/* issue an error message */ 

perror("Error"); 

else 

/* print result of fcloseall() function */ 

printf("%d streams were closed.\n", streams_closed); 

return 0; 

函数名: fcvt 

功 能把一个浮点数转换为字符串 

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

程序例

#include ; 

#include ; 

#include ; 

int main(void) 

char *string; 

double value; 

int dec, sign; 

int ndig = 10; 

clrscr(); 

value = http://linux.chinaunix.net/techdoc/develop/2006/07/30/9.876; 

string = ecvt(value, ndig, &dec, &sign); 

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

sign = %d\n", string, dec, sign); 

value = http://linux.chinaunix.net/techdoc/develop/2006/07/30/-123.45; 

ndig= 15; 

string = ecvt(value,ndig,&dec,&sign); 

printf("string = %s dec = %d sign = %d\n", 

string, dec, sign); 

value = http://linux.chinaunix.net/techdoc/develop/2006/07/30/0.6789e5; /* scientific 

notation */ 

ndig = 5; 

string = ecvt(value,ndig,&dec,&sign); 

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

sign = %d\n", string, dec, sign); 

return 0; 

函数名: fdopen 

功 能把流与一个文件句柄相接 

用 法: FILE *fdopen(int handle, char *type); 

程序例

#include ; 

#include ; 

#include ; 

#include ; 

int main(void) 

int handle; 

FILE *stream; 

/* open a file */ 

handle = open("DUMMY.FIL", O_CREAT, 

S_IREAD | S_IWRITE); 

/* now turn the handle into a stream */ 

stream = fdopen(handle, "w"); 

if (stream == NULL) 

printf("fdopen failed\n"); 

else 

fprintf(stream, "Hello world\n"); 

fclose(stream); 

return 0; 

函数名: feof 

功 能检测流上的文件结束符 

用 法: int feof(FILE *stream); 

程序例

#include ; 

int main(void) 

FILE *stream; 

/* open a file for reading */ 

stream = fopen("DUMMY.FIL", "r"); 

/* read a character from the file */ 

fgetc(stream); 

/* check for EOF */ 

if (feof(stream)) 

printf("We have reached end-of-file\n"); 

/* close the file */ 

fclose(stream); 

return 0; 

函数名: ferror 

功 能检测流上的错误 

用 法: int ferror(FILE *stream); 

程序例

#include ; 

int main(void) 

FILE *stream; 

/* open a file for writing */ 

stream = fopen("DUMMY.FIL", "w"); 

/* force an error condition by attempting to read */ 

(void) getc(stream); 

if (ferror(stream)) /* test for an error on the stream */ 

/* display an error message */ 

printf("Error reading from DUMMY.FIL\n"); 

/* reset the error and EOF indicators */ 

clearerr(stream); 

fclose(stream); 

return 0; 

函数名: fflush 

功 能清除一个流 

用 法: int fflush(FILE *stream); 

程序例

#include ; 

#include ; 

#include ; 

#include ; 

void flush(FILE *stream); 

int main(void) 

FILE *stream; 

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

/* create a file */ 

stream = fopen("DUMMY.FIL", "w"); 

/* write some data to the file */ 

fwrite(msg, strlen(msg), 1, stream); 

clrscr(); 

printf("Press any key to flush\ 

DUMMY.FIL:"); 

getch(); 

/* flush the data to DUMMY.FIL without\ 

closing it */ 

flush(stream); 

printf("\nFile was flushed, Press any key\ 

to quit:"); 

getch(); 

return 0; 

void flush(FILE *stream) 

int duphandle; 

/* flush the stream's internal buffer */ 

fflush(stream); 

/* make a duplicate file handle */ 

duphandle = dup(fileno(stream)); 

/* close the duplicate handle to flush\ 

the DOS buffer */ 

close(duphandle); 

函数名: fgetc 

功 能从流中读取字符 

用 法: int fgetc(FILE *stream); 

程序例

#include ; 

#include ; 

#include ; 

int main(void) 

FILE *stream; 

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

char ch; 

/* open a file for update */ 

stream = fopen("DUMMY.FIL", "w+"); 

/* write a string into the file */ 

fwrite(string, strlen(string), 1, stream); 

/* seek to the beginning of the file */ 

fseek(stream, 0, SEEK_SET); 

do 

/* read a char from the file */ 

ch = fgetc(stream); 

/* display the character */ 

putch(ch); 

} while (ch != EOF); 

fclose(stream); 

return 0; 

函数名: fgetchar 

功 能从流中读取字符 

用 法: int fgetchar(void); 

程序例

#include ; 

int main(void) 

char ch; 

/* prompt the user for input */ 

printf("Enter a character followed by \ 

;: "); 

/* read the character from stdin */ 

ch = fgetchar(); 

/* display what was read */ 

printf("The character read is: '%c'\n", 

ch); 

return 0; 

函数名: fgetpos 

功 能取得当前文件的句柄 

用 法: int fgetpos(FILE *stream); 

程序例

#include ; 

#include ; 

int main(void) 

FILE *stream; 

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

fpos_t filepos; 

/* open a file for update */ 

stream = fopen("DUMMY.FIL", "w+"); 

/* write a string into the file */ 

fwrite(string, strlen(string), 1, stream); 

/* report the file pointer position */ 

fgetpos(stream, &filepos); 

printf("The file pointer is at byte\ 

%ld\n", filepos); 

fclose(stream); 

return 0; 

函数名: fgets 

功 能从流中读取一字符串 

用 法: char *fgets(char *string, int n, FILE *stream); 

程序例

#include ; 

#include ; 

int main(void) 

FILE *stream; 

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

char msg[20]; 

/* open a file for update */ 

stream = fopen("DUMMY.FIL", "w+"); 

/* write a string into the file */ 

fwrite(string, strlen(string), 1, stream); 

/* seek to the start of the file */ 

fseek(stream, 0, SEEK_SET); 

/* read a string from the file */ 

fgets(msg, strlen(string)+1, stream); 

/* display the string */ 

printf("%s", msg); 

fclose(stream); 

return 0; 

函数名: filelength 

功 能取文件长度字节数 

用 法: long filelength(int handle); 

程序例

#include ; 

#include ; 

#include ; 

#include ; 

int main(void) 

int handle; 

char buf[11] = "0123456789"; 

/* create a file containing 10 bytes */ 

handle = open("DUMMY.FIL", O_CREAT); 

write(handle, buf, strlen(buf)); 

/* display the size of the file */ 

printf("file length in bytes: %ld\n", 

filelength(handle)); 

/* close the file */ 

close(handle); 

return 0; 

函数名: fillellipse 

功 能画出并填充一椭圆 

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

程序例

#include ; 

#include ; 

int main(void) 

int gdriver = DETECT, gmode; 

int xcenter, ycenter, i; 

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

xcenter = getmaxx() / 2; 

ycenter = getmaxy() / 2; 

for (i=0; i函数名: fillpoly 

功 能画并填充一个多边形 

用 法: void far fillpoly(int numpoints, int far *polypoints); 

程序例

#include ; 

#include ; 

#include ; 

#include ; 

int main(void) 

/* request auto detection */ 

int gdriver = DETECT, gmode, errorcode; 

int i, maxx, maxy; 

/* our polygon array */ 

int poly[8]; 

/* 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 */ 

maxx = getmaxx(); 

maxy = getmaxy(); 

poly[0] = 20; /* 1st vertext */ 

poly[1] = maxy / 2; 

poly[2] = maxx - 20; /* 2nd */ 

poly[3] = 20; 

poly[4] = maxx - 50; /* 3rd */ 

poly[5] = maxy - 20; 

/* 

4th vertex. fillpoly automatically 

closes the polygon. 

*/ 

poly[6] = maxx / 2; 

poly[7] = maxy / 2; 

/* loop through the fill patterns */ 

for (i=EMPTY_FILL; i函数名: findfirst, findnext 

功 能搜索磁盘目录取得下一个匹配的findfirst模式的文件 

用 法: int findfirst(char *pathname, struct ffblk *ffblk, int attrib); 

int findnext(struct ffblk *ffblk); 

程序例

/* findnext example */ 

#include ; 

#include ; 

int main(void) 

struct ffblk ffblk; 

int done; 

printf("Directory listing of *.*\n"); 

done = findfirst("*.*",&ffblk,0); 

while (!done) 

printf(" %s\n", ffblk.ff_name); 

done = findnext(&ffblk); 

return 0; 

函数名: floodfill 

功 能填充一个有界区域 

用 法: void far floodfill(int x, int y, int border); 

程序例

#include ; 

#include ; 

#include ; 

#include ; 

int main(void) 

/* request auto detection */ 

int gdriver = DETECT, gmode, errorcode; 

int maxx, maxy; 

/* 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 */ 

maxx = getmaxx(); 

maxy = getmaxy(); 

/* select drawing color */ 

setcolor(getmaxcolor()); 

/* select fill color */ 

setfillstyle(SOLID_FILL, getmaxcolor()); 

/* draw a border around the screen */ 

rectangle(0, 0, maxx, maxy); 

/* draw some circles */ 

circle(maxx / 3, maxy /2, 50); 

circle(maxx / 2, 20, 100); 

circle(maxx-20, maxy-50, 75); 

circle(20, maxy-20, 25); 

/* wait for a key */ 

getch(); 

/* fill in bounded region */ 

floodfill(2, 2, getmaxcolor()); 

/* clean up */ 

getch(); 

closegraph(); 

return 0; 

函数名: floor 

功 能向下舍入 

用 法: double floor(double x); 

程序例

#include ; 

#include ; 

int main(void) 

double number = 123.54; 

double down, up; 

down = floor(number); 

up = ceil(number); 

printf("original number %10.2lf\n", 

number); 

printf("number rounded down %10.2lf\n", 

down); 

printf("number rounded up %10.2lf\n", 

up); 

return 0; 

函数名: flushall 

功 能清除所有缓冲区 

用 法: int flushall(void); 

程序例

#include ; 

int main(void) 

FILE *stream; 

/* create a file */ 

stream = fopen("DUMMY.FIL", "w"); 

/* flush all open streams */ 

printf("%d streams were flushed.\n", 

flushall()); 

/* close the file */ 

fclose(stream); 

return 0; 

函数名: fmod 

功 能计算xy的模x/y的余数 

用 法: double fmod(double x, double y); 

程序例

#include ; 

#include ; 

int main(void) 

double x = 5.0, y = 2.0; 

double result; 

result = fmod(x,y); 

printf("The remainder of (%lf / %lf) is \ 

%lf\n", x, y, result); 

return 0; 

函数名: fnmerge 

功 能建立新文件名 

用 法: void fnerge(char *path, char *drive, char *dir); 

程序例

#include ; 

#include ; 

#include ; 

int main(void) 

char s[MAXPATH]; 

char drive[MAXDRIVE]; 

char dir[MAXDIR]; 

char file[MAXFILE]; 

char ext[MAXEXT]; 

getcwd(s,MAXPATH); /* get the current working directory */ 

strcat(s,"\\"); /* append on a trailing \ character */ 

fnsplit(s,drive,dir,file,ext); /* split the string to separate elems */ 

strcpy(file,"DATA"); 

strcpy(ext,".TXT"); 

fnmerge(s,drive,dir,file,ext); /* merge everything into one string */ 

puts(s); /* display resulting string */ 

return 0; 

函数名: fopen 

功 能打开一个流 

用 法: FILE *fopen(char *filename, char *type); 

程序例

#include ; 

#include ; 

#include ; 

int main(void) 

char *s; 

char drive[MAXDRIVE]; 

char dir[MAXDIR]; 

char file[MAXFILE]; 

char ext[MAXEXT]; 

int flags; 

s=getenv("COMSPEC"); /* get the comspec environment parameter */ 

flags=fnsplit(s,drive,dir,file,ext); 

printf("Command processor info:\n"); 

if(flags & DRIVE) 

printf("\tdrive: %s\n",drive); 

if(flags & DIRECTORY) 

printf("\tdirectory: %s\n",dir); 

if(flags & FILENAME) 

printf("\tfile: %s\n",file); 

if(flags & EXTENSION) 

printf("\textension: %s\n",ext); 

return 0; 

函数名: fprintf 

功 能传送格式化输出到一个流中 

用 法: int fprintf(FILE *stream, char *format[, argument,...]); 

程序例

/* Program to create backup of the 

AUTOEXEC.BAT file */ 

#include ; 

int main(void) 

FILE *in, *out; 

if ((in = fopen("\\AUTOEXEC.BAT", "rt")) 

== NULL) 

fprintf(stderr, "Cannot open input \ 

file.\n"); 

return 1; 

if ((out = fopen("\\AUTOEXEC.BAK", "wt")) 

== NULL) 

fprintf(stderr, "Cannot open output \ 

file.\n"); 

return 1; 

while (!feof(in)) 

fputc(fgetc(in), out); 

fclose(in); 

fclose(out); 

return 0; 

函数名: FP_OFF 

功 能获取远地址偏移量 

用 法: unsigned FP_OFF(void far *farptr); 

程序例

/* FP_OFF */ 

#include ; 

#include ; 

int main(void) 

char *str = "fpoff.c"; 

printf("The offset of this file in memory\ 

is: %Fp\n", FP_OFF(str)); 

return 0; 

函数名: FP_SEG 

功 能获取远地址段值 

用 法: unsigned FP_SEG(void far *farptr); 

程序例

/* FP_SEG */ 

#include ; 

#include ; 

int main(void) 

char *filename = "fpseg.c"; 

printf("The offset of this file in memory\ 

is: %Fp\n", FP_SEG(filename)); 

return(0); 

函数名: fputc 

功 能送一个字符到一个流中 

用 法: int fputc(int ch, FILE *stream); 

程序例

#include ; 

int main(void) 

char msg[] = "Hello world"; 

int i = 0; 

while (msg) 

fputc(msg, stdout); 

i++; 

return 0; 

函数名: fputchar 

功 能送一个字符到标准输出流(stdout)中 

用 法: int fputchar(char ch); 

程序例

#include ; 

int main(void) 

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

int i = 0; 

while (msg) 

fputchar(msg); 

i++; 

return 0; 

函数名: fputs 

功 能送一个字符到一个流中 

用 法: int fputs(char *string, FILE *stream); 

程序例

#include ; 

int main(void) 

/* write a string to standard output */ 

fputs("Hello world\n", stdout); 

return 0; 

函数名: fread 

功 能从一个流中读数据 

用 法: int fread(void *ptr, int size, int nitems, FILE *stream); 

程序例

#include ; 

#include ; 

int main(void) 

FILE *stream; 

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

char buf[20]; 

if ((stream = fopen("DUMMY.FIL", "w+")) 

== NULL) 

fprintf(stderr, 

"Cannot open output file.\n"); 

return 1; 

/* write some data to the file */ 

fwrite(msg, strlen(msg)+1, 1, stream); 

/* seek to the beginning of the file */ 

fseek(stream, SEEK_SET, 0); 

/* read the data and display it */ 

fread(buf, strlen(msg)+1, 1, stream); 

printf("%s\n", buf); 

fclose(stream); 

return 0; 

函数名: free 

功 能释放已分配的块 

用 法: void free(void *ptr); 

程序例

#include ; 

#include ; 

#include ; 

int main(void) 

char *str; 

/* allocate memory for string */ 

str = malloc(10); 

/* copy "Hello" to string */ 

strcpy(str, "Hello"); 

/* display string */ 

printf("String is %s\n", str); 

/* free memory */ 

free(str); 

return 0; 

函数名: freemem 

功 能释放先前分配的DOS内存块 

用 法: int freemem(unsigned seg); 

程序例

#include ; 

#include ; 

#include ; 

int main(void) 

unsigned int size, segp; 

int stat; 

size = 64; /* (64 x 16) = 1024 bytes */ 

stat = allocmem(size, &segp); 

if (stat 函数名: freopen 

功 能替换一个流 

用 法: FILE *freopen(char *filename, char *type, FILE *stream); 

程序例

#include ; 

int main(void) 

/* redirect standard output to a file */ 

if (freopen("OUTPUT.FIL", "w", stdout) 

== NULL) 

fprintf(stderr, "error redirecting\ 

stdout\n"); 

/* this output will go to a file */ 

printf("This will go into a file."); 

/* close the standard output stream */ 

fclose(stdout); 

return 0; 

函数名: frexp 

功 能把一个双精度数分解为尾数的指数 

用 法: double frexp(double value, int *eptr); 

程序例

#include ; 

#include ; 

int main(void) 

double mantissa, number; 

int exponent; 

number = 8.0; 

mantissa = frexp(number, &exponent); 

printf("The number %lf is ", number); 

printf("%lf times two to the ", mantissa); 

printf("power of %d\n", exponent); 

return 0; 

函数名: fscanf 

功 能从一个流中执行格式化输入 

用 法: int fscanf(FILE *stream, char *format[,argument...]); 

程序例

#include ; 

#include ; 

int main(void) 

int i; 

printf("Input an integer: "); 

/* read an integer from the 

standard input stream */ 

if (fscanf(stdin, "%d", &i)) 

printf("The integer read was: %i\n", 

i); 

else 

fprintf(stderr, "Error reading an \ 

integer from stdin.\n"); 

exit(1); 

return 0; 

函数名: fseek 

功 能重定位流上的文件指针 

用 法: int fseek(FILE *stream, long offset, int fromwhere); 

程序例

#include ; 

long filesize(FILE *stream); 

int main(void) 

FILE *stream; 

stream = fopen("MYFILE.TXT", "w+"); 

fprintf(stream, "This is a test"); 

printf("Filesize of MYFILE.TXT is %ld bytes\n", filesize(stream)); 

fclose(stream); 

return 0; 

long filesize(FILE *stream) 

long curpos, length; 

curpos = ftell(stream); 

fseek(stream, 0L, SEEK_END); 

length = ftell(stream); 

fseek(stream, curpos, SEEK_SET); 

return length; 

函数名: fsetpos 

功 能定位流上的文件指针 

用 法: int fsetpos(FILE *stream, const fpos_t *pos); 

程序例

#include ; 

#include ; 

void showpos(FILE *stream); 

int main(void) 

FILE *stream; 

fpos_t filepos; 

/* open a file for update */ 

stream = fopen("DUMMY.FIL", "w+"); 

/* save the file pointer position */ 

fgetpos(stream, &filepos); 

/* write some data to the file */ 

fprintf(stream, "This is a test"); 

/* show the current file position */ 

showpos(stream); 

/* set a new file position, display it */ 

if (fsetpos(stream, &filepos) == 0) 

showpos(stream); 

else 

fprintf(stderr, "Error setting file \ 

pointer.\n"); 

exit(1); 

/* close the file */ 

fclose(stream); 

return 0; 

void showpos(FILE *stream) 

fpos_t pos; 

/* display the current file pointer 

position of a stream */ 

fgetpos(stream, &pos); 

printf("File position: %ld\n", pos); 

函数名: fstat 

功 能获取打开文件信息 

用 法: int fstat(char *handle, struct stat *buff); 

程序例

#include ; 

#include ; 

#include ; 

int main(void) 

struct stat statbuf; 

FILE *stream; 

/* open a file for update */ 

if ((stream = fopen("DUMMY.FIL", "w+")) 

== NULL) 

fprintf(stderr, "Cannot open output \ 

file.\n"); 

return(1); 

fprintf(stream, "This is a test"); 

fflush(stream); 

/* get information about the file */ 

fstat(fileno(stream), &statbuf); 

fclose(stream); 

/* display the information returned */ 

if (statbuf.st_mode & S_IFCHR) 

printf("Handle refers to a device.\n"); 

if (statbuf.st_mode & S_IFREG) 

printf("Handle refers to an ordinary \ 

file.\n"); 

if (statbuf.st_mode & S_IREAD) 

printf("User has read permission on \ 

file.\n"); 

if (statbuf.st_mode & S_IWRITE) 

printf("User has write permission on \ 

file.\n"); 

printf("Drive letter of file: %c\n", 

'A'+statbuf.st_dev); 

printf("Size of file in bytes: %ld\n", 

statbuf.st_size); 

printf("Time file last opened: %s\n", 

ctime(&statbuf.st_ctime)); 

return 0; 

函数名: ftell 

功 能返回当前文件指针 

用 法: long ftell(FILE *stream); 

程序例

#include ; 

int main(void) 

FILE *stream; 

stream = fopen("MYFILE.TXT", "w+"); 

fprintf(stream, "This is a test"); 

printf("The file pointer is at byte \ 

%ld\n", ftell(stream)); 

fclose(stream); 

return 0; 

函数名: fwrite 

功 能写内容到流中 

用 法: int fwrite(void *ptr, int size, int nitems, FILE *stream); 

程序例

#include ; 

struct mystruct 

int i; 

char ch; 

}; 

int main(void) 

FILE *stream; 

struct mystruct s; 

if ((stream = fopen("TEST.$$$", "wb")) == NULL) /* open file TEST.$$$ */ 

fprintf(stderr, "Cannot open output file.\n"); 

return 1; 

s.i = 0; 

s.ch = 'A'; 

fwrite(&s, sizeof(s), 1, stream); /* write struct s to file */ 

fclose(stream); /* close file */ 

return 0; 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics