標準/拡張機能 | C/C++ | 依存項目 |
---|---|---|
Language Environment | 両方 | なし |
#include <stdio.h>
#include <stdio_ext.h>
int __flbf(FILE *stream);
__flbf() 関数は、指定されたストリームが行バッファリングされているかどうかを判別します。
__flbf() 関数は、ストリームが行バッファリングされている場合、ゼロ以外の値を戻します。それ以外の場合、__flbf() 関数は 0 を戻します。エラーが発生した場合、__flbf() は 0 を戻し、errno をゼロ以外の値に設定します。
エラー状態を検査するアプリケーションは、errno を 0 に設定し、__flbf() を呼び出してから、errno を検査する必要があります。errno がゼロ以外である場合、エラーが発生したと想定されます。
/* CELEBF84
この例では、行バッファリングされたすべてのファイルをフラッシュします。
*/
#include <stdio.h>
#include <stdio_ext.h>
#include <string.h>
#define BUF_SIZE 128
int main(void)
{
char lbuf[BUF_SIZE]; /* line buffer */
char fbuf[BUF_SIZE]; /* full buffer */
char *tagstr = "This file was modified!";
FILE *lfp;
FILE *ffp;
lfp = fopen("newlfile.dat", "a+");
if(lfp == NULL){
perror("Open file failed!¥n");
return -1;
}
if(setvbuf(lfp, lbuf, _IOLBF, sizeof(lbuf)) != 0){ /* set lbuf to line-buffered */
perror("Format line-buffered failed!¥n");
fclose(lfp);
return -1;
}
if (__flbf(lfp)) printf("newlfile.dat is line-buffered¥n");
else printf("newlfile.dat is not line-buffered¥n");
if(fwrite(lfp,strlen(tagstr), 1, lfp) != 1){ /* write tag string to line buffer*/
perror("Write line buffer failed!¥n");
fclose(lfp);
return -1;
}
printf("Write to the line buffered file succeeded¥n");
ffp = fopen("newffile.dat", "a+");
if(ffp == NULL){
perror("Open file failed!¥n");
fclose(lfp);
return -1;
}
if(setvbuf(ffp, fbuf, _IOFBF, sizeof(fbuf)) != 0){ /* set fbuf to full-buffered */
perror("Format full-buffered failed!¥n");
fclose(ffp);
return -1;
}
if (__flbf(ffp)) printf("newffile.dat is line-buffered¥n");
else printf("newffile.dat is not line-buffered¥n");
if(fwrite(tagstr, strlen(tagstr), 1, ffp) != 1){ /* write tag string to full buffer */
perror("Write full buffer failed!¥n");
fclose(lfp);
fclose(ffp);
return -1;
}
printf("Write to the full buffered file succeeded¥n");
_flushlbf(); /* flush line buffered files */
printf("Only line buffered files are flushed...¥n");
fclose(lfp);
fclose(ffp);
return 0;
}
newlfile.dat is line-buffered
Write to the line buffered file succeeded
newffile.dat is not line-buffered
Write to the full buffered file succeeded
Only line buffered files are flushed...