《UNIX环境高级编程》——读书笔记5, 6

来源:互联网 发布:linux压缩目录命令 编辑:程序博客网 时间:2024/06/05 08:04

5. 标准I/O库

5.1 引言

5.2 流和FILE对象读

流的定向:决定所读、写的字符是单字节or多字节,多字节:宽定向,单字节:字节定向;

#include <stdio.h>

#include <wchar.h>

int fwide( FILE * fp, int mode );

mode < 0, 字节定向;

mode > 0, 宽定向;

mode = 0, 不设置,返回标识该流定向的值。


5.3 标准输入、标准输出和标准出错

<stdio.h>

STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO


5.4 缓冲

(1)全缓冲  malloc, flush, fflush

        磁盘,缓冲区

(2)行缓冲 

        终端

(3)不带缓冲 fputs, stderr


#include <stdio.h>

void setbuf( FILE *restrict fp, char * restrict buf );

int setvbuf(  FILE *restrict fp, char * restrict buf, int mode, size_t size );

mode: _IOFBF 全缓冲, _IOLBF 行缓冲, _IONBF 不带缓冲

int fflush( FILE *fp );


5.5 打开流

#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 );

int fclose( FILE *fp );

type:

r, rb  为读而打开

w, wb 把文件截短至0长,或为写而创建

a, ab 添加,为在文件尾写而打开,或为写而创建

r+, r+b, rb+ 为读和写而打开

w+, w+b, wb+ 把文件截短至0长,或为读和写而打开

a+, a+b, ab+ 为在文件尾和写而打开或创建


5.6 读和写流

每次一个字符的I/O,每次一行的I/O,直接I/O

(1)输入函数

#include <stdio.h>

int getc( FILE *fp );

int fgetc( FILE *fp );

int getchar( void );


int ferror( FILE *fp );

int feof( FILE *fp );

void clearerr( FILE *fp ); 

int ungetc( int c, FILE * fp );

(2)输出函数

#include <stdio.h>

int putc( int c, FILE *fp );

int fputc( int c, FILE *fp );

int putchar( int c );


5.7 每次一行I/O

#include <stdio.h>

char *fgets( char *restrict buf, int n, FILE * restrict fp );

char *gets( char * buf );

char *fputs( const char *restrict buf, FILE * restrict fp );

char *puts( const char * str );


5.8 标准I/O的效率

5.9 二进制I/O

#include <stdio.h>

size_t fread( void * restrict ptr, size_t size, size_t nobj, FILE *restrict fp );

size_t fwrite( const void * restrict ptr, size_t size, size_t nobj, FILE *restrict fp );


5.10 定位流

#include <stdio.h>

long ftell( FILE *fp );

int fseek( FILE *fp, long offset, int whence ); SEEK_SET, SEEK_CUR, SEEK_END

void rewind( FILE * fp );


off_t ftello( FILE *fp );

int fseeko( FILE *fp, off_t offset, int whence );


int fgetpos( FILE *restrict fp, fpos_t *restrict pos );

int fsetpos( FILE *fp, const fpos_t *pos );


5.11 格式化I/O

(1) 格式化输出

#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, ... );

% [flags] [fldwidth] [precision] [lenmodifier] convtype


#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 fp, const char * restrict format, va_list arg );

int vsnprintf( char *restrict buf, size_t n, const char * restrict format, va_list arg );


(2) 格式化输入

#include <stdio.h>

int scanf( const char * restrict format, ... );

int fscanf( FILE *restrict fp, const char *restrict format, ... );

int sscanf( char * restrict buf, const char * restrict format, ... );

% [*] [fldwidth] [lenmodifier] convtype


int vsanf( const char * restrict format, va_list arg );

int vfscanf( FILE *restrict fp, const char * restrict format, va_list arg );

int vsscanf( const char *restrict fp, const char * restrict format, va_list arg );


5.12 实现细节

#include <stdio.h>

int fileno( FILE *fp );


5.13 临时文件

#include <stdio.h>

char *tmpnam( char *ptr );

FILE *tmpfile( void );


int mkstemp( char *template );


5.14 标准I/O的替代软件


6 系统数据文件和信息

6.1 引言

6.2 口令文件

#include <pwd.h>

struct passwd *getpwuid( uid_t uid );

struct passwd *getpwnam( const char *name );


struct passwd *getpwent( void );

void setpwent( void );

void endpwent( void );


6.3 阴影文件

#include <shadow.h>

struct spwd *getspnam( const char *name );

struct spwd *getspent( void );

void setspent( void );

void endspent( void );


6.4 组文件

#include <grp.h>

struct group *getgrent( void );

void setgrent( void );

void endgrent( void );


6.5 附加组ID

#include <unistd.h>

int getgroups( int gidsetsize, gid_t grouplist[] );


#include <grp.h>

#include <unistd.h>

int setgroups( int ngroups, const gid_t grouplist[] );


#include <grp.h>

#include <unistd.h>

int initgroups( const char *usrname, gid_t basegid );


6.6 实现的区别

6.7 其他数据文件

6.8 登录账户记录

6.9 系统标识

#include <sys/utsname.h>

int uname( struct utsname *name );


#include <unistd.h>

int gethostname( char *name, int namelen );


6.10 时间和日期例程

#include <time.h>

time_t time( time_t *calptr );


#include <sys/time.h>

int gettimeofday( struct timeval *restrict tp, void *restrict tzp );

struct timeval{

    time_t tv_sec;

    long tv_usec;

};


#include <time.h>

struct tm *gmtime( const time_t *calptr );

struct tm *localtime( const time_t *calptr );


time_t mktime( struct tm *tmptr );


char *asctime( const struct tm *tmptr );

chat *ctime( const time_t *calptr );


size_t strftime( char *restrict buf, size_t maxsize, const char *restrict format, const struct tm *restrict tmptr );

原创粉丝点击