说明:此部分内容是自己对标准I/O的一些常用的API做了一个简单的总结
1. I/O的定义
标准I/O(Input/Output)是ANSI C标准 (C)库中定义的一系列用于I/O(输入输出)操作的函数。
2.特点
1.它是通过缓冲机制减少系统调用的次数,以达到提高效率的作用
2.围绕流进行操作,流用FILE *描述,FILE表示结构体,包含文件的信息
3.默认打开了三个流:stdin(标准输入)、stdout(标准输出)、stderr(标准错误)(它们的类型为FILE*)
4.只能操作普通文件
3.函数接口
一、FILE *fopen(const char *path, const char *mode);
1.功能:打开文件
2.参数:path:路径 mode:打开方式
3.返回值:成功,文件流 FILE*;失败,NULL,并设置errno
4.案例:
#include <stdio.h>
int main(int argc,char const * argv[])
{
FILE *fp=fopen("test.txt","r");
if(fp==NULL)
{
perror("file opening failed");
return -1;
}
}
//补充打开方式
//r:只读,流被定位到文件开头
//r+:可读可写,流被定位到文件开头
//w:只写,文件不存在创建,存在清空
//w+:可读可写,文件不存在创建,存在清空
//a:追加,文件不存在创建,存在进行末尾写
//a+:读和追加,文件不存在创建,存在进行末尾写,第一次读文件从文件开头读
二、int fgetc(FILE *stream);
1.功能:从文件中读取一个字符
2.参数:stream,文件流
3.返回值:成功,写字符的ascii;失败,EOF
4.案例:
#include <stdio.h>
int main(int argc,char const * argv[])
{
int ch;
FILE *fp=fopen("test.txt","r");
if(fp==NULL)
{
perror("file opening failed");
return -1;
}
//调用一次出一个字符
while((ch=fgetc(fp))!=EOF)
{
printf("%c",ch);
}
}
三,char *fgets(char *s, int size, FILE *stream);
1.功能:从文件中读取一串字符
2.参数:s,存放读取的字符串的首地址;size,读取文件的大小;stream,文件流
3.返回值:成功,读取字符串的首地址;失败或读到文件末尾:NULL
4.补充知识:fgets(buf,32,stdin)
实际最多size-1,组后补 '\0',遇到 '\n' 不再往后继续读,同时能获取到 '\n'
可以防止数组越界
5.案例:
#include <stdio.h>
int main(int argc,char const * argv[])
{
int ch;
FILE *fp=fopen("test.txt","r");
if(fp==NULL)
{
perror("file opening failed");
return -1;
}
char a[32];
fgets(a,5,fp);
printf("%s",a);
}
四、size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
1.功能:从文件流中读取多个元素
2.参数:ptr:用来存放读取元素;size:元素大小(sizeof(数据类型));nmemb:读取对象的个数
stream:要读取的文件
3.返回值:成功:读取对象的个数;读到文件尾:0;失败:-1
五、size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
1.功能:将元素写到流文件当中
2.参数:ptr:用来存放读取元素;size:元素大小(sizeof(数据类型));nmemb:读取对象的个数
stream:要读取的文件
3.返回值:成功:写的元素个数;失败 :-1
4.案例:
#include <stdio.h>
int main(int argc,char const * argv[])
{
int a[3]={10,23,45};
int b[3]={};
FILE *fp=fopen("test.txt","r");
if(fp==NULL)
{
perror("file opening failed");
return -1;
}
//写到流文件中
fwrite(a,sizeof(int),3,fp);
//将指针指向数据首地址
rewind(fp); //等效 fseek(sp,0,SEEK_SET);
fread(b,sizeof(int),3,fp);
for(int t=0;i<3;i++)
{
printf("%d ",b[i]);
}
}
六、int fputc(int c, FILE * stream)
1.功能:向文件中写入一个字符
2.参数:c:要写的字符;stream:文件流
3.返回值:成功:写的字符的ASCII;失败:EOF
七、int fputs(const char *s, FILE *stream);
1.功能:向文件中写字符串
2.参数:s:要写的内容;stream:文件流
3.返回值:成功:非负整数;失败:EOF
八、void rewind(FILE *stream);
功能:将文件位置指针定位到起始位置
九、int fseek(FILE *stream, long offset, int whence);
1.功能:文件的定位操作
2.参数:stream:文件流;offset:偏移量,正数表示向后文件尾部偏移,负数表示向文件开头偏移;
whence:相对位置,
SEEK_SET:相对于文件开头
SEEK_CUR:相对于文件当前位置
SEEK_END:相对于文件末尾
3.返回值:成功:0;失败:-1
注:当打开文件的方式为a或a+时,fseek不起作用
十、long ftell(FILE *stream);
1.功能:获取当前的文件位置
2.参数:要检测的文件流
3.返回值:成功:当前的文件位置,出错:-1
4.案例:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc,char const *argv[])
{
FILE *fd=fopen("test.txt","w");
if(fd==NULL)
{
perror("open error");
return -1;
}
//功能:文件的定位操作
fseek(fd,10,SEEK_SET);
//功能:向文件流中写入一个字符
fputc('a',fd);
fseek(fd,20,SEEK_cuR);
//功能:向文件中写字符串
fputs("hello",fd);
//功能:获取当前的文件位置
printf("%ld\n",ftell(fd));
}
十一、int feof(FILE * stream);
1.功能:判断文件有没有到结尾
2.返回:到达文件末尾,返回非零值
十二、int ferror(FILE * stream);
1.功能:检测文件有没有出错
2.返回:文件出错,返回非零值dsf
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char const *argv[])
{
int ch;
FILE * fp=fopen("test.txt","r");
if(fp==NULL)
{
perror("File opening failed");
return -1;
}
//检测文件有没有出错
if(ferror(fp))
{
printf("error\n");
}
//检测文件有没有到结尾
if(feof(fp))
{
printf("eof\n");
}
return 0;
}
十三、FILE * freopen(const char *pathname, const char *mode, FILE* fp)
1.功能:将指定的文件流重定向到打开的文件中
2.参数:path:文件路径;mode:打开文件的方式(同fopen);fp:文件流指针
3.返回值:成功:返回文件流指针;失败:NULL
#include <stdio.h>
int main(int argc,char const *argv[])
{
printf("hello\n");
//将指定的文件流重定向到打开的文件中
freopen("tang.txt","r+",stdout);
printf("world\n");
//将标准输出流重定向到终端文件
freopen("/dev/tty","r+",stdout);
printf("nihao\n");
return 0;
}