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

📄 王大刚--c语言编程宝典--r.htm

📁 初学者的良师益友。其中包括C的全部教程。
💻 HTM
📖 第 1 页 / 共 2 页
字号:
      <P>#include &lt;stdio.h&gt; <BR>#include &lt;alloc.h&gt; <BR>#include 
      &lt;string.h&gt; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; char *str; <BR>
      <P>&nbsp;&nbsp; /* allocate memory for string */ <BR>&nbsp;&nbsp; str = 
      malloc(10); <BR>
      <P>&nbsp;&nbsp; /* copy "Hello" into string */ <BR>&nbsp;&nbsp; 
      strcpy(str, "Hello"); <BR>
      <P>&nbsp;&nbsp; printf("String is %s\n&nbsp; Address is %p\n", str, str); 
      <BR>&nbsp;&nbsp; str = realloc(str, 20); <BR>&nbsp;&nbsp; printf("String 
      is %s\n&nbsp; New address is %p\n", str, str); <BR>
      <P>&nbsp;&nbsp; /* free memory */ <BR>&nbsp;&nbsp; free(str); <BR>
      <P>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: rectangle <BR>功&nbsp; 能: 画一个矩形 <BR>用&nbsp; 法: void far 
      rectangle(int left, int top, int right, int bottom); <BR>程序例: <BR>
      <P>#include &lt;graphics.h&gt; <BR>#include &lt;stdlib.h&gt; <BR>#include 
      &lt;stdio.h&gt; <BR>#include &lt;conio.h&gt; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; /* request auto detection */ 
      <BR>&nbsp;&nbsp; int gdriver = DETECT, gmode, errorcode; <BR>&nbsp;&nbsp; 
      int left, top, right, bottom; <BR>
      <P>&nbsp;&nbsp; /* initialize graphics and local variables */ 
      <BR>&nbsp;&nbsp; initgraph(&amp;gdriver, &amp;gmode, ""); <BR>
      <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 
      error code */ <BR>&nbsp;&nbsp; } <BR>
      <P>&nbsp;&nbsp; left = getmaxx() / 2 - 50; <BR>&nbsp;&nbsp; top = 
      getmaxy() / 2 - 50; <BR>&nbsp;&nbsp; right = getmaxx() / 2 + 50; 
      <BR>&nbsp;&nbsp; bottom = getmaxy() / 2 + 50; <BR>
      <P>&nbsp;&nbsp; /* draw a rectangle */ <BR>&nbsp;&nbsp; 
      rectangle(left,top,right,bottom); <BR>
      <P>&nbsp;&nbsp; /* clean up */ <BR>&nbsp;&nbsp; getch(); <BR>&nbsp;&nbsp; 
      closegraph(); <BR>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: registerbgidriver <BR>功&nbsp; 能: 登录已连接进来的图形驱动程序代码 <BR>用&nbsp; 法: 
      int registerbgidriver(void(*driver)(void)); <BR>程序例: <BR>
      <P>#include &lt;graphics.h&gt; <BR>#include &lt;stdlib.h&gt; <BR>#include 
      &lt;stdio.h&gt; <BR>#include &lt;conio.h&gt; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; /* request auto detection */ 
      <BR>&nbsp;&nbsp; int gdriver = DETECT, gmode, errorcode; <BR>
      <P>&nbsp;&nbsp; /* register a driver that was added into graphics.lib */ 
      <BR>&nbsp;&nbsp; errorcode = registerbgidriver(EGAVGA_driver); <BR>
      <P>&nbsp;&nbsp; /* report any registration errors */ <BR>&nbsp;&nbsp; if 
      (errorcode &lt; 0) <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 error 
      code */ <BR>&nbsp;&nbsp; } <BR>
      <P>&nbsp;&nbsp; /* initialize graphics and local variables */ 
      <BR>&nbsp;&nbsp; initgraph(&amp;gdriver, &amp;gmode, ""); <BR>
      <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 
      error code */ <BR>&nbsp;&nbsp; } <BR>
      <P>&nbsp;&nbsp; /* draw a line */ <BR>&nbsp;&nbsp; line(0, 0, getmaxx(), 
      getmaxy()); <BR>
      <P>&nbsp;&nbsp; /* clean up */ <BR>&nbsp;&nbsp; getch(); <BR>&nbsp;&nbsp; 
      closegraph(); <BR>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: remove <BR>功&nbsp; 能: 删除一个文件 <BR>用&nbsp; 法: int remove(char 
      *filename); <BR>程序例: <BR>
      <P>#include &lt;stdio.h&gt; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; char file[80]; <BR>
      <P>&nbsp;&nbsp; /* prompt for file name to delete */ <BR>&nbsp;&nbsp; 
      printf("File to delete: "); <BR>&nbsp;&nbsp; gets(file); <BR>
      <P>&nbsp;&nbsp; /* delete the file */ <BR>&nbsp;&nbsp; if (remove(file) == 
      0) <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Removed %s.\n",file); 
      <BR>&nbsp;&nbsp; else <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; perror("remove"); 
      <BR>
      <P>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: rename <BR>功&nbsp; 能: 重命名文件 <BR>用&nbsp; 法: int rename(char 
      *oldname, char *newname); <BR>程序例: <BR>
      <P>#include &lt;stdio.h&gt; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; char oldname[80], newname[80]; 
      <BR>
      <P>&nbsp;&nbsp; /* prompt for file to rename and new name */ 
      <BR>&nbsp;&nbsp; printf("File to rename: "); <BR>&nbsp;&nbsp; 
      gets(oldname); <BR>&nbsp;&nbsp; printf("New name: "); <BR>&nbsp;&nbsp; 
      gets(newname); <BR>
      <P>&nbsp;&nbsp; /* Rename the file */ <BR>&nbsp;&nbsp; if (rename(oldname, 
      newname) == 0) <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Renamed %s to 
      %s.\n", oldname, newname); <BR>&nbsp;&nbsp; else 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; perror("rename"); <BR>
      <P>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: restorecrtmode <BR>功&nbsp; 能: 将屏幕模式恢复为先前的imitgraph设置 <BR>用&nbsp; 
      法: void far restorecrtmode(void); <BR>程序例: <BR>
      <P>#include &lt;graphics.h&gt; <BR>#include &lt;stdlib.h&gt; <BR>#include 
      &lt;stdio.h&gt; <BR>#include &lt;conio.h&gt; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; /* request auto detection */ 
      <BR>&nbsp;&nbsp; int gdriver = DETECT, gmode, errorcode; <BR>&nbsp;&nbsp; 
      int x, y; <BR>
      <P>&nbsp;&nbsp; /* initialize graphics and local variables */ 
      <BR>&nbsp;&nbsp; initgraph(&amp;gdriver, &amp;gmode, ""); <BR>
      <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 
      error code */ <BR>&nbsp;&nbsp; } <BR>
      <P>&nbsp;&nbsp; x = getmaxx() / 2; <BR>&nbsp;&nbsp; y = getmaxy() / 2; 
