📄 王大刚--c语言编程宝典--r.htm
字号:
<P>#include <stdio.h> <BR>#include <alloc.h> <BR>#include
<string.h> <BR>
<P>int main(void) <BR>{ <BR> char *str; <BR>
<P> /* allocate memory for string */ <BR> str =
malloc(10); <BR>
<P> /* copy "Hello" into string */ <BR>
strcpy(str, "Hello"); <BR>
<P> printf("String is %s\n Address is %p\n", str, str);
<BR> str = realloc(str, 20); <BR> printf("String
is %s\n New address is %p\n", str, str); <BR>
<P> /* free memory */ <BR> free(str); <BR>
<P> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: rectangle <BR>功 能: 画一个矩形 <BR>用 法: void far
rectangle(int left, int top, int right, int bottom); <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 left, top, right, bottom; <BR>
<P> /* initialize graphics and local variables */
<BR> initgraph(&gdriver, &gmode, ""); <BR>
<P> /* read result of initialization */ <BR>
errorcode = graphresult(); <BR> if (errorcode != grOk)
/* an error occurred */ <BR> {
<BR> printf("Graphics error: %s\n",
grapherrormsg(errorcode)); <BR>
printf("Press any key to halt:"); <BR>
getch(); <BR> exit(1); /* terminate with an
error code */ <BR> } <BR>
<P> left = getmaxx() / 2 - 50; <BR> top =
getmaxy() / 2 - 50; <BR> right = getmaxx() / 2 + 50;
<BR> bottom = getmaxy() / 2 + 50; <BR>
<P> /* draw a rectangle */ <BR>
rectangle(left,top,right,bottom); <BR>
<P> /* clean up */ <BR> getch(); <BR>
closegraph(); <BR> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: registerbgidriver <BR>功 能: 登录已连接进来的图形驱动程序代码 <BR>用 法:
int registerbgidriver(void(*driver)(void)); <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>
<P> /* register a driver that was added into graphics.lib */
<BR> errorcode = registerbgidriver(EGAVGA_driver); <BR>
<P> /* report any registration errors */ <BR> if
(errorcode < 0) <BR> { <BR>
printf("Graphics error: %s\n", grapherrormsg(errorcode));
<BR> printf("Press any key to halt:");
<BR> getch();
<BR> exit(1); /* terminate with an error
code */ <BR> } <BR>
<P> /* initialize graphics and local variables */
<BR> initgraph(&gdriver, &gmode, ""); <BR>
<P> /* read result of initialization */ <BR>
errorcode = graphresult(); <BR> if (errorcode != grOk)
/* an error occurred */ <BR> {
<BR> printf("Graphics error: %s\n",
grapherrormsg(errorcode)); <BR>
printf("Press any key to halt:"); <BR>
getch(); <BR> exit(1); /* terminate with an
error code */ <BR> } <BR>
<P> /* draw a line */ <BR> line(0, 0, getmaxx(),
getmaxy()); <BR>
<P> /* clean up */ <BR> getch(); <BR>
closegraph(); <BR> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: remove <BR>功 能: 删除一个文件 <BR>用 法: int remove(char
*filename); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>
<P>int main(void) <BR>{ <BR> char file[80]; <BR>
<P> /* prompt for file name to delete */ <BR>
printf("File to delete: "); <BR> gets(file); <BR>
<P> /* delete the file */ <BR> if (remove(file) ==
0) <BR> printf("Removed %s.\n",file);
<BR> else <BR> perror("remove");
<BR>
<P> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: rename <BR>功 能: 重命名文件 <BR>用 法: int rename(char
*oldname, char *newname); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>
<P>int main(void) <BR>{ <BR> char oldname[80], newname[80];
<BR>
<P> /* prompt for file to rename and new name */
<BR> printf("File to rename: "); <BR>
gets(oldname); <BR> printf("New name: "); <BR>
gets(newname); <BR>
<P> /* Rename the file */ <BR> if (rename(oldname,
newname) == 0) <BR> printf("Renamed %s to
%s.\n", oldname, newname); <BR> else
<BR> perror("rename"); <BR>
<P> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: restorecrtmode <BR>功 能: 将屏幕模式恢复为先前的imitgraph设置 <BR>用
法: void far restorecrtmode(void); <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 x, y; <BR>
<P> /* initialize graphics and local variables */
<BR> initgraph(&gdriver, &gmode, ""); <BR>
<P> /* read result of initialization */ <BR>
errorcode = graphresult(); <BR> if (errorcode != grOk)
/* an error occurred */ <BR> {
<BR> printf("Graphics error: %s\n",
grapherrormsg(errorcode)); <BR>
printf("Press any key to halt:"); <BR>
getch(); <BR> exit(1); /* terminate with an
error code */ <BR> } <BR>
<P> x = getmaxx() / 2; <BR> y = getmaxy() / 2;
<BR>
<P> /* output a message */ <BR>
settextjustify(CENTER_TEXT, CENTER_TEXT); <BR> outtextxy(x, y,
"Press any key to exit graphics:"); <BR> getch(); <BR>
<P> /* restore system to text mode */ <BR>
restorecrtmode(); <BR> printf("We're now in text mode.\n");
<BR> printf("Press any key to return to graphics mode:");
<BR> getch(); <BR>
<P> /* return to graphics mode */ <BR>
setgraphmode(getgraphmode()); <BR>
<P> /* output a message */ <BR>
settextjustify(CENTER_TEXT, CENTER_TEXT); <BR> outtextxy(x, y,
"We're back in graphics mode."); <BR> outtextxy(x,
y+textheight("W"), "Press any key to halt:"); <BR>
<P> /* clean up */ <BR> getch(); <BR>
closegraph(); <BR> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: rewind <BR>功 能: 将文件指针重新指向一个流的开头 <BR>用 法: int
rewind(FILE *stream); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>#include <dir.h> <BR>
<P> int main(void) <BR> { <BR> FILE *fp;
<BR> char *fname = "TXXXXXX", *newname, first; <BR>
<P> newname = mktemp(fname); <BR> fp =
fopen(newname,"w+"); <BR>
fprintf(fp,"abcdefghijklmnopqrstuvwxyz"); <BR>
rewind(fp); <BR> fscanf(fp,"%c",&first);
<BR> printf("The first character is: %c\n",first);
<BR> fclose(fp); <BR> remove(newname);
<BR>
<P> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: rmdir <BR>功 能: 删除DOS文件目录 <BR>用 法: int rmdir(char
*stream); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>#include <conio.h> <BR>#include
<process.h> <BR>#include <dir.h> <BR>
<P>#define DIRNAME "testdir.$$$" <BR>
<P>int main(void) <BR>{ <BR> int stat; <BR>
<P> stat = mkdir(DIRNAME); <BR> if (!stat)
<BR>
printf("Directory created\n"); <BR> else <BR> {
<BR> printf("Unable to create directory\n");
<BR> exit(1); <BR> } <BR>
<P> getch(); <BR> system("dir/p");
<BR> getch(); <BR>
<P> stat = rmdir(DIRNAME); <BR> if (!stat)
<BR>
printf("\nDirectory deleted\n"); <BR> else <BR> {
<BR> perror("\nUnable to delete directory\n");
<BR> exit(1); <BR> } <BR>
<P> return 0; <BR>} <BR> <BR>
<HR width="94%" color=#ee9b73 SIZE=1>
</TD>
<TD class=tt3 vAlign=bottom width="8%" bgColor=#e0e0e0><STRONG><A
href="http://www.hjflying.8u8.com/cl/036.htm">后一页</A><BR><A
href="http://www.hjflying.8u8.com/cl/034.htm">前一页</A><BR><A
href="http://www.hjflying.8u8.com/cl/index.html">回目录</A><BR><A
href="http://www.hjflying.8u8.com/index.htm">回首页</A><BR></STRONG></TD></TR></TBODY></TABLE></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -