📄 王大刚--c语言编程宝典--f.htm
字号:
<BR> fprintf(stream, "Hello world\n");
<BR> fclose(stream); <BR> }
<BR> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: feof <BR>功 能: 检测流上的文件结束符 <BR>用 法: int feof(FILE
*stream); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>
<P>int main(void) <BR>{ <BR> FILE *stream; <BR>
<P> /* open a file for reading */ <BR> stream =
fopen("DUMMY.FIL", "r"); <BR>
<P> /* read a character from the file */ <BR>
fgetc(stream); <BR>
<P> /* check for EOF */ <BR> if (feof(stream))
<BR> printf("We have reached
end-of-file\n"); <BR>
<P> /* close the file */ <BR> fclose(stream);
<BR> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: ferror <BR>功 能: 检测流上的错误 <BR>用 法: int ferror(FILE
*stream); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>
<P>int main(void) <BR>{ <BR> FILE *stream; <BR>
<P> /* open a file for writing */ <BR> stream =
fopen("DUMMY.FIL", "w"); <BR>
<P> /* force an error condition by attempting to read */
<BR> (void) getc(stream); <BR>
<P> if (ferror(stream)) /* test for an error on the
stream */ <BR> { <BR> /* display
an error message */ <BR> printf("Error
reading from DUMMY.FIL\n"); <BR>
<P> /* reset the error and EOF indicators */
<BR> clearerr(stream); <BR> }
<BR>
<P> fclose(stream); <BR> return 0; <BR>}
<BR> <BR> <BR> <BR>
<P>函数名: fflush <BR>功 能: 清除一个流 <BR>用 法: int fflush(FILE
*stream); <BR>程序例: <BR>
<P>#include <string.h> <BR>#include <stdio.h> <BR>#include
<conio.h> <BR>#include <io.h> <BR>
<P>void flush(FILE *stream); <BR>
<P>int main(void) <BR>{ <BR> FILE *stream; <BR>
char msg[] = "This is a test"; <BR>
<P> /* create a file */ <BR> stream =
fopen("DUMMY.FIL", "w"); <BR>
<P> /* write some data to the file */ <BR>
fwrite(msg, strlen(msg), 1, stream); <BR>
<P> clrscr(); <BR> printf("Press any key to flush\
<BR> DUMMY.FIL:"); <BR> getch(); <BR>
<P> /* flush the data to DUMMY.FIL without\
<BR> closing it */ <BR>
flush(stream); <BR>
<P> printf("\nFile was flushed, Press any key\
<BR> to quit:"); <BR> getch(); <BR>
return 0; <BR>} <BR>
<P>void flush(FILE *stream) <BR>{ <BR> int
duphandle; <BR>
<P> /* flush the stream's internal buffer */
<BR> fflush(stream); <BR>
<P> /* make a duplicate file handle */
<BR> duphandle = dup(fileno(stream)); <BR>
<P> /* close the duplicate handle to flush\
<BR> the DOS buffer */
<BR> close(duphandle); <BR>} <BR> <BR>
<BR> <BR>
<P>函数名: fgetc <BR>功 能: 从流中读取字符 <BR>用 法: int fgetc(FILE
*stream); <BR>程序例: <BR>
<P>#include <string.h> <BR>#include <stdio.h> <BR>#include
<conio.h> <BR>
<P>int main(void) <BR>{ <BR> FILE *stream; <BR>
char string[] = "This is a test"; <BR> char ch; <BR>
<P> /* open a file for update */ <BR> stream =
fopen("DUMMY.FIL", "w+"); <BR>
<P> /* write a string into the file */ <BR>
fwrite(string, strlen(string), 1, stream); <BR>
<P> /* seek to the beginning of the file */ <BR>
fseek(stream, 0, SEEK_SET); <BR>
<P> do <BR> { <BR>
/* read a char from the file */ <BR> ch =
fgetc(stream); <BR>
<P> /* display the character */
<BR> putch(ch); <BR> } while (ch
!= EOF); <BR>
<P> fclose(stream); <BR> return 0; <BR>}
<BR> <BR> <BR> <BR>
<P>函数名: fgetchar <BR>功 能: 从流中读取字符 <BR>用 法: int fgetchar(void);
<BR>程序例: <BR>
<P>#include <stdio.h> <BR>
<P>int main(void) <BR>{ <BR> char ch; <BR>
<P> /* prompt the user for input */ <BR>
printf("Enter a character followed by \ <BR> <Enter>:
"); <BR>
<P> /* read the character from stdin */ <BR> ch =
fgetchar(); <BR>
<P> /* display what was read */ <BR> printf("The
character read is: '%c'\n",
<BR> ch);
<BR> return 0; <BR>} <BR> <BR> <BR> <BR>
<P>函数名: fgetpos <BR>功 能: 取得当前文件的句柄 <BR>用 法: int fgetpos(FILE
*stream); <BR>程序例: <BR>
<P>#include <string.h> <BR>#include <stdio.h> <BR>
<P>int main(void) <BR>{ <BR> FILE *stream; <BR>
char string[] = "This is a test"; <BR> fpos_t filepos; <BR>
<P> /* open a file for update */ <BR> stream =
fopen("DUMMY.FIL", "w+"); <BR>
<P> /* write a string into the file */ <BR>
fwrite(string, strlen(string), 1, stream); <BR>
<P> /* report the file pointer position */ <BR>
fgetpos(stream, &filepos); <BR> printf("The file pointer
is at byte\ <BR>
%ld\n", filepos); <BR>
<P> fclose(stream); <BR> return 0; <BR>}
<BR> <BR> <BR> <BR>
<P>函数名: fgets <BR>功 能: 从流中读取一字符串 <BR>用 法: char *fgets(char
*string, int n, FILE *stream); <BR>程序例: <BR>
<P>#include <string.h> <BR>#include <stdio.h> <BR>
<P>int main(void) <BR>{ <BR> FILE *stream; <BR>
char string[] = "This is a test"; <BR> char msg[20]; <BR>
<P> /* open a file for update */ <BR> stream =
fopen("DUMMY.FIL", "w+"); <BR>
<P> /* write a string into the file */ <BR>
fwrite(string, strlen(string), 1, stream); <BR>
<P> /* seek to the start of the file */ <BR>
fseek(stream, 0, SEEK_SET); <BR>
<P> /* read a string from the file */ <BR>
fgets(msg, strlen(string)+1, stream); <BR>
<P> /* display the string */ <BR> printf("%s",
msg); <BR>
<P> fclose(stream); <BR> return 0; <BR>}
<BR> <BR> <BR> <BR>
<P>函数名: filelength <BR>功 能: 取文件长度字节数 <BR>用 法: long
filelength(int handle); <BR>程序例: <BR>
<P>#include <string.h> <BR>#include <stdio.h> <BR>#include
<fcntl.h> <BR>#include <io.h> <BR>
<P>int main(void) <BR>{ <BR> int handle; <BR> char
buf[11] = "0123456789"; <BR>
<P> /* create a file containing 10 bytes */ <BR>
handle = open("DUMMY.FIL", O_CREAT); <BR> write(handle, buf,
strlen(buf)); <BR>
<P> /* display the size of the file */ <BR>
printf("file length in bytes: %ld\n", <BR>
filelength(handle)); <BR>
<P> /* close the file */ <BR> close(handle);
<BR> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: fillellipse <BR>功 能: 画出并填充一椭圆 <BR>用 法: void far
fillellipse(int x, int y, int xradius, int yradius); <BR>程序例: <BR>
<P>#include <graphics.h> <BR>#include <conio.h> <BR>
<P>int main(void) <BR>{ <BR> int gdriver = DETECT, gmode;
<BR> int xcenter, ycenter, i; <BR>
<P> initgraph(&gdriver,&gmode,""); <BR>
xcenter = getmaxx() / 2; <BR> ycenter = getmaxy() / 2; <BR>
<P> for (i=0; i<13; i++) <BR> {
<BR> setfillstyle(i,WHITE);
<BR> fillellipse(xcenter,ycenter,100,50);
<BR> getch(); <BR> } <BR>
<P> closegraph(); <BR> return 0; <BR>} <BR>
<BR> <BR> <BR>
<P>函数名: fillpoly <BR>功 能: 画并填充一个多边形 <BR>用 法: void far
fillpoly(int numpoints, int far *polypoints); <BR>程序例: <BR>
<P>#include <graphics.h> <BR>#include <stdlib.h> <BR>#include
<stdio.h> <BR>#include <conio.h> <BR>
<P>int main(void) <BR>{ <BR> /* request auto detection */
<BR> int gdriver = DETECT, gmode, errorcode; <BR>
int i, maxx, maxy; <BR>
<P> /* our polygon array */ <BR> int poly[8]; <BR>
<P> /* initialize graphics, local variables */
<BR> initgraph(&gdriver, &gmode, ""); <BR>
<P> /* read result of initialization */ <BR>
errorcode = graphresult(); <BR> if (errorcode != grOk)
<BR> /* an error occurred */ <BR> {
<BR> printf("Graphics error: %s\n",
<BR>
grapherrormsg(errorcode)); <BR>
printf("Press any key to halt:"); <BR>
getch(); <BR> exit(1);
<BR> /* terminate with an error code */
<BR> } <BR>
<P> maxx = getmaxx(); <BR> maxy = getmaxy(); <BR>
<P> poly[0] = 20; /*
1st vertext */ <BR> poly[1] = maxy / 2; <BR>
<P> poly[2] = maxx - 20; /* 2nd */ <BR> poly[3] =
20; <BR>
<P> poly[4] = maxx - 50; /* 3rd */ <BR> poly[5] =
maxy - 20; <BR>
<P> /* <BR> 4th vertex. fillpoly
automatically <BR> closes the polygon.
<BR> */ <BR> poly[6] = maxx / 2; <BR>
poly[7] = maxy / 2; <BR>
<P> /* loop through the fill patterns */ <BR> for
(i=EMPTY_FILL; i<USER_FILL; i++) <BR> {
<BR> /* set fill pattern */
<BR> setfillstyle(i, getmaxcolor()); <BR>
<P> /* draw a filled polygon */
<BR> fillpoly(4, poly); <BR>
<P> getch(); <BR> } <BR>
<P> /* clean up */ <BR> closegraph();
<BR> return 0; <BR>} <BR> <BR> <BR> <BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -