fflush flush 很晕

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值