当前位置: 首页 > news >正文

自学嵌入式 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;

}

    

http://www.xdnf.cn/news/704809.html

相关文章:

  • git+svn+sourcetree客户端下载和安装教程
  • DeepSeek R1开源模型的技术突破与AI产业格局的重构
  • Nacos | 三种方式的配置中心,整合Springboot3.x + yaml文件完成 0错误 自动刷新(亲测无误)
  • 单片机——keil5
  • WSL 开发环境搭建指南:Java 11 + 中间件全家桶安装实战
  • STM32开发全解析:从环境搭建到项目实战的技术文档撰写指南
  • 代谢组数据分析(二十五):代谢组与蛋白质组数据分析的异同
  • day13 leetcode-hot100-23(链表2)
  • xLSTM技术介绍
  • 技术文档写作大纲
  • JWT 不对外,Session ID 对外:构建安全可控的微服务认证架构
  • jenkins集成gitlab实现自动构建
  • 力扣-最长回文子串
  • 【课堂笔记】EM算法
  • stm32cube ide如何将工具链替换成arm-none-eabi-gcc
  • 零基础设计模式——结构型模式 - 代理模式
  • 安装flash-attention失败的终极解决方案(WINDOWS环境)
  • 按照状态实现自定义排序的方法
  • 机器学习数据降维方法
  • Apache Airflow
  • 【MySQL】联合查询(下)
  • 微服务各个部分的作用
  • 【基于SpringBoot的图书购买系统】操作Jedis对图书图书的增-删-改:从设计到实战的全栈开发指南
  • 汽车总线分析总结(CAN、LIN、FlexRay、MOST、车载以太网)
  • 机器视觉2,硬件选型
  • [低代码表单生成器设计基础]ElementUI中Layout布局属性Form表单属性详解
  • 华为OD机试真题——矩形相交的面积(2025A卷:100分)Java/python/JavaScript/C/C++/GO最佳实现
  • spring4第4课-ioc控制反转-详解如何注入参数
  • Flutte ListView 列表组件
  • 主流Markdown编辑器的综合评测与推荐