自学嵌入式 day 25 - 系统编程 标准io 缓冲区 文件io
(3)二进制文件读写函数:
①fread:
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
功能:从指定的stream流对象中获取nmemeb个大小为size字节的数据块到ptr所在的本地内存中。
参数:ptr 要存储数据的本地内存一般是数组或者结构体指针
size 单个数据块的元数据大小。最小单元的大小
nmemb 要获取的数据块的个数,拷贝的数据块个数。
stream 要获取数据的源文件流对象,如果是stdin表示从键盘获取数据,如果是fp文件则表示从普通文件获取。
返回值:成功 小于等于nemeb的整数,表示获取的数据长度
失败 小于0,结尾 0;
#include<stdio.h>
typedef struct
{
char name[10];
int age;
char sex;
}PRE;int main()
{
FILE* fp = fopen("1.txt","r");
if(NULL == fp)
{
fprintf(stderr,"fopen error");
return 1;
}
PRE p = {0};int ret = fread(&p,sizeof(PRE),1,fp);
if(ret > 0)
{
printf("ret = %d,name:%s,age:%d,sex:%c\n",ret,p.name,p.age,p.sex);
}
fclose(fp);
return 0;}
②fwrite:
size_t fwrite(const void *ptr, size_t size,size_t nmemb, FILE *stream);
功能:从ptr所在本地内存中取出nmemb个大小为size的数据块写入到stream流对应的文件流对象中。
参数:ptr 要写的数据块地址,一般是数组或者结构体指针
size 要写的数据块元数据大小,单位是字节
nmemb 要写的数据块的个数
stream 要写的目标文件流对象。如果是stdout则表示数据会写到终端屏幕显示,如果是fp的普通文件则会写入到文件中。
返回值:成功 小于等于nmemb 的个数。
失败 <0
#include<stdio.h>
#include<string.h>typedef struct
{
char name[10];
int age;
char sex;
}PRE;
int main()
{
FILE* fp = fopen("1.txt","w");
if(NULL == fp)
{
fprintf(stderr,"fopen error\n");
return 1;
}
PRE p;
strcpy(p.name,"zhangsan");
p.age = 11;
p.sex = 'M';
fwrite(&p,sizeof(PRE),1,fp);fclose(fp);
return 0;
}
2.标准io之文件定位
(1)函数:
①fseek:
int fseek(FILE *stream, long offset, int whence);
功能:将stream流文件中的文件指针从whence位置开始偏移offset字节的长度。
参数:stream 要移动文件指针的目标文件流对象。
注意:不支持设备文件,一般用于普通文件。
offset 要在文件内偏移的距离,单位字节。如果值为整数,则向文件末尾偏移。如果值为负数,则向文件开头偏移
whence 偏移的起始位置,由系统定义的三个宏开始。
SEEK_SET 文件的开头位置
SEEK_CUR 文件的当前位置
SEEK_END 文件的末尾位置
返回值:成功: 返回 0
失败: -1;
#include<stdio.h>
int main()
{
FILE* fp = fopen("1.txt","r");
if(NULL == fp)
{
fprintf(stderr,"fopen error\n");
return 1;
}
fseek(fp,1,SEEK_SET);
char buf[1024] = {0};
fgets(buf,sizeof(buf),fp);
printf("%s",buf);
return 0;
}
②rewind:
rewind() 等效于:fseek(stream,0L,SEEK_SET);
#include<stdio.h>
int main()
{
FILE* fp = fopen("2.txt","r");
if(NULL == fp)
{
fprintf(stderr,"fopen error\n");
return 1;
}
fseek(fp,0,SEEK_END);
long size = ftell(fp);
printf("%ld\n",size);
rewind(fp);
char buf[1024] = {0};
fgets(buf,sizeof(buf),fp);
printf("%s",buf);
return 0;
}
③ ftell:
long ftell(FILE *stream);rewind(fp);
功能:获取当前文件流指针的具体位置,一般以文件开头到当前指针的字节数为返回值。
参数:stream 要返回指针距离的文件流对象
返回值:成功 获取到的距离长度,单位是字节
失败 -1;
#include<stdio.h>
int main()
{
FILE* fp = fopen("2.txt","r");
if(NULL == fp)
{
fprintf(stderr,"fopen error\n");
return 1;
}
fseek(fp,0,SEEK_END);
long size = ftell(fp);
printf("%ld\n",size);return 0;
}
注:①stdin:标准输入
②stdout:标准输出
③stderr:标准错误输出
#include <stdio.h>
int main(int argc, char *argv[])
{
char buf[100]={0};fgets(buf,sizeof(buf),stdin);
fputs(buf,stdout);
fprintf(stderr,"%s 123",buf);
return 0;
}
(4)缓冲区:
①行缓冲:大小:1kb
作用:主要用于人机交互
刷新条件:
1.遇到\n刷新
2.缓存区满刷新
3.程序结束刷新
4.fflush刷新 fflush(stdout); FILE*fp stdin stdout stderr
②全缓冲:大小:4k
作用:主要用于文件的读写
刷新条件:
1.缓存区满刷新
2.程序结束刷新
3.fflush来刷新 fflush(fp);
③无缓冲:大小:0k
作用:主要用于出错处理信息的输出 stderr
3.文件io:
(1)定义:操作系统为了方便用户使用系统功能而对外提供的一组系统函数。称之为 系统调用 其中有个文件IO,一般都是对设备文件操作,当然也可以对普通文件进行操作
(2)函数:
①open:
int open(const char *pathname, int flags,int mode);
功能:获得一个文件描述符
参数:
pathname:文件名
flags:
O_CREAT, 创建文件
O_EXCL,需要和O_CREAT同时使用,表示新建的文件不能存在,成功,否则open就会失败
O_NOCTTY,不是终端设备
O_TRUNC文件内容清空
O_APPEND追加
O_ASYNC异步io,什么时候io不确定,
O_NONBLOCK非阻塞
注:多个宏可写成:宏 | 宏,“|”:二进制或
返回值:成功返回文件描述符
失败返回-1
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int a = 12312;
int fd = open("1.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (-1 == fd)
{
fprintf(stderr, "open error\n");
return 1;
}
// system("pause");
return 0;
}
②write
ssize_t write(int fd, const void *buf, size_t count);
功能:通过文件描述符向文件中写一串数据
参数:
fd:文件描述符
buf:要写入文件的字符串的首地址
count:要写入字符的个数
返回值:成功返回实际写入的个数
失败返回-1
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int a = 12312;
int fd = open("1.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (-1 == fd)
{
fprintf(stderr, "open error\n");
return 1;
}
char buf[1024] = "hello";
ssize_t ret = write(fd, buf, strlen(buf));
printf("write ret:%ld\n", ret);
close(fd);
return 0;
}
③read:
ssize_t read(int fd, void *buf, size_t count);
功能:通过文件描述符读取文件中的数据
参数:
fd:文件描述符
buf:存放数据空间的首地址
count:要读到数据的个数
返回值:成功返回读到数据的个数
失败返回-1
读到文件结尾返回0
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int fd = open("1.txt", O_RDONLY);
if (-1 == fd)
{
fprintf(stderr, "open error\n");
return 1;
}
char buf[1024] = {0};
ssize_t ret = read(fd,buf,sizeof(buf));
printf("readret:%ld %s\n", ret,buf);
close(fd);
return 0;
}