본문 바로가기
C 언어

fflush

by SpeeDr00t 2016. 7. 9.
반응형

int fflush(FILE *fp)

#include <errno.h>
#include <stdio.h>
#include "local.h"

/* Flush a single file, or (if fp is NULL) all files.  */
int
fflush(FILE *fp)
{

	if (fp == NULL)
		return (_fwalk(__sflush));
	if ((fp->_flags & (__SWR | __SRW)) == 0) {
		errno = EBADF;
		return (EOF);
	}
	return (__sflush(fp));
}

int
__sflush(FILE *fp)
{
	unsigned char *p;
	int n, t;

	t = fp->_flags;
	if ((t & __SWR) == 0)
		return (0);

	if ((p = fp->_bf._base) == NULL)
		return (0);

	n = fp->_p - p;		/* write this much */

	/*
	 * Set these immediately to avoid problems with longjmp and to allow
	 * exchange buffering (via setvbuf) in user write function.
	 */
	fp->_p = p;
	fp->_w = t & (__SLBF|__SNBF) ? 0 : fp->_bf._size;

	for (; n > 0; n -= t, p += t) {
		t = (*fp->_write)(fp->_cookie, (char *)p, n);
		if (t <= 0) {
			fp->_flags |= __SERR;
			return (EOF);
		}
	}
	return (0);
}
반응형

'C 언어' 카테고리의 다른 글

mkstemp  (0) 2016.07.09
fgetln  (0) 2016.07.09
fprintf  (0) 2016.07.09
fscanf  (0) 2016.07.09
fread  (0) 2016.07.09