数模论坛

 找回密码
 注-册-帐-号
搜索
热搜: 活动 交友 discuz

C语言的函数!

  [复制链接]
 楼主| 发表于 2004-5-8 17:38:44 | 显示全部楼层
<>函数名: free
功  能: 释放已分配的块
用  法: void free(void *ptr);
程序例: <>#include &lt;string.h&gt;
#include &lt;stdio.h&gt;
#include &lt;alloc.h&gt; <>int main(void)
{
   char *str; <P>   /* allocate memory for string */
   str = malloc(10); <P>   /* copy "Hello" to string */
   strcpy(str, "Hello"); <P>   /* display string */
   printf("String is %s\n", str); <P>   /* free memory */
   free(str); <P>   return 0;
}
  </P>
 楼主| 发表于 2004-5-8 17:38:53 | 显示全部楼层
<>函数名: freemem
功  能: 释放先前分配的DOS内存块
用  法: int freemem(unsigned seg);
程序例: <>#include &lt;dos.h&gt;
#include &lt;alloc.h&gt;
#include &lt;stdio.h&gt; <>int main(void)
{
   unsigned int size, segp;
   int stat; <P>   size = 64; /* (64 x 16) = 1024 bytes */
   stat = allocmem(size, &amp;segp);
   if (stat &lt; 0)
      printf("Allocated memory at segment:\
      %x\n", segp);
   else
      printf("Failed: maximum number of\
      paragraphs available is %u\n",
      stat);
   freemem(segp); <P>   return 0;
}
</P>
 楼主| 发表于 2004-5-8 17:39:03 | 显示全部楼层
<>函数名: freopen
功  能: 替换一个流
用  法: FILE *freopen(char *filename, char *type, FILE *stream);
程序例: <>#include &lt;stdio.h&gt; <>int main(void)
{
   /* redirect standard output to a file */
   if (freopen("OUTPUT.FIL", "w", stdout)
       == NULL)
      fprintf(stderr, "error redirecting\
              stdout\n"); <P>   /* this output will go to a file */
   printf("This will go into a file."); <P>   /* close the standard output stream */
   fclose(stdout); <P>   return 0;
}
  
</P>
 楼主| 发表于 2004-5-8 17:39:11 | 显示全部楼层
<>函数名: frexp
功  能: 把一个双精度数分解为尾数的指数
用  法: double frexp(double value, int *eptr);
程序例: <>#include &lt;math.h&gt;
#include &lt;stdio.h&gt; <>int main(void)
{
   double mantissa, number;
   int exponent; <P>   number = 8.0;
   mantissa = frexp(number, &amp;exponent); <P>   printf("The number %lf is ", number);
   printf("%lf times two to the ", mantissa);
   printf("power of %d\n", exponent); <P>   return 0;
}
</P>
 楼主| 发表于 2004-5-8 17:39:23 | 显示全部楼层
<>函数名: fscanf
功  能: 从一个流中执行格式化输入
用  法: int fscanf(FILE *stream, char *format[,argument...]);
程序例: <>#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt; <>int main(void)
{
   int i; <P>   printf("Input an integer: "); <P>   /* read an integer from the
      standard input stream */
   if (fscanf(stdin, "%d", &amp;i))
      printf("The integer read was: %i\n",
             i);
   else
   {
      fprintf(stderr, "Error reading an \
              integer from stdin.\n");
      exit(1);
   }
   return 0;
}
</P>
 楼主| 发表于 2004-5-8 17:39:33 | 显示全部楼层
<>函数名: fseek
功  能: 重定位流上的文件指针
用  法: int fseek(FILE *stream, long offset, int fromwhere);
程序例: <>#include &lt;stdio.h&gt; <>long filesize(FILE *stream); <P>int main(void)
{
   FILE *stream; <P>   stream = fopen("MYFILE.TXT", "w+");
   fprintf(stream, "This is a test");
   printf("Filesize of MYFILE.TXT is %ld bytes\n", filesize(stream));
   fclose(stream);
   return 0;
} <P>long filesize(FILE *stream)
{
   long curpos, length; <P>   curpos = ftell(stream);
   fseek(stream, 0L, SEEK_END);
   length = ftell(stream);
   fseek(stream, curpos, SEEK_SET);
   return length;
}
</P>
 楼主| 发表于 2004-5-8 17:39:45 | 显示全部楼层
