__flbf() — Determine if a stream is line buffered
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
Language Environment | both | None |
Format
#include <stdio.h>
#include <stdio_ext.h>
int __flbf(FILE *stream);
General description
The __flbf() function determines if the specified stream is line buffered.
Returned value
The __flbf() function returns nonzero if the stream is line buffered. Otherwise, the __flbf() function returns 0. If an error has occurred, __flbf() returns 0 and sets errno to nonzero.
An application wishing to check for error situations should set errno to 0, then call __flbf(), and then check errno. If errno is nonzero, assume that an error has occurred.
- Error Code
- Description
- EBADF
- The stream specified by stream is not valid.
Example
CELEBF84
/* CELEBF84
This example flushes all the line-buffered files.
*/
#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;
}
Output
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...