<BR>
      <P>&nbsp;&nbsp; /* output a message */ <BR>&nbsp;&nbsp; 
      settextjustify(CENTER_TEXT, CENTER_TEXT); <BR>&nbsp;&nbsp; outtextxy(x, y, 
      "Press any key to exit graphics:"); <BR>&nbsp;&nbsp; getch(); <BR>
      <P>&nbsp;&nbsp; /* restore system to text mode */ <BR>&nbsp;&nbsp; 
      restorecrtmode(); <BR>&nbsp;&nbsp; printf("We're now in text mode.\n"); 
      <BR>&nbsp;&nbsp; printf("Press any key to return to graphics mode:"); 
      <BR>&nbsp;&nbsp; getch(); <BR>
      <P>&nbsp;&nbsp; /* return to graphics mode */ <BR>&nbsp;&nbsp; 
      setgraphmode(getgraphmode()); <BR>
      <P>&nbsp;&nbsp; /* output a message */ <BR>&nbsp;&nbsp; 
      settextjustify(CENTER_TEXT, CENTER_TEXT); <BR>&nbsp;&nbsp; outtextxy(x, y, 
      "We're back in graphics mode."); <BR>&nbsp;&nbsp; outtextxy(x, 
      y+textheight("W"), "Press any key to halt:"); <BR>
      <P>&nbsp;&nbsp; /* clean up */ <BR>&nbsp;&nbsp; getch(); <BR>&nbsp;&nbsp; 
      closegraph(); <BR>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: rewind <BR>功&nbsp; 能: 将文件指针重新指向一个流的开头 <BR>用&nbsp; 法: int 
      rewind(FILE *stream); <BR>程序例: <BR>
      <P>#include &lt;stdio.h&gt; <BR>#include &lt;dir.h&gt; <BR>
      <P>&nbsp;int main(void) <BR>&nbsp;{ <BR>&nbsp;&nbsp;&nbsp; FILE *fp; 
      <BR>&nbsp;&nbsp;&nbsp; char *fname = "TXXXXXX", *newname, first; <BR>
      <P>&nbsp;&nbsp;&nbsp; newname = mktemp(fname); <BR>&nbsp;&nbsp;&nbsp; fp = 
      fopen(newname,"w+"); <BR>&nbsp;&nbsp;&nbsp; 
      fprintf(fp,"abcdefghijklmnopqrstuvwxyz"); <BR>&nbsp;&nbsp;&nbsp; 
      rewind(fp); <BR>&nbsp;&nbsp;&nbsp; fscanf(fp,"%c",&amp;first); 
      <BR>&nbsp;&nbsp;&nbsp; printf("The first character is: %c\n",first); 
      <BR>&nbsp;&nbsp;&nbsp; fclose(fp); <BR>&nbsp;&nbsp;&nbsp; remove(newname); 
      <BR>
      <P>&nbsp;&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: rmdir <BR>功&nbsp; 能: 删除DOS文件目录 <BR>用&nbsp; 法: int rmdir(char 
      *stream); <BR>程序例: <BR>
      <P>#include &lt;stdio.h&gt; <BR>#include &lt;conio.h&gt; <BR>#include 
      &lt;process.h&gt; <BR>#include &lt;dir.h&gt; <BR>
      <P>#define DIRNAME "testdir.$$$" <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; int stat; <BR>
      <P>&nbsp;&nbsp; stat = mkdir(DIRNAME); <BR>&nbsp;&nbsp; if (!stat) 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      printf("Directory created\n"); <BR>&nbsp;&nbsp; else <BR>&nbsp;&nbsp; { 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Unable to create directory\n"); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1); <BR>&nbsp;&nbsp; } <BR>
      <P>&nbsp;&nbsp; getch(); <BR>&nbsp;&nbsp; system("dir/p"); 
      <BR>&nbsp;&nbsp; getch(); <BR>
      <P>&nbsp;&nbsp; stat = rmdir(DIRNAME); <BR>&nbsp;&nbsp; if (!stat) 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      printf("\nDirectory deleted\n"); <BR>&nbsp;&nbsp; else <BR>&nbsp;&nbsp; { 
      <BR>&nbsp;&nbsp; perror("\nUnable to delete directory\n"); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1); <BR>&nbsp;&nbsp; } <BR>
      <P>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <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 + -