Advanced Programming in UNIX Environment Episode 22

二进制I/O

#include <stdio.h>

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

(1)读或写一个二进制数组。
(2)读或写一个结构。
在一个系统上写的数据,要在另一个系统上进行处理。
(1)在一个结构中,同一个成员的偏移量可能的偏移量随编译程序和系统的不同而不同(有不同的对其要求)。
(2)用来存储多字节整数和浮点值的二进制格式在不同的系统结构间也可能不同。

定位流

(1)ftell和fseek函数。
(2)ftello和fseeko函数。
(3)fgetpos和fsetpos函数。

#include <stdio.h>

long ftell(FILE *fp);
long fseek(FILE *fp,long offset,int whense);
void rewind(FILE *fp);

除了偏移量的类型是off_t而非long以外,ftello函数与ftell相同,fseeko函数与fseek相同。

#include <stdio.h>

off_t ftello(FILE *fp);
int fseeko(FILE *fp,off_t offset,int whense);

fgetpos和fsetpos两个函数是ISO C引入的。

#include <stdio.h>

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

格式化I/O

#include <stdio.h>

int printf(const char *restrict format,...);
int fprintf(FILE *restrict fp,const char *restrict format,...);
int dprintf(int fd,...);
int sprintf(char *restrict buf,...);
int snprintf(char *restritc buf,size_t n,...);

printf将格式化数据写到标准输出,fprintf写至指定的流,dprintf写至指定的文件描述符,sprintf将格式化的字符送入数组buf中。 sprintf在该数组的尾端自动加一个null字节,但该字符包括在返回值中。

这是printf族的变体类似于上面的5种。

#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 vdprintf(int fd,va_list arg);
int vsprintf(char *restrict buf,va_list arg);
int vsnprintf(char *restritc buf,va_list arg);

格式化输入

#include <stdio.h>

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

scanf也使用由

#include <stdarg.h>

#include <stdio.h>

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

实现细节

注意,fileno不是ISO C标准部分,而是POSIX.1支持的扩展。

#include <stdio.h>

int fileno(FILE *fp);

如果要调用dup或fcntl等函数,则需要此函数。

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...