fflush flush 很晕

char *fgets(char *restrict buf, int n, FILE *restrict fp);
fgets : including the next newline,but no more than n-1 characters,into the buffer.

char *gets(char *buf);
it doesn't store the newline in the buffer.

int fputs(const char *restrict str, FILE *restrict fp);
fputs writes the null-terminated string to the specified stream, the null byte at the  end is not written.

int puts(const char *str);
the puts function writes the null-terminated string to the standard output, without writing the null byte, but puts then writes a newline character to the standard output.


===========================================

fflush

int fflush ( FILE * stream );
this function causes any unwritten data for the stream to be passed to the kernel, as special case, if fp is NULL, this function causes all output streams to be flushed.

fflush is meant to be called on an output stream. This is an excerpt from the C standard:

int fflush(FILE *ostream);

ostream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.



flush

ostream& flush(ostream& os);
Synchronizes the buffer associated with the stream to its controlled output sequence.This effectively means that all unwritten characters in the buffer are written to its controlled output sequence as soon as possible ("flushed").

// Flushing files (flush manipulator)
#include <fstream>
using namespace std;

int main () {

  ofstream outfile ("test.txt");

  for (int n=0; n<100; n++)
    outfile << n << flush;

  outfile.close();

  return 0;
}

Standard output streams also have a member function with the same name and behavior (see ostream::flush).


ostream::flush
ostream& flush ( );
Flush output stream buffer
Synchronizes the buffer associated with the stream to its controlled output sequence. This effectively means that all unwritten characters in the buffer are written to its controlled output sequence as soon as possible ("flushed").

The function only has meaning for buffered streams, in which case it effectively calls the pubsync member of the streambuf object (rdbuf()->pubsync()) associated to the stream.

A manipulator exists with the same name and behavior (see flush manipulator).


// Flushing files
#include <fstream>
using namespace std;

int main () {

  ofstream outfile ("test.txt");

  for (int n=0; n<100; n++)
  {
    outfile << n;
    outfile.flush();
  }

  outfile.close();

  return 0;
}


When this example is executed the content of the file test.txt is updated 100 times.


================================

/* fflush example */
#include <stdio.h>
char mybuffer[80];
int main()
{
   FILE * pFile;
   pFile = fopen ("example.txt","r+");
   if (pFile == NULL) perror ("Error opening file");
   else {
     fputs ("test",pFile);
     fflush (pFile);    // flushing or repositioning required
     fgets(mybuffer,80,pFile);
printf("%s----",mybuffer);
     puts(mybuffer);
     fclose (pFile);
   }
   return 0;
}

猜你喜欢

转载自wwwjjq.iteye.com/blog/1648044