标准I/O库简介:
什么是标准I/O库
标准C库当中用于文件I/O操作相关的一套库函数,使用标准I/O需要包含头文件
标准I/O和文件I/O之间的区别
- 标准I/O是库函数,而文件I/O是系统调用
- 标准I/O是对文件I/O的封装
- 可移植性:标准I/O相比于文件I/O具有更好的可移植性
- 效率:标准I/O在效率上要优于文件I/O
FILE指针
标准I/O使用FILE指针作为文件句柄,与文件I/O中的文件描述符相似。
fopen()函数
标准I/O中使用fopen()函数打开文件
FILE *fopen(const char *pathname, const char *mode);
参数:
pathname表示文件路径
mode可取如下值之一:
.
读文件和写文件:
读文件 fread()函数
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
写文件 fwrite()函数
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
设置读写位置 fseek()函数
int fseek(FILE *stream, long offset, int whence);
fread不能判断出错或者到文件末尾,需要借助feof和ferror
feof()函数和ferror函数
判断是否到达文件末尾 feof()函数
int feof(FILE *stream);
判断是否发生错误 ferror()函数
int ferror(FILE * stream);
返回值:非0 true 0 false
清除标致 clearerr()函数
void clearerr(FILE *stream);
clearerr和fseek都会清除标致 文件到达末尾会设置end of file标致 出错会设置error标致
格式化I/O
格式化输出
printf()
fprintf()
dprintf()
sprintf()
snprintf()
sprintf()函数和snprintf()函数会在字符串尾端自动加上一个字符串终止字符'\0'
格式化输入
scanf()
fscanf()
sscanf()
stdout 文件指针 1 文件描述符
文件I/O的内核缓冲
read()和write()系统调用在进行文件读写操作的时候并不会直接访问磁盘设备,而是仅仅在用户缓冲区和内核缓冲区之间复制数据。
内核缓冲数据不知道啥时候将数据写入文件。
刷新内核缓冲到磁盘:
fclose后就不需要在close了 对于由文件描述符fdopen得来的文件指针
fileno也是
对文件的读写操作
#include<stdio.h>
int main()
{
FILE *f=NULL;
int ret;
char buf[128]={0};
f=fopen("./text.txt","w+");
if(f==NULL)
{
perror("fopen fail");
return -1;
}
ret=fwrite("hello world",1,11,f);
fflush(f);
if(ret<11)
{
perror("fwrite fail");
return -1;
}
ret=fseek(f,0,SEEK_SET);
if(ret==-1)
{
perror("fseek fail");
fclose(f);
return -1;
}
ret=fread(buf,1,11,f);
if(ret<11)
{
printf("fread fail\n");
fclose(f);
return -1;
}
printf("buf is %s\n",buf);
return 0;
}
#include<stdio.h>
int main()
{
FILE *f=NULL;
int ret;
char buf[128]={0};
f=fopen("./text.txt","w+");
if(f==NULL)
{
perror("open failed");
return -1;
}
ret=fwrite("hello world",1,11,f);
if(ret<11)
{
printf("fwrite error");
fclose(f);
return -1;
}
ret=fseek(f,0,SEEK_SET);
if(-1==ret)
{
perror("fseek error");
fclose(f);
return -1;
}
ret=fread(buf,1,11,f);
if(ret<11)
{
if(ferror(f))
{
printf("fread error\n");
fclose(f);
return -1;
}
else
{
if(feof(f))
{
printf("fread end-of-file\n");
fclose(f);
return -1;
}
}
clearerr(f);
}
printf("%s\n",buf);
fclose(f);
return 0;
}
#include<stdio.h>
#include<unistd.h>
int main()
{
write(1,"hello world",11);
return 0;
}
#include<stdio.h>
int main()
{
FILE *f=NULL;
f=fopen("./text.txt","w+");
if(f==NULL)
{
perror("fopen failed");
return 1;
}
fprintf(f,"hello world %d",10);
fprintf(stdout,"hello world %d",10);
dprintf(1,"hello world %d",10);
return 0;
}
#include<stdio.h>
int main()
{
int data;
char buf1[25];
char buf2[25];
char buf[128]={0};
//scanf("%s",buf);
//fscanf(stdin,"%s",buf);
//printf("%s\n",buf);
sscanf("100 hello world","%d %s %s",&data,buf1,buf2);
printf("%d,%s,%s",data,buf1,buf2);
return 0;
}
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
char buf[4096];
int main()
{
int fd;
fd=open("./text.txt",O_WRONLY|O_CREAT|O_TRUNC,0666);
if(-1==fd)
{
perror("open fail");
return -1;
}
for(int i=0;i<4096;i++)
{
write(fd,buf,sizeof(buf));
}
//fsync(fd);
//fdatasync(fd);
//sync();
close(fd);
return 0;
}
#include<stdio.h>
int main()
{
//if(-1==setvbuf(stdout,NULL,_IONBF,0)){
// perror("setvbuf error");
// return 1;
//}
printf("hello world 1\n");
printf("hello world 2");
fflush(stdout);
while(1)
{
sleep(1);
}
return 0;
}
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
int main()
{
int fd;
FILE *f=NULL;
int ret;
fd=open("./text.txt",O_WRONLY|O_CREAT|O_TRUNC,0644);
if(-1==fd)
{
perror("open failed");
return -1;
}
f=fdopen(fd,"w");
if(f==NULL)
{
perror("f failed");
close(fd);
return -1;
}
ret=fwrite("hello world",1,11,f);
if(ret<11)
{
printf("fwrite failed\n");
fclose(f);
return -1;
}
fclose(f);
return 0;
}