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

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

📁 初学者的良师益友。其中包括C的全部教程。
💻 HTM
📖 第 1 页 / 共 4 页
字号:
      <P>&nbsp;&nbsp; if ((stream = fopen("DUMMY.FIL", "w+")) 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; == NULL) <BR>&nbsp;&nbsp; { 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fprintf(stderr, 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      "Cannot open output file.\n"); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 
      1; <BR>&nbsp;&nbsp; } <BR>
      <P>&nbsp;&nbsp; /* write some data to the file */ <BR>&nbsp;&nbsp; 
      fwrite(msg, strlen(msg)+1, 1, stream); <BR>
      <P>&nbsp;&nbsp; /* seek to the beginning of the file */ <BR>&nbsp;&nbsp; 
      fseek(stream, SEEK_SET, 0); <BR>
      <P>&nbsp;&nbsp; /* read the data and display it */ <BR>&nbsp;&nbsp; 
      fread(buf, strlen(msg)+1, 1, stream); <BR>&nbsp;&nbsp; printf("%s\n", 
      buf); <BR>
      <P>&nbsp;&nbsp; fclose(stream); <BR>&nbsp;&nbsp; return 0; <BR>} 
      <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: free <BR>功&nbsp; 能: 释放已分配的块 <BR>用&nbsp; 法: void free(void *ptr); 
      <BR>程序例: <BR>
      <P>#include &lt;string.h&gt; <BR>#include &lt;stdio.h&gt; <BR>#include 
      &lt;alloc.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" to string */ <BR>&nbsp;&nbsp; strcpy(str, 
      "Hello"); <BR>
      <P>&nbsp;&nbsp; /* display string */ <BR>&nbsp;&nbsp; printf("String is 
      %s\n", str); <BR>
      <P>&nbsp;&nbsp; /* free memory */ <BR>&nbsp;&nbsp; free(str); <BR>
      <P>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>
      <P>函数名: freemem <BR>功&nbsp; 能: 释放先前分配的DOS内存块 <BR>用&nbsp; 法: int 
      freemem(unsigned seg); <BR>程序例: <BR>
      <P>#include &lt;dos.h&gt; <BR>#include &lt;alloc.h&gt; <BR>#include 
      &lt;stdio.h&gt; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; unsigned int size, segp; 
      <BR>&nbsp;&nbsp; int stat; <BR>
      <P>&nbsp;&nbsp; size = 64; /* (64 x 16) = 1024 bytes */ <BR>&nbsp;&nbsp; 
      stat = allocmem(size, &amp;segp); <BR>&nbsp;&nbsp; if (stat &lt; 0) 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Allocated memory at segment:\ 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; %x\n", segp); <BR>&nbsp;&nbsp; else 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Failed: maximum number of\ 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; paragraphs available is %u\n", 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stat); <BR>&nbsp;&nbsp; freemem(segp); 
      <BR>
      <P>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: freopen <BR>功&nbsp; 能: 替换一个流 <BR>用&nbsp; 法: FILE *freopen(char 
      *filename, char *type, FILE *stream); <BR>程序例: <BR>
      <P>#include &lt;stdio.h&gt; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; /* redirect standard output to a 
      file */ <BR>&nbsp;&nbsp; if (freopen("OUTPUT.FIL", "w", stdout) 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; == NULL) 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fprintf(stderr, "error redirecting\ 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      stdout\n"); <BR>
      <P>&nbsp;&nbsp; /* this output will go to a file */ <BR>&nbsp;&nbsp; 
      printf("This will go into a file."); <BR>
      <P>&nbsp;&nbsp; /* close the standard output stream */ <BR>&nbsp;&nbsp; 
      fclose(stdout); <BR>
      <P>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: frexp <BR>功&nbsp; 能: 把一个双精度数分解为尾数的指数 <BR>用&nbsp; 法: double 
      frexp(double value, int *eptr); <BR>程序例: <BR>
      <P>#include &lt;math.h&gt; <BR>#include &lt;stdio.h&gt; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; double mantissa, number; 
      <BR>&nbsp;&nbsp; int exponent; <BR>
      <P>&nbsp;&nbsp; number = 8.0; <BR>&nbsp;&nbsp; mantissa = frexp(number, 
      &amp;exponent); <BR>
      <P>&nbsp;&nbsp; printf("The number %lf is ", number); <BR>&nbsp;&nbsp; 
      printf("%lf times two to the ", mantissa); <BR>&nbsp;&nbsp; printf("power 
      of %d\n", exponent); <BR>
      <P>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: fscanf <BR>功&nbsp; 能: 从一个流中执行格式化输入 <BR>用&nbsp; 法: int fscanf(FILE 
      *stream, char *format[,argument...]); <BR>程序例: <BR>
      <P>#include &lt;stdlib.h&gt; <BR>#include &lt;stdio.h&gt; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; int i; <BR>
      <P>&nbsp;&nbsp; printf("Input an integer: "); <BR>
      <P>&nbsp;&nbsp; /* read an integer from the 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; standard input stream */ 
      <BR>&nbsp;&nbsp; if (fscanf(stdin, "%d", &amp;i)) 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("The integer read was: %i\n", 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      i); <BR>&nbsp;&nbsp; else <BR>&nbsp;&nbsp; { 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fprintf(stderr, "Error reading an \ 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      integer from stdin.\n"); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1); 
      <BR>&nbsp;&nbsp; } <BR>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>&nbsp; 
      <BR>&nbsp; <BR>
      <P>函数名: fseek <BR>功&nbsp; 能: 重定位流上的文件指针 <BR>用&nbsp; 法: int fseek(FILE 
      *stream, long offset, int fromwhere); <BR>程序例: <BR>
      <P>#include &lt;stdio.h&gt; <BR>
      <P>long filesize(FILE *stream); <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; FILE *stream; <BR>
      <P>&nbsp;&nbsp; stream = fopen("MYFILE.TXT", "w+"); <BR>&nbsp;&nbsp; 
      fprintf(stream, "This is a test"); <BR>&nbsp;&nbsp; printf("Filesize of 
      MYFILE.TXT is %ld bytes\n", filesize(stream)); <BR>&nbsp;&nbsp; 
      fclose(stream); <BR>&nbsp;&nbsp; return 0; <BR>} <BR>
      <P>long filesize(FILE *stream) <BR>{ <BR>&nbsp;&nbsp; long curpos, length; 
      <BR>
      <P>&nbsp;&nbsp; curpos = ftell(stream); <BR>&nbsp;&nbsp; fseek(stream, 0L, 
      SEEK_END); <BR>&nbsp;&nbsp; length = ftell(stream); <BR>&nbsp;&nbsp; 
      fseek(stream, curpos, SEEK_SET); <BR>&nbsp;&nbsp; return length; <BR>} 
      <BR>&nbsp; <BR>&nbsp; <BR>&nbsp; <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: fsetpos <BR>功&nbsp; 能: 定位流上的文件指针 <BR>用&nbsp; 法: int fsetpos(FILE 
      *stream, const fpos_t *pos); <BR>程序例: <BR>
      <P>#include &lt;stdlib.h&gt; <BR>#include &lt;stdio.h&gt; <BR>
      <P>void showpos(FILE *stream); <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; FILE *stream; <BR>&nbsp;&nbsp; 
      fpos_t filepos; <BR>
      <P>&nbsp;&nbsp; /* open a file for update */ <BR>&nbsp;&nbsp; stream = 
      fopen("DUMMY.FIL", "w+"); <BR>
      <P>&nbsp;&nbsp; /* save the file pointer position */ <BR>&nbsp;&nbsp; 
      fgetpos(stream, &amp;filepos); <BR>
      <P>&nbsp;&nbsp; /* write some data to the file */ <BR>&nbsp;&nbsp; 
      fprintf(stream, "This is a test"); <BR>
      <P>&nbsp;&nbsp; /* show the current file position */ <BR>&nbsp;&nbsp; 
      showpos(stream); <BR>
      <P>&nbsp;&nbsp; /* set a new file position, display it */ <BR>&nbsp;&nbsp; 
      if (fsetpos(stream, &amp;filepos) == 0) <BR>&nbsp;&nbsp;&nbsp;&nbsp; 
      showpos(stream); <BR>&nbsp;&nbsp; else <BR>&nbsp;&nbsp; { 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fprintf(stderr, "Error setting file \ 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pointer.\n"); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1); <BR>&nbsp;&nbsp; } <BR>
      <P>&nbsp;&nbsp; /* close the file */ <BR>&nbsp;&nbsp; fclose(stream); 
      <BR>&nbsp;&nbsp; return 0; <BR>} <BR>
      <P>void showpos(FILE *stream) <BR>{ <BR>&nbsp;&nbsp; fpos_t pos; <BR>
      <P>&nbsp;&nbsp; /* display the current file pointer 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; position of a stream */ 
      <BR>&nbsp;&nbsp; fgetpos(stream, &amp;pos); <BR>&nbsp;&nbsp; printf("File 
      position: %ld\n", pos); <BR>} <BR>&nbsp; <BR>
      <P>函数名: fstat <BR>功&nbsp; 能: 获取打开文件信息 <BR>用&nbsp; 法: int fstat(char 
      *handle, struct stat *buff); <BR>程序例: <BR>
      <P>#include &lt;sys\stat.h&gt; <BR>#include &lt;stdio.h&gt; <BR>#include 
      &lt;time.h&gt; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; struct stat statbuf; 
      <BR>&nbsp;&nbsp; FILE *stream; <BR>
      <P>&nbsp;&nbsp; /* open a file for update */ <BR>&nbsp;&nbsp; if ((stream 
      = fopen("DUMMY.FIL", "w+")) <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; == 
      NULL) <BR>&nbsp;&nbsp; { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      fprintf(stderr, "Cannot open output \ 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      file.\n"); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return(1); <BR>&nbsp;&nbsp; 
      } <BR>&nbsp;&nbsp; fprintf(stream, "This is a test"); <BR>&nbsp;&nbsp; 
      fflush(stream); <BR>
      <P>&nbsp;&nbsp; /* get information about the file */ <BR>&nbsp;&nbsp; 
      fstat(fileno(stream), &amp;statbuf); <BR>&nbsp;&nbsp; fclose(stream); <BR>
      <P>&nbsp;&nbsp; /* display the information returned */ <BR>&nbsp;&nbsp; if 
      (statbuf.st_mode &amp; S_IFCHR) <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      printf("Handle refers to a device.\n"); <BR>&nbsp;&nbsp; if 
      (statbuf.st_mode &amp; S_IFREG) <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      printf("Handle refers to an ordinary \ 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      file.\n"); <BR>&nbsp;&nbsp; if (statbuf.st_mode &amp; S_IREAD) 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("User has read permission on \ 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      file.\n"); <BR>&nbsp;&nbsp; if (statbuf.st_mode &amp; S_IWRITE) 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("User has write permission on \ 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      file.\n"); <BR>
      <P>&nbsp;&nbsp; printf("Drive letter of file: %c\n", <BR>&nbsp;&nbsp; 
      'A'+statbuf.st_dev); <BR>&nbsp;&nbsp; printf("Size of file in bytes: 
      %ld\n", <BR>&nbsp;&nbsp; statbuf.st_size); <BR>&nbsp;&nbsp; printf("Time 
      file last opened: %s\n", <BR>&nbsp;&nbsp; ctime(&amp;statbuf.st_ctime)); 
      <BR>&nbsp;&nbsp; return 0; <BR>} <BR>&nbsp; <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: ftell <BR>功&nbsp; 能: 返回当前文件指针 <BR>用&nbsp; 法: long ftell(FILE 
      *stream); <BR>程序例: <BR>
      <P>#include &lt;stdio.h&gt; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; FILE *stream; <BR>
      <P>&nbsp;&nbsp; stream = fopen("MYFILE.TXT", "w+"); <BR>&nbsp;&nbsp; 
      fprintf(stream, "This is a test"); <BR>&nbsp;&nbsp; printf("The file 
      pointer is at byte \ 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; %ld\n", 
      ftell(stream)); <BR>&nbsp;&nbsp; fclose(stream); <BR>&nbsp;&nbsp; return 
      0; <BR>} <BR>&nbsp; <BR>&nbsp; <BR>&nbsp; <BR>
      <P>函数名: fwrite <BR>功&nbsp; 能: 写内容到流中 <BR>用&nbsp; 法: int fwrite(void *ptr, 
      int size, int nitems, FILE *stream); <BR>程序例: <BR>
      <P>#include &lt;stdio.h&gt; <BR>
      <P>struct mystruct <BR>{ <BR>&nbsp; int i; <BR>&nbsp; char ch; <BR>}; <BR>
      <P>int main(void) <BR>{ <BR>&nbsp;&nbsp; FILE *stream; <BR>&nbsp;&nbsp; 
      struct mystruct s; <BR>
      <P>&nbsp;&nbsp; if ((stream = fopen("TEST.$$$", "wb")) == NULL) /* open 
      file TEST.$$$ */ <BR>&nbsp;&nbsp; { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      fprintf(stderr, "Cannot open output file.\n"); 
      <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 1; <BR>&nbsp;&nbsp; } 
      <BR>&nbsp;&nbsp; s.i = 0; <BR>&nbsp;&nbsp; s.ch = 'A'; <BR>&nbsp;&nbsp; 
      fwrite(&amp;s, sizeof(s), 1, stream); /* write struct s to file */ 
      <BR>&nbsp;&nbsp; fclose(stream); /* close file */ <BR>&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/026.htm">后一页</A><BR><A 
      href="http://www.hjflying.8u8.com/cl/024.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 + -