⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fp.htm

📁 turbo c
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<BR>&nbsp;<P>函数名: putc<BR>功&nbsp; 能: 输出一字符到指定流中<BR>用&nbsp; 法: int putc(int ch, FILE *stream);<BR>程序例:<P>#include &lt;stdio.h><P>int main(void)<BR>{<BR>&nbsp;&nbsp; char msg[] = "Hello world\n";<BR>&nbsp;&nbsp; int i = 0;<P>&nbsp;&nbsp; while (msg[i])<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putc(msg[i++], stdout);<BR>&nbsp;&nbsp; return 0;<BR>}<BR>&nbsp;<BR>&nbsp;<P>函数名: putch<BR>功&nbsp; 能: 输出字符到控制台<BR>用&nbsp; 法: int putch(int ch);<BR>程序例:<P>#include &lt;stdio.h><BR>#include &lt;conio.h><P>int main(void)<BR>{<BR>&nbsp;&nbsp; char ch = 0;<P>&nbsp;&nbsp; printf("Input a string:");<BR>&nbsp;&nbsp; while ((ch != '\r'))<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ch = getch();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putch(ch);<BR>&nbsp;&nbsp; }<BR>&nbsp;&nbsp; return 0;<BR>}<BR>&nbsp;<BR>&nbsp;<P>函数名: putchar<BR>功&nbsp; 能: 在stdout上输出字符<BR>用&nbsp; 法: int putchar(int ch);<BR>程序例:<P>#include &lt;stdio.h><P>/* define some box-drawing characters */<BR>#define LEFT_TOP&nbsp; 0xDA<BR>#define RIGHT_TOP 0xBF<BR>#define HORIZ&nbsp;&nbsp;&nbsp;&nbsp; 0xC4<BR>#define VERT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0xB3<BR>#define LEFT_BOT&nbsp; 0xC0<BR>#define RIGHT_BOT 0xD9<P>int main(void)<BR>{<BR>&nbsp;&nbsp; char i, j;<P>&nbsp;&nbsp; /* draw the top of the box */<BR>&nbsp;&nbsp; putchar(LEFT_TOP);<BR>&nbsp;&nbsp; for (i=0; i&lt;10; i++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putchar(HORIZ);<BR>&nbsp;&nbsp; putchar(RIGHT_TOP);<BR>&nbsp;&nbsp; putchar('\n');<P>&nbsp;&nbsp; /* draw the middle */<BR>&nbsp;&nbsp; for (i=0; i&lt;4; i++)<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putchar(VERT);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (j=0; j&lt;10; j++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putchar(' ');<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putchar(VERT);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putchar('\n');<BR>&nbsp;&nbsp; }<P>&nbsp;&nbsp; /* draw the bottom */<BR>&nbsp;&nbsp; putchar(LEFT_BOT);<BR>&nbsp;&nbsp; for (i=0; i&lt;10; i++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putchar(HORIZ);<BR>&nbsp;&nbsp; putchar(RIGHT_BOT);<BR>&nbsp;&nbsp; putchar('\n');<P>&nbsp;&nbsp; return 0;<BR>}<BR>&nbsp;<BR>&nbsp;<P>函数名: putenv<BR>功&nbsp; 能: 把字符串加到当前环境中<BR>用&nbsp; 法: int putenv(char *envvar);<BR>程序例:<P>#include &lt;stdio.h><BR>#include &lt;stdlib.h><BR>#include &lt;alloc.h><BR>#include &lt;string.h><BR>#include &lt;dos.h><P>int main(void)<BR>{<BR>&nbsp;&nbsp; char *path, *ptr;<BR>&nbsp;&nbsp; int i = 0;<P>&nbsp;&nbsp; /* get the current path environment */<BR>&nbsp;&nbsp; ptr = getenv("PATH");<P>&nbsp;&nbsp; /* set up new path */<BR>&nbsp;&nbsp; path = malloc(strlen(ptr)+15);<BR>&nbsp;&nbsp; strcpy(path,"PATH=");<BR>&nbsp;&nbsp; strcat(path,ptr);<BR>&nbsp;&nbsp; strcat(path,";c:\\temp");<P>&nbsp;&nbsp; /* replace the current path and display current environment*/<BR>&nbsp;&nbsp; putenv(path);<BR>&nbsp;&nbsp; while (environ[i])<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("%s\n",environ[i++]);<P>&nbsp;&nbsp; return 0;<BR>}<BR>&nbsp;<BR>&nbsp;<P>函数名: putimage<BR>功&nbsp; 能: 在屏幕上输出一个位图<BR>用&nbsp; 法: void far putimage(int x, int y, void far *bitmap, intop);<BR>程序例:<P>#include &lt;graphics.h><BR>#include &lt;stdlib.h><BR>#include &lt;stdio.h><BR>#include &lt;conio.h><P>#define ARROW_SIZE 10<P>void draw_arrow(int x, int y);<P>int main(void)<BR>{<BR>&nbsp;&nbsp; /* request autodetection */<BR>&nbsp;&nbsp; int gdriver = DETECT, gmode, errorcode;<BR>&nbsp;&nbsp; void *arrow;<BR>&nbsp;&nbsp; int x, y, maxx;<BR>&nbsp;&nbsp; unsigned int size;<P>&nbsp;&nbsp; /* initialize graphics and local variables */<BR>&nbsp;&nbsp; initgraph(&amp;gdriver, &amp;gmode, "");<P>&nbsp;&nbsp; /* read result of initialization */<BR>&nbsp;&nbsp; errorcode = graphresult();<BR>&nbsp;&nbsp; if (errorcode != grOk)&nbsp; /* an error occurred */<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Graphics error: %s\n", grapherrormsg(errorcode));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Press any key to halt:");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getch();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1); /* terminate with an errorcode */<BR>&nbsp;&nbsp; }<P>&nbsp;&nbsp; maxx = getmaxx();<BR>&nbsp;&nbsp; x = 0;<BR>&nbsp;&nbsp; y = getmaxy() / 2;<P>&nbsp;&nbsp; /* draw the image to be grabbed */<BR>&nbsp;&nbsp; draw_arrow(x, y);<P>&nbsp;&nbsp; /* calculate the size of the image */<BR>&nbsp;&nbsp; size = imagesize(x, y-ARROW_SIZE, x+(4*ARROW_SIZE), y+ARROW_SIZE);<P>&nbsp;&nbsp; /* allocate memory to hold the image */<BR>&nbsp;&nbsp; arrow = malloc(size);<P>&nbsp;&nbsp; /* grab the image */<BR>&nbsp;&nbsp; getimage(x, y-ARROW_SIZE, x+(4*ARROW_SIZE), y+ARROW_SIZE,arrow);<P>&nbsp;&nbsp; /* repeat until a key is pressed */<BR>&nbsp;&nbsp; while (!kbhit())<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* erase old image */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putimage(x, y-ARROW_SIZE, arrow, XOR_PUT);<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x += ARROW_SIZE;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (x >= maxx)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x = 0;<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* plot new image */<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putimage(x, y-ARROW_SIZE, arrow, XOR_PUT);<BR>&nbsp;&nbsp; }<P>&nbsp;&nbsp; /* clean up */<BR>&nbsp;&nbsp; free(arrow);<BR>&nbsp;&nbsp; closegraph();<BR>&nbsp;&nbsp; return 0;<BR>}<P>void draw_arrow(int x, int y)<BR>{<BR>&nbsp;&nbsp; /* draw an arrow on the screen */<BR>&nbsp;&nbsp; moveto(x, y);<BR>&nbsp;&nbsp; linerel(4*ARROW_SIZE, 0);<BR>&nbsp;&nbsp; linerel(-2*ARROW_SIZE, -1*ARROW_SIZE);<BR>&nbsp;&nbsp; linerel(0, 2*ARROW_SIZE);<BR>&nbsp;&nbsp; linerel(2*ARROW_SIZE, -1*ARROW_SIZE);<BR>}<BR>&nbsp;<BR>&nbsp;<P>函数名: putpixel<BR>功&nbsp; 能: 在指定位置画一像素<BR>用&nbsp; 法: void far putpixel (int x, int y, int pixelcolor);<BR>程序例:<P>#include &lt;graphics.h><BR>#include &lt;stdlib.h><BR>#include &lt;stdio.h><BR>#include &lt;conio.h><BR>#include &lt;dos.h><P>#define PIXEL_COUNT 1000<BR>#define DELAY_TIME&nbsp; 100&nbsp; /* in milliseconds */<P>int main(void)<BR>{<BR>&nbsp;&nbsp; /* request autodetection */<BR>&nbsp;&nbsp; int gdriver = DETECT, gmode, errorcode;<BR>&nbsp;&nbsp; int i, x, y, color, maxx, maxy, maxcolor, seed;<P>&nbsp;&nbsp; /* initialize graphics and local variables */<BR>&nbsp;&nbsp; initgraph(&amp;gdriver, &amp;gmode, "");<P>&nbsp;&nbsp; /* read result of initialization */<BR>&nbsp;&nbsp; errorcode = graphresult();<BR>&nbsp;&nbsp; if (errorcode != grOk)&nbsp; /* an error occurred */<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Graphics error: %s\n", grapherrormsg(errorcode));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Press any key to halt:");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getch();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1); /* terminate with an errorcode */<BR>&nbsp;&nbsp; }<P>&nbsp;&nbsp; maxx = getmaxx() + 1;<BR>&nbsp;&nbsp; maxy = getmaxy() + 1;<BR>&nbsp;&nbsp; maxcolor = getmaxcolor() + 1;<P>&nbsp;&nbsp; while (!kbhit())<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* seed the random number generator*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; seed = random(32767);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; srand(seed);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (i=0; i&lt;PIXEL_COUNT; i++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp; x = random(maxx);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; y = random(maxy);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; color = random(maxcolor);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putpixel(x, y, color);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delay(DELAY_TIME);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; srand(seed);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (i=0; i&lt;PIXEL_COUNT; i++)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp; x = random(maxx);<BR>&nbsp; y = random(maxy);<BR>&nbsp; color = random(maxcolor);<BR>&nbsp; if (color == getpixel(x, y))<BR>&nbsp;&nbsp;&nbsp;&nbsp; putpixel(x, y, 0);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp; }<P>&nbsp;&nbsp; /* clean up */<BR>&nbsp;&nbsp; getch();<BR>&nbsp;&nbsp; closegraph();<BR>&nbsp;&nbsp; return 0;<BR>}<BR>&nbsp;<BR>&nbsp;<P>函数名: puts<BR>功&nbsp; 能: 送一字符串到流中<BR>用&nbsp; 法: int puts(char *string);<BR>程序例:<P>#include &lt;stdio.h><BR>int main(void)<BR>{<BR>&nbsp;&nbsp; char string[] = "This is an example output string\n";<P>&nbsp;&nbsp; puts(string);<BR>&nbsp;&nbsp; return 0;<BR>}<BR>&nbsp;<BR>&nbsp;<P>函数名: puttext<BR>功&nbsp; 能: 将文本从存储区拷贝到屏幕<BR>用&nbsp; 法: int puttext(int left, int top, int right, int bottom,void *source);<BR>程序例:<P>#include &lt;conio.h><BR>int main(void)<BR>{<BR>&nbsp;&nbsp; char buffer[512];<P>&nbsp;&nbsp; /* put some text to the console */<BR>&nbsp;&nbsp; clrscr();<BR>&nbsp;&nbsp; gotoxy(20, 12);<BR>&nbsp;&nbsp; cprintf("This is a test.&nbsp; Press any key to continue...");<BR>&nbsp;&nbsp; getch();<P>&nbsp;&nbsp; /* grab screen contents */<BR>&nbsp;&nbsp; gettext(20, 12, 36, 21,buffer);<BR>&nbsp;&nbsp; clrscr();<P>&nbsp;&nbsp; /* put selected characters back to the screen */<BR>&nbsp;&nbsp; gotoxy(20, 12);<BR>&nbsp;&nbsp; puttext(20, 12, 36, 21, buffer);<BR>&nbsp;&nbsp; getch();<P>&nbsp;&nbsp; return 0;<BR>}<BR>&nbsp;<BR>&nbsp;<P>函数名: putw<BR>功&nbsp; 能: 把一字符或字送到流中<BR>用&nbsp; 法: int putw(int w, FILE *stream);<BR>程序例:<P>#include &lt;stdio.h><BR>#include &lt;stdlib.h><P>#define FNAME "test.$$$"<P>int main(void)<BR>{<BR>&nbsp;&nbsp; FILE *fp;<BR>&nbsp;&nbsp; int word;<P>&nbsp;&nbsp; /* place the word in a file */<BR>&nbsp;&nbsp; fp = fopen(FNAME, "wb");<BR>&nbsp;&nbsp; if (fp == NULL)<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Error opening file %s\n", FNAME);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1);<BR>&nbsp;&nbsp; }<P>&nbsp;&nbsp; word = 94;<BR>&nbsp;&nbsp; putw(word,fp);<BR>&nbsp;&nbsp; if (ferror(fp))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Error writing to file\n");<BR>&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Successful write\n");<BR>&nbsp;&nbsp; fclose(fp);<P>&nbsp;&nbsp; /* reopen the file */<BR>&nbsp;&nbsp; fp = fopen(FNAME, "rb");<BR>&nbsp;&nbsp; if (fp == NULL)<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Error opening file %s\n", FNAME);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1);<BR>&nbsp;&nbsp; }<P>&nbsp;&nbsp; /* extract the word */<BR>&nbsp;&nbsp; word = getw(fp);<BR>&nbsp;&nbsp; if (ferror(fp))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Error reading file\n");<BR>&nbsp;&nbsp; else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Successful read: word= %d\n", word);<P>&nbsp;&nbsp; /* clean up */<BR>&nbsp;&nbsp; fclose(fp);<BR>&nbsp;&nbsp; unlink(FNAME);<P>&nbsp;&nbsp; return 0;<BR>}<BR>&nbsp;<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<A HREF="index.html">返回目录</A><BR></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -