第五章 标准IO库:
stdin, stdout, stderr
#include <stdio.h>
#include <wchar.h>
int fwide(FILE * fp, int mode);
we need clear error before call it,
and check with ferror after call it
#include <stdio.h>
void setbuf(FILE * restrict fp, char * restrict buf);
if buf is NULL, means close buffer, to be unbufferd
else size of buf should >= BUFSIZ (BUFSIZ defined in stdio.h)
int setvbuf(FILE * restrict fp, char * restrict buf, int mode, size_t size);
mode: _IOFBF, _IOLBF, _IONBF
we'd better set buf as NULL to let function malloc suit size itself
ret is 0 means success
#include <stdio.h>
int fflush(FILE * fp);
if fp is NULL, fflush all ostream
ret is 0 means success, or ret is EOF
#include <stdio.h>
FILE * fopen(const char * restrict pathname, const char * restrict type);
FILE * freopen(const char * restrict pathname, const char * restrict type,
FILE * restrict fp);
FILE * fdopen(int filedes, const char * type);
type:
r, rb; w, wb; a, ab; r+, rb+, r+b; w+, wb+, w+b; a+, ab+, a+b.
#include <stdio.h>
int fclose(FILE * fp);
ret is 0 means success, or ret is EOF
#include <stdio.h>
int getc(FILE * fp);
maybe it's a macro, may we cannot past getc as a argu, maybe it is faster
int fgetc(FILE * fp);
it is safer, but maybe slowwer than getc, just maybe
int getchar();
same as getc(stdin)
#include <stdio.h>
int ferror(FILE * fp);
ret is 0 means False
int feof(FILE * fp);
ret is 0 means False
void clearerr(FILE * fp);
#include <stdio.h>
int ungetc(int c, FILE * fp);
ret == c means success, or ret is EOF
#include <stdio.h>
int putc(int c, FILE * fp);
maybe it's a macro, may we cannot past putc as a argu, maybe it is faster
int fputc(int c, FILE * fp);
it is safer, but maybe slowwer than putc, just maybe
int putchar(int c);
same as putc(c, stdout);
ret == c means success, or ret is EOF
#include <stdio.h>
char * fgets(char * restrict buf, int n, FILE * restrict fp);
we need handle '\n' ourselves
char * gets(char * buf);
buf maybe overflow, so gets is not safe, and it auto drop '\n' itself
#include <stdio.h>
int fputs(const char * restrict str, FILE * restrict fp);
we need send '\n' to str, if we need a new line
int puts(const char * str);
safe, but it auto add a '\n' to the str tail itself
ret is EOF means error
#include <stdio.h>
size_t fread(void * restrict ptr, size_t size, size_t nobj,
FILE * restrict fp);
if ret < nobj, use ferror or feof to check it
size_t fwrite(const void * restrict ptr, size_t size, size_t nobj,
FILE * restrict fp);
if ret < nobj, means error
#include <stdio.h>
long ftell(FILE * fp);
ret is -1 means failed
long fseek(FILE * fp, long offset, int whence);
whence: SEEK_SET, SEEK_CUR, SEEK_END
SEEK_END may not implement in some OS, UNIX surpport it
ret is 0 means success
void rewind(FILE * fp);
#include <stdio.h>
off_t ftello(FILE * fp);
ret is -1 means failed
int fseeko(FILE * fp, off_t offset, int whence);
ret is 0 means success
#include <stdio.h>
int fgetpos(FILE * restrict fp, fpos_t * restrict pos);
ret is 0 means success
int fsetpos(FILE * fp, const fpos_t * pos);
ret is 0 means success
#include <stdio.h>
int printf(const char * restrict format, ...);
int fprintf(FILE * restrict fp, const char * restrict format, ...);
int sprintf(char * restrict buf,const char * restrict format, ...);
int snprintf(char * restrict buf, size_t n, const char * restrict format, ...);
format: %[flags][fldwidth][precision][lenmodifier]convtype
flags: -, +, (space), (#another-convtype), 0
fldwidth: number or *
precision: number or *
lenmodifier: hh, h, l, ll, j, z, t, L
convtype: d, i; o; u; x, X; a, A; e, E; f, f; g, G;
c; s; p; n; %; C; S
ret < 0, means failed
#include <stdarg.h>
#include <stdio.h>
int vprintf(const char * restrict format, va_list arg);
int vfprintf(FILE * restrict fp, const char * restrict format, va_list arg);
int vsprintf(char * restrict buf, const char * restrict format, va_list arg);
int vsnprintf(char * restrict buf, size_t n, const char * restrict format,
va_list arg);
ret < 0, means failed
#include <stdio.h>
int scanf(const char * restrict format, ...);
int fscanf(FILE * restrict fp, const char * restrict format, ...);
int sscanf(const char * restrict buf, const char * restrict format, ...);
format: %[*][fldwidth][lenmodifier]convtype
*: convert, but not update that element
fldwidth: number
lenmodifier: hh, h, l, ll, j, z, t, L
convtype: d; i; o; u; x; a, A, e, E, f, F, g, G;
c, s, [, [^, p, n, %, C; S
ret is EOF, means failed
#include <stdarg.h>
#include <stdio.h>
int vscanf(const char * restrict format, va_list arg);
int vfscanf(FILE * restrict fp, const char * restrict format, va_list arg);
int vsscanf(const char * restrict buf, const char * restrict format,
va_list arg);
ret is EOF, means failed
#include <stdio.h>
int fileno(FILE * fp);
#include <stdio.h>
char * tmpnam(char * ptr);
call TMP_MAX times at most (stdio.h)
if ptr is NULL, name in a static block, means cannot call twice
else size of ptr need >= L_tmpnam (stdio.h)
FILE * tmpfile(void);
create a temp file, and auto unlink it soon (not close it)
ret is NULL, means failed
#include <stdio.h>
char * tempnam(const char * directory, const char * prefix);
if prefix not NULL, length of prefix need <= 5
#include <stdlib.h>
int mkstemp(char * template);
template like "fileXXXXXX", last 6 X will be replace by function
this new file will not unlink itself
ret is a file descriptor
示例:
#include <stdio.h>
int
main(int argc, char * argv[])
{
int c;
while ((c = getc(stdin)) != EOF) {
if (putc(c, stdout) == EOF) {
printf("output error\n");
return -1;
}
}
if (ferror(stdin)) {
printf("input error\n");
return -1;
}
return 0;
}
#include <stdio.h>
int
main(int argc, char * argv[])
{
const int MAXLINE = 4096;
char buf[MAXLINE];
while (fgets(buf, MAXLINE, stdin) != NULL) {
if (fputs(buf, stdout) == EOF) {
printf("output error\n");
return -1;
}
}
if (ferror(stdin)) {
printf("input error\n");
return -1;
}
return 0;
}
#include <stdio.h>
void
pr_stdio(const char * name, FILE * fp)
{
printf("stream = %s, ", name);
if (fp->_IO_file_flags & _IO_UNBUFFERED) {
printf("unbuffered");
}
else if (fp->_IO_file_flags & _IO_LINE_BUF) {
printf("line buffered");
}
else {
printf("fully buffered");
}
printf(", buffer size = %d\n", fp->_IO_buf_end - fp->_IO_buf_base);
}
int
main(int argc, char * argv[])
{
FILE * fp;
fputs("enter any character\n", stdout);
if (getchar() == EOF) {
printf("getchar error\n");
return -1;
}
fputs("one line to standard error\n", stderr);
pr_stdio("stdin", stdin);
pr_stdio("stdout", stdout);
pr_stdio("stderr", stderr);
if ((fp = fopen("/etc/motd", "rb")) == NULL) {
printf("fopen error\n");
return -1;
}
if (fgetc(fp) == EOF && ferror(fp)) {
printf("fgetc error\n");
return -1;
}
pr_stdio("/etc/motd", fp);
return 0;
}
#include <stdio.h>
int
main(int argc, char * argv[])
{
FILE * fp;
const int MAXLINE = 4096;
char line[MAXLINE];
char name[L_tmpnam];
printf("%s\n", tmpnam(NULL));
printf("%s\n", tmpnam(name));
printf("%s\n", name);
if ((fp = tmpfile()) == NULL) {
printf("tmpfile error\n");
return -1;
}
if (fputs("one line to output\n", fp) == EOF) {
printf("fputs error\n");
return -1;
}
rewind(fp);
if (fgets(line, MAXLINE, fp) == NULL) {
printf("fgets error\n");
return -1;
}
if (fputs(line) == EOF) {
printf("puts error\n");
return -1;
}
return 0;
}
#include <stdio.h>
int
main(int argc, char * argv[])
{
if (argc != 3) {
printf("usage: ./a.out <directory> <prefix>\n");
return 0;
}
printf("%s\n", tempnam(argv[1][0] != ' ' ? argv[1] : NULL,
argv[2][0] != ' ' ? argv[2] : NULL));
return 0;
}