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

📄 f函数.txt

📁 C语言函数..应用示例
💻 TXT
📖 第 1 页 / 共 2 页
字号:
功  能: 填充一个有界区域
用  法: void far floodfill(int x, int y, int border);
程序例:

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main(void)
{
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;
   int maxx, maxy;

   /* initialize graphics, local variables */
   initgraph(&gdriver, &gmode, "");

   /* read result of initialization */
   errorcode = graphresult();
   if (errorcode != grOk)
   /* an error occurred */
   {
      printf("Graphics error: %s\n",
             grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);
      /* terminate with an error code */
   }

   maxx = getmaxx();
   maxy = getmaxy();

   /* select drawing color */
   setcolor(getmaxcolor());

   /* select fill color */
   setfillstyle(SOLID_FILL, getmaxcolor());

   /* draw a border around the screen */
   rectangle(0, 0, maxx, maxy);

   /* draw some circles */
   circle(maxx / 3, maxy /2, 50);
   circle(maxx / 2, 20, 100);
   circle(maxx-20, maxy-50, 75);
   circle(20, maxy-20, 25);

   /* wait for a key */
   getch();

   /* fill in bounded region */
   floodfill(2, 2, getmaxcolor());

   /* clean up */
   getch();
   closegraph();
   return 0;
}
 
 
 

函数名: floor
功  能: 向下舍入
用  法: double floor(double x);
程序例:

#include <stdio.h>
#include <math.h>

int main(void)
{
   double number = 123.54;
   double down, up;

   down = floor(number);
   up = ceil(number);

   printf("original number     %10.2lf\n",
          number);
   printf("number rounded down %10.2lf\n",
          down);
   printf("number rounded up   %10.2lf\n",
          up);

   return 0;
}
 
 
 

函数名: flushall
功  能: 清除所有缓冲区
用  法: int flushall(void);
程序例:

#include <stdio.h>

int main(void)
{
   FILE *stream;

   /* create a file */
   stream = fopen("DUMMY.FIL", "w");

   /* flush all open streams */
   printf("%d streams were flushed.\n",
   flushall());

   /* close the file */
   fclose(stream);
   return 0;
}
 
 
 

函数名: fmod
功  能: 计算x对y的模, 即x/y的余数
用  法: double fmod(double x, double y);
程序例:

#include <stdio.h>
#include <math.h>

int main(void)
{
   double x = 5.0, y = 2.0;
   double result;

   result = fmod(x,y);
   printf("The remainder of (%lf / %lf) is \
          %lf\n", x, y, result);
   return 0;
}
 
 
 

函数名: fnmerge
功  能: 建立新文件名
用  法: void fnerge(char *path, char *drive, char *dir);
程序例:

#include <string.h>
#include <stdio.h>
#include <dir.h>
 

int main(void)
{
    char s[MAXPATH];
    char drive[MAXDRIVE];
    char dir[MAXDIR];
    char file[MAXFILE];
    char ext[MAXEXT];

    getcwd(s,MAXPATH);              /* get the current working directory */
    strcat(s,"\\");                  /* append on a trailing \ character */
    fnsplit(s,drive,dir,file,ext); /* split the string to separate elems */
    strcpy(file,"DATA");
    strcpy(ext,".TXT");
    fnmerge(s,drive,dir,file,ext);   /* merge everything into one string */
    puts(s);                                 /* display resulting string */

    return 0;
}
 
 
 

函数名: fopen
功  能: 打开一个流
用  法: FILE *fopen(char *filename, char *type);
程序例:

#include <stdlib.h>
#include <stdio.h>
#include <dir.h>

int main(void)
{
    char *s;
    char drive[MAXDRIVE];
    char dir[MAXDIR];
    char file[MAXFILE];
    char ext[MAXEXT];
    int flags;

    s=getenv("COMSPEC"); /* get the comspec environment parameter */
    flags=fnsplit(s,drive,dir,file,ext);

    printf("Command processor info:\n");
    if(flags & DRIVE)
       printf("\tdrive: %s\n",drive);
    if(flags & DIRECTORY)
       printf("\tdirectory: %s\n",dir);
    if(flags & FILENAME)
       printf("\tfile: %s\n",file);
    if(flags & EXTENSION)
       printf("\textension: %s\n",ext);

    return 0;
}
 

函数名: fprintf
功  能: 传送格式化输出到一个流中
用  法: int fprintf(FILE *stream, char *format[, argument,...]);
程序例:

/* Program to create backup of the
   AUTOEXEC.BAT file */

#include <stdio.h>

int main(void)
{
   FILE *in, *out;

   if ((in = fopen("\\AUTOEXEC.BAT", "rt"))
       == NULL)
   {
      fprintf(stderr, "Cannot open input \
       file.\n");
      return 1;
   }

   if ((out = fopen("\\AUTOEXEC.BAK", "wt"))
       == NULL)
   {
      fprintf(stderr, "Cannot open output \
       file.\n");
      return 1;
   }

   while (!feof(in))
      fputc(fgetc(in), out);

   fclose(in);
   fclose(out);
   return 0;
}
 
 
 

函数名: FP_OFF
功  能: 获取远地址偏移量
用  法: unsigned FP_OFF(void far *farptr);
程序例:

/* FP_OFF */

#include <dos.h>
#include <stdio.h>

int main(void)
{
   char *str = "fpoff.c";

   printf("The offset of this file in memory\
          is: %Fp\n", FP_OFF(str));

   return 0;
}
 
 

函数名: FP_SEG
功  能: 获取远地址段值
用  法: unsigned FP_SEG(void far *farptr);
程序例:

/* FP_SEG */

#include <dos.h>
#include <stdio.h>

int main(void)
{
   char *filename = "fpseg.c";

   printf("The offset of this file in memory\
   is: %Fp\n", FP_SEG(filename));

   return(0);
}
 
 
 

函数名: fputc
功  能: 送一个字符到一个流中
用  法: int fputc(int ch, FILE *stream);
程序例:

#include <stdio.h>

int main(void)
{
   char msg[] = "Hello world";
   int i = 0;

   while (msg[i])
   {
      fputc(msg[i], stdout);
      i++;
   }
   return 0;
}
 
 
 

函数名: fputchar
功  能: 送一个字符到标准输出流(stdout)中
用  法: int fputchar(char ch);
程序例:

#include <stdio.h>

int main(void)
{
   char msg[] = "This is a test";
   int i = 0;

   while (msg[i])
   {
      fputchar(msg[i]);
      i++;
   }
   return 0;
}
 
 
 

函数名: fputs
功  能: 送一个字符到一个流中
用  法: int fputs(char *string, FILE *stream);
程序例:

#include <stdio.h>

int main(void)
{
   /* write a string to standard output */
   fputs("Hello world\n", stdout);

   return 0;
}
 
 
 

函数名: fread
功  能: 从一个流中读数据
用  法: int fread(void *ptr, int size, int nitems, FILE *stream);
程序例:

#include <string.h>
#include <stdio.h>

int main(void)
{
   FILE *stream;
   char msg[] = "this is a test";
   char buf[20];

   if ((stream = fopen("DUMMY.FIL", "w+"))
       == NULL)
   {
      fprintf(stderr,
              "Cannot open output file.\n");
      return 1;
   }

   /* write some data to the file */
   fwrite(msg, strlen(msg)+1, 1, stream);

   /* seek to the beginning of the file */
   fseek(stream, SEEK_SET, 0);

   /* read the data and display it */
   fread(buf, strlen(msg)+1, 1, stream);
   printf("%s\n", buf);

   fclose(stream);
   return 0;
}
 
 

函数名: free
功  能: 释放已分配的块
用  法: void free(void *ptr);
程序例:

#include <string.h>
#include <stdio.h>
#include <alloc.h>

int main(void)
{
   char *str;

   /* allocate memory for string */
   str = malloc(10);

   /* copy "Hello" to string */
   strcpy(str, "Hello");

   /* display string */
   printf("String is %s\n", str);

   /* free memory */
   free(str);

   return 0;
}
 

函数名: freemem
功  能: 释放先前分配的DOS内存块
用  法: int freemem(unsigned seg);
程序例:

#include <dos.h>
#include <alloc.h>
#include <stdio.h>

int main(void)
{
   unsigned int size, segp;
   int stat;

   size = 64; /* (64 x 16) = 1024 bytes */
   stat = allocmem(size, &segp);
   if (stat < 0)
      printf("Allocated memory at segment:\
      %x\n", segp);
   else
      printf("Failed: maximum number of\
      paragraphs available is %u\n",
      stat);
   freemem(segp);

   return 0;
}
 
 

函数名: freopen
功  能: 替换一个流
用  法: FILE *freopen(char *filename, char *type, FILE *stream);
程序例:

#include <stdio.h>