<>函数名: fsetpos
功  能: 定位流上的文件指针
用  法: int fsetpos(FILE *stream, const fpos_t *pos);
程序例: <>#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt; <>void showpos(FILE *stream); <P>int main(void)
{
   FILE *stream;
   fpos_t filepos; <P>   /* open a file for update */
   stream = fopen("DUMMY.FIL", "w+"); <P>   /* save the file pointer position */
   fgetpos(stream, &amp;filepos); <P>   /* write some data to the file */
   fprintf(stream, "This is a test"); <P>   /* show the current file position */
   showpos(stream); <P>   /* set a new file position, display it */
   if (fsetpos(stream, &amp;filepos) == 0)
     showpos(stream);
   else
   {
      fprintf(stderr, "Error setting file \
       pointer.\n");
      exit(1);
   } <P>   /* close the file */
   fclose(stream);
   return 0;
} <P>void showpos(FILE *stream)
{
   fpos_t pos; <P>   /* display the current file pointer
      position of a stream */
   fgetpos(stream, &amp;pos);
   printf("File position: %ld\n", pos);
}
  </P>
 楼主| 发表于 2004-5-8 17:39:54 | 显示全部楼层
<>函数名: fstat
功  能: 获取打开文件信息
用  法: int fstat(char *handle, struct stat *buff);
程序例: <>#include &lt;sys\stat.h&gt;
#include &lt;stdio.h&gt;
#include &lt;time.h&gt; <>int main(void)
{
   struct stat statbuf;
   FILE *stream; <P>   /* open a file for update */
   if ((stream = fopen("DUMMY.FIL", "w+"))
       == NULL)
   {
      fprintf(stderr, "Cannot open output \
              file.\n");
      return(1);
   }
   fprintf(stream, "This is a test");
   fflush(stream); <P>   /* get information about the file */
   fstat(fileno(stream), &amp;statbuf);
   fclose(stream); <P>   /* display the information returned */
   if (statbuf.st_mode &amp; S_IFCHR)
      printf("Handle refers to a device.\n");
   if (statbuf.st_mode &amp; S_IFREG)
      printf("Handle refers to an ordinary \
             file.\n");
   if (statbuf.st_mode &amp; S_IREAD)
      printf("User has read permission on \
             file.\n");
   if (statbuf.st_mode &amp; S_IWRITE)
      printf("User has write permission on \
              file.\n"); <P>   printf("Drive letter of file: %c\n",
   'A'+statbuf.st_dev);
   printf("Size of file in bytes: %ld\n",
   statbuf.st_size);
   printf("Time file last opened: %s\n",
   ctime(&amp;statbuf.st_ctime));
   return 0;
}
  
  
</P>
 楼主| 发表于 2004-5-8 17:40:03 | 显示全部楼层
<>函数名: ftell
功  能: 返回当前文件指针
用  法: long ftell(FILE *stream);
程序例: <>#include &lt;stdio.h&gt; <>int main(void)
{
   FILE *stream; <P>   stream = fopen("MYFILE.TXT", "w+");
   fprintf(stream, "This is a test");
   printf("The file pointer is at byte \
          %ld\n", ftell(stream));
   fclose(stream);
   return 0;
}
</P>
 楼主| 发表于 2004-5-8 17:40:15 | 显示全部楼层
<>函数名: fwrite
功  能: 写内容到流中
用  法: int fwrite(void *ptr, int size, int nitems, FILE *stream);
程序例: <>#include &lt;stdio.h&gt; <>struct mystruct
{
  int i;
  char ch;
}; <P>int main(void)
{
   FILE *stream;
   struct mystruct s; <P>   if ((stream = fopen("TEST.$$$", "wb")) == NULL) /* open file TEST.$$$ */
   {
      fprintf(stderr, "Cannot open output file.\n");
      return 1;
   }
   s.i = 0;
   s.ch = 'A';
   fwrite(&amp;s, sizeof(s), 1, stream); /* write struct s to file */
   fclose(stream); /* close file */
   return 0;
}
</P>
您需要登录后才可以回帖 登录 | 注-册-帐-号

本版积分规则

小黑屋|手机版|Archiver|数学建模网 ( 湘ICP备11011602号 )

GMT+8, 2024-4-19 00:52 , Processed in 0.051015 second(s), 12 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表