C Tutorial/stdio.h/fflush

Материал из C\C++ эксперт
Перейти к: навигация, поиск

fflush

Item Value Header

  1. include <stdio.h>

Declaration int fflush(FILE *stream); Function force the buffer contents to be written to the file. Return 0 on success or EOF on error.


<source lang="cpp">#include <stdio.h>

 #include <stdlib.h>
 int main(void){
    FILE *fp;
    if((fp=fopen("test", "rb"))==NULL) {
       printf("Cannot open file.\n");
       exit(1);
    }
     char ch = "C";
     int i;
     for(i=0; i<5; i++) {
       fwrite(ch, sizeof(ch), 1, fp);
       fflush(fp);
     }
     fclose(fp);
     return 0;
 }</source>

Flush the buffer

<source lang="cpp">#include <stdio.h>

int main() {

   char a,b;

   printf("Which character is greater?\n");
   printf("Type a single character:");
   a=getchar();
   fflush(stdin);
   printf("Type another character:");
   b=getchar();
   fflush(stdin);

   if(a > b)
   {
       printf(""%c" is greater than "%c"!\n",a,b);
   }
   else if (b > a)
   {
       printf(""%c" is greater than "%c"!\n",b,a);
   }
   else
   {
       printf("Next time, don"t type the same character twice.");
   }
   return(0);

}</source>

Which character is greater?
      Type a single character:2
      Type another character:"2" is greater than "
      "!