int main(void)
{
   /* redirect standard output to a file */
   if (freopen("OUTPUT.FIL", "w", stdout)
       == NULL)
      fprintf(stderr, "error redirecting\
              stdout\n");

   /* this output will go to a file */
   printf("This will go into a file.");

   /* close the standard output stream */
   fclose(stdout);

   return 0;
}
 
 
 

函数名: frexp
功  能: 把一个双精度数分解为尾数的指数
用  法: double frexp(double value, int *eptr);
程序例:

#include <math.h>
#include <stdio.h>

int main(void)
{
   double mantissa, number;
   int exponent;

   number = 8.0;
   mantissa = frexp(number, &exponent);

   printf("The number %lf is ", number);
   printf("%lf times two to the ", mantissa);
   printf("power of %d\n", exponent);

   return 0;
}
 
 

函数名: fscanf
功  能: 从一个流中执行格式化输入
用  法: int fscanf(FILE *stream, char *format[,argument...]);
程序例:

#include <stdlib.h>
#include <stdio.h>

int main(void)
{
   int i;

   printf("Input an integer: ");

   /* read an integer from the
      standard input stream */
   if (fscanf(stdin, "%d", &i))
      printf("The integer read was: %i\n",
             i);
   else
   {
      fprintf(stderr, "Error reading an \
              integer from stdin.\n");
      exit(1);
   }
   return 0;
}
 
 
 

函数名: fseek
功  能: 重定位流上的文件指针
用  法: int fseek(FILE *stream, long offset, int fromwhere);
程序例:

#include <stdio.h>

long filesize(FILE *stream);

int main(void)
{
   FILE *stream;

   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;
}

long filesize(FILE *stream)
{
   long curpos, length;

   curpos = ftell(stream);
   fseek(stream, 0L, SEEK_END);
   length = ftell(stream);
   fseek(stream, curpos, SEEK_SET);
   return length;
}
 
 
 
 
 

函数名: fsetpos
功  能: 定位流上的文件指针
用  法: int fsetpos(FILE *stream, const fpos_t *pos);
程序例:

#include <stdlib.h>
#include <stdio.h>

void showpos(FILE *stream);

int main(void)
{
   FILE *stream;
   fpos_t filepos;

   /* open a file for update */
   stream = fopen("DUMMY.FIL", "w+");

   /* save the file pointer position */
   fgetpos(stream, &filepos);

   /* write some data to the file */
   fprintf(stream, "This is a test");

   /* show the current file position */
   showpos(stream);

   /* set a new file position, display it */
   if (fsetpos(stream, &filepos) == 0)
     showpos(stream);
   else
   {
      fprintf(stderr, "Error setting file \
       pointer.\n");
      exit(1);
   }

   /* close the file */
   fclose(stream);
   return 0;
}

void showpos(FILE *stream)
{
   fpos_t pos;

   /* display the current file pointer
      position of a stream */
   fgetpos(stream, &pos);
   printf("File position: %ld\n", pos);
}
 

函数名: fstat
功  能: 获取打开文件信息
用  法: int fstat(char *handle, struct stat *buff);
程序例:

#include <sys\stat.h>
#include <stdio.h>
#include <time.h>

int main(void)
{
   struct stat statbuf;
   FILE *stream;

   /* 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);

   /* get information about the file */
   fstat(fileno(stream), &statbuf);
   fclose(stream);

   /* display the information returned */
   if (statbuf.st_mode & S_IFCHR)
      printf("Handle refers to a device.\n");
   if (statbuf.st_mode & S_IFREG)
      printf("Handle refers to an ordinary \
             file.\n");
   if (statbuf.st_mode & S_IREAD)
      printf("User has read permission on \
             file.\n");
   if (statbuf.st_mode & S_IWRITE)
      printf("User has write permission on \
              file.\n");

   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(&statbuf.st_ctime));
   return 0;
}
 
 
 

函数名: ftell
功  能: 返回当前文件指针
用  法: long ftell(FILE *stream);
程序例:

#include <stdio.h>

int main(void)
{
   FILE *stream;

   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;
}
 
 
 

函数名: fwrite
功  能: 写内容到流中
用  法: int fwrite(void *ptr, int size, int nitems, FILE *stream);
程序例:

#include <stdio.h>

struct mystruct
{
  int i;
  char ch;
};

int main(void)
{
   FILE *stream;
   struct mystruct s;

   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(&s, sizeof(s), 1, stream); /* write struct s to file */
   fclose(stream); /* close file */
   return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -