基本概念
缓冲机制
熟悉的标准 I/O 函数
陌生的标准 I/O 函数
===============================================================================
#include <stdio.h>
void setbuf(FILE *stream, char *buf);
void setbuffer(FILE *stream, char *buf, size_tsize);
void setlinebuf(FILE *stream);
int setvbuf(FILE *stream, char *buf, int mode , size_t size);
===============================================================================
===============================================================================
#include <stdio.h>
int fflush(FILE *stream);
===============================================================================
===============================================================================
#include <stdio.h>
FILE *fopen (const char *path, const char *mode);
FILE *fdopen (int fildes, const char *mode);
FILE *freopen (const char *path, const char *mode, FILE *stream);
===============================================================================
===============================================================================
#include <stdio.h>
int fileno( FILE *stream);
===============================================================================
===============================================================================
#include <stdio.h>
char *tmpnam(char *s);
FILE *tmpfile (void);
char *tempnam(const char *dir, const char *pfx);
===============================================================================
===============================================================================
#include <stdio.h>
#include <stdarg.h>
int vprintf(const char *format, va_list ap);
int vfprintf(FILE *stream, const char *format, va_list ap);
int vsprintf(char *str, const char *format, va_list ap);
int vsnprintf(char *str, size_t size, const char *format, va_list ap);
-------------------------------------------------------------------------------
SAMPLE: myMessageBox 函数
int myMessageBox (HWND hwnd, DWORD dwStyle, char* title, char* text, ...)
{
char * buf = NULL;
int size = 0;
int i = 0;
va_list args;
int rc;
va_start (args, text);
do {
size += 1000;
if (buf) free (buf);
buf = malloc (size);
i = vsnprintf (buf, size, text, args);
} while (i == size);
va_end(args);
rc = MessageBox (hwnd, buf, title, dwStyle);
if (buf)
free (buf);
return rc;
}
===============================================================================
===============================================================================
#include <stdarg.h>
void va_start( va_list ap, last);
type va_arg( va_list ap, type);
void va_end( va_list ap);
-------------------------------------------------------------------------------
SAMPLE: varg.c
#include <stdio.h>
#include <stdarg.h>
void foo(char *fmt, ...)
{
va_list ap;
int d;
char c, *s;
va_start(ap, fmt);
while (*fmt)
switch(*fmt++) {
case 's': /* string */
s = va_arg(ap, char *);
printf("string %s\n", s);
break;
case 'd': /* int */
d = va_arg(ap, int);
printf("int %d\n", d);
break;
case 'c': /* char */
c = va_arg(ap, char);
printf("char %c\n", c);
break;
}
va_end(ap);
}
int main (void)
{
foo ("ssdcds", "Hello", "world.", 50, 'a', 2000, "END");
return 0;
}
===============================================================================
![]()