数模论坛

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

C语言的函数!

  [复制链接]
 楼主| 发表于 2004-5-8 17:33:54 | 显示全部楼层
<>函数名: farmalloc
功  能: 从远堆中分配存储块
用  法: void far *farmalloc(unsigned long size);
程序例: <>#include &lt;stdio.h&gt;
#include &lt;alloc.h&gt;
#include &lt;string.h&gt;
#include &lt;dos.h&gt; <>int main(void)
{
   char far *fptr;
   char *str = "Hello"; <P>   /* allocate memory for the far pointer */
   fptr = farmalloc(10); <P>   /* copy "Hello" into allocated memory */
   /*
      Note: movedata is used because we might
      be in a small data model, in which case
      a normal string copy routine can not be
      used since it assumes the pointer size
      is near.
   */
   movedata(FP_SEG(str), FP_OFF(str),
     FP_SEG(fptr), FP_OFF(fptr),
     strlen(str)); <P>   /* display string (note the F modifier) */
   printf("Far string is: %Fs\n", fptr); <P>   /* free the memory */
   farfree(fptr); <P>   return 0;
}
  
</P>
 楼主| 发表于 2004-5-8 17:34:03 | 显示全部楼层
<>函数名: farrealloc
功  能: 调整远堆中的分配块
用  法: void far *farrealloc(void far *block, unsigned long newsize);
程序例: <>#include &lt;stdio.h&gt;
#include &lt;alloc.h&gt; <>int main(void)
{
   char far *fptr; <P>   fptr = farmalloc(10);
   printf("First address: %Fp\n", fptr);
   fptr = farrealloc(fptr,20);
   printf("New address  : %Fp\n", fptr);
   farfree(fptr);
   return 0;
}
  
  </P>
 楼主| 发表于 2004-5-8 17:34:12 | 显示全部楼层
<>函数名: fclose
功  能: 关闭一个流
用  法: int fclose(FILE *stream);
程序例: <>#include &lt;string.h&gt;
#include &lt;stdio.h&gt; <>int main(void)
{
   FILE *fp;
   char buf[11] = "0123456789"; <P>   /* create a file containing 10 bytes */
   fp = fopen("DUMMY.FIL", "w");
   fwrite(&amp;buf, strlen(buf), 1, fp); <P>   /* close the file */
   fclose(fp);
   return 0;
}
  
</P>
 楼主| 发表于 2004-5-8 17:34:22 | 显示全部楼层
<>函数名: fcloseall
功  能: 关闭打开流
用  法: int fcloseall(void);
程序例: <>#include &lt;stdio.h&gt; <>int main(void)
{
   int streams_closed; <P>   /* open two streams */
   fopen("DUMMY.ONE", "w");
   fopen("DUMMY.TWO", "w"); <P>   /* close the open streams */
   streams_closed = fcloseall(); <P>   if (streams_closed == EOF)
      /* issue an error message */
      perror("Error");
   else
      /* print result of fcloseall() function */
      printf("%d streams were closed.\n", streams_closed); <P>   return 0;
}
</P>
 楼主| 发表于 2004-5-8 17:34:32 | 显示全部楼层
<>函数名: fcvt
功  能: 把一个浮点数转换为字符串
用  法: char *fcvt(double value, int ndigit, int *decpt, int *sign);
程序例: <>#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;conio.h&gt; <>int main(void)
{
   char *string;
   double value;
   int dec, sign;
   int ndig = 10; <P>   clrscr();
   value = 9.876;
   string = ecvt(value, ndig, &amp;dec, &amp;sign);
   printf("string = %s      dec = %d \
          sign = %d\n", string, dec, sign); <P>   value = -123.45;
   ndig= 15;
   string = ecvt(value,ndig,&amp;dec,&amp;sign);
   printf("string = %s dec = %d sign = %d\n",
          string, dec, sign);
  <P>   value = 0.6789e5; /* scientific
                        notation */
   ndig = 5;
   string = ecvt(value,ndig,&amp;dec,&amp;sign);
   printf("string = %s           dec = %d\
          sign = %d\n", string, dec, sign); <P>   return 0;
}
  
</P>
 楼主| 发表于 2004-5-8 17:34:41 | 显示全部楼层
