《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 );
- 《UNIX环境高级编程》——读书笔记5, 6
- 《unix环境高级编程》 读书笔记 (6)
- Unix环境高级编程读书笔记(6)
- 《unix环境高级编程》 读书笔记 (5)
- Unix环境高级编程读书笔记(5)
- UNIX环境高级编程读书笔记(5)
- 读书笔记——《UNIX环境高级编程》第一章_UNIX基础知识
- 《UNIX环境高级编程》——读书笔记14
- 《UNIX环境高级编程》——读书笔记16
- 《UNIX环境高级编程》——读书笔记3, 4
- 《UNIX环境高级编程》读书笔记 —— 文件 I/O
- UNIX环境高级编程读书笔记(四)—出错处理
- UNIX环境高级编程读书笔记(十三)—守护进程
- UNIX环境高级编程读书笔记(四)—出错处理
- UNIX环境高级编程读书笔记(十三)—守护进程
- UNIX环境高级编程读书笔记
- UNIX环境高级编程读书笔记
- Unix 环境高级编程-读书笔记
- 有用的命令
- 数据结构 线性表
- 《UNIX环境高级编程》——读书笔记3, 4
- C++ vector容器简单解释
- 常用正则表达式推荐
- 《UNIX环境高级编程》——读书笔记5, 6
- openstack 镜像制作
- $monitor(...)//$display(...)
- 为什么竞价广告的计费价格是竞价排名第2的价格而非竞价排名第1的价格呢?
- android编译全过程
- 关于将HTML导出到Excel中时样式的问题
- 通过写framebuffer显示BMP图片
- java 调用 oracle 中procedure
- 《UNIX环境高级编程》——读书笔记7, 8, 9, 10