char *fgets(char *restrict buf, int n, FILE *restrict fp);
fgets : [b]including[/b] the next newline,but no more than n-1 characters,into the buffer.
char *gets(char *buf);
it [b]doesn't store the newline [/b]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, [b]but puts then writes a newline character to the standard output.[/b]
===========================================
[b]fflush[/b]
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.
[b]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, [u]the fflush function causes any unwritten data for that [i]stream [/i]to be delivered to the host environment to be written to the [i]file[/i]; [/u]otherwise, the behavior is undefined.[/b]
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 [b]with the same name and behavior (see ostream::flush).[/b]
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); [b]// flushing or repositioning required[/b]
fgets(mybuffer,80,pFile);
printf("%s----",mybuffer);
puts(mybuffer);
fclose (pFile);
}
return 0;
}
fgets : [b]including[/b] the next newline,but no more than n-1 characters,into the buffer.
char *gets(char *buf);
it [b]doesn't store the newline [/b]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, [b]but puts then writes a newline character to the standard output.[/b]
===========================================
[b]fflush[/b]
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.
[b]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, [u]the fflush function causes any unwritten data for that [i]stream [/i]to be delivered to the host environment to be written to the [i]file[/i]; [/u]otherwise, the behavior is undefined.[/b]
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 [b]with the same name and behavior (see ostream::flush).[/b]
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); [b]// flushing or repositioning required[/b]
fgets(mybuffer,80,pFile);
printf("%s----",mybuffer);
puts(mybuffer);
fclose (pFile);
}
return 0;
}