<>函数名: fdopen
功  能: 把流与一个文件句柄相接
用  法: FILE *fdopen(int handle, char *type);
程序例: <>#include &lt;sys\stat.h&gt;
#include &lt;stdio.h&gt;
#include &lt;fcntl.h&gt;
#include &lt;io.h&gt; <>int main(void)
{
   int handle;
   FILE *stream; <P>   /* open a file */
   handle = open("DUMMY.FIL", O_CREAT,
    S_IREAD | S_IWRITE); <P>   /* now turn the handle into a stream */
   stream = fdopen(handle, "w"); <P>   if (stream == NULL)
      printf("fdopen failed\n");
   else
   {
      fprintf(stream, "Hello world\n");
      fclose(stream);
   }
   return 0;
}
  
  </P>
 楼主| 发表于 2004-5-8 17:34:50 | 显示全部楼层
<>函数名: feof
功  能: 检测流上的文件结束符
用  法: int feof(FILE *stream);
程序例: <>#include &lt;stdio.h&gt; <>int main(void)
{
   FILE *stream; <P>   /* open a file for reading */
   stream = fopen("DUMMY.FIL", "r"); <P>   /* read a character from the file */
   fgetc(stream); <P>   /* check for EOF */
   if (feof(stream))
      printf("We have reached end-of-file\n"); <P>   /* close the file */
   fclose(stream);
   return 0;
}
  
</P>
 楼主| 发表于 2004-5-8 17:34:59 | 显示全部楼层
<>函数名: ferror
功  能: 检测流上的错误
用  法: int ferror(FILE *stream);
程序例: <>#include &lt;stdio.h&gt; <>int main(void)
{
   FILE *stream; <P>   /* open a file for writing */
   stream = fopen("DUMMY.FIL", "w"); <P>   /* force an error condition by attempting to read */
   (void) getc(stream); <P>   if (ferror(stream))  /* test for an error on the stream */
   {
      /* display an error message */
      printf("Error reading from DUMMY.FIL\n"); <P>      /* reset the error and EOF indicators */
      clearerr(stream);
   } <P>   fclose(stream);
   return 0;
}
  
  
</P>
 楼主| 发表于 2004-5-8 17:35:09 | 显示全部楼层
<>函数名: fflush
功  能: 清除一个流
用  法: int fflush(FILE *stream);
程序例: <>#include &lt;string.h&gt;
#include &lt;stdio.h&gt;
#include &lt;conio.h&gt;
#include &lt;io.h&gt; <>void flush(FILE *stream); <P>int main(void)
{
   FILE *stream;
   char msg[] = "This is a test"; <P>   /* create a file */
   stream = fopen("DUMMY.FIL", "w"); <P>   /* write some data to the file */
   fwrite(msg, strlen(msg), 1, stream); <P>   clrscr();
   printf("Press any key to flush\
   DUMMY.FIL:");
   getch(); <P>   /* flush the data to DUMMY.FIL without\
      closing it */
   flush(stream); <P>   printf("\nFile was flushed, Press any key\
   to quit:");
   getch();
   return 0;
} <P>void flush(FILE *stream)
{
     int duphandle; <P>     /* flush the stream's internal buffer */
     fflush(stream); <P>     /* make a duplicate file handle */
     duphandle = dup(fileno(stream)); <P>     /* close the duplicate handle to flush\
        the DOS buffer */
     close(duphandle);
}
  
  
  </P>
 楼主| 发表于 2004-5-8 17:35:22 | 显示全部楼层
<>函数名: fgetc
功  能: 从流中读取字符
用  法: int fgetc(FILE *stream);
程序例: <>#include &lt;string.h&gt;
#include &lt;stdio.h&gt;
#include &lt;conio.h&gt; <>int main(void)
{
   FILE *stream;
   char string[] = "This is a test";
   char ch; <P>   /* open a file for update */
   stream = fopen("DUMMY.FIL", "w+"); <P>   /* write a string into the file */
   fwrite(string, strlen(string), 1, stream); <P>   /* seek to the beginning of the file */
   fseek(stream, 0, SEEK_SET); <P>   do
   {
      /* read a char from the file */
      ch = fgetc(stream); <P>      /* display the character */
      putch(ch);
   } while (ch != EOF); <P>   fclose(stream);
   return 0;
}
  
  
</P>
您需要登录后才可以回帖 登录 | 注-册-帐-号

本版积分规则

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

GMT+8, 2021-11-29 20:38 , Processed in 0.076884 second(s), 12 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

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