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

将整数,结构体,结构体数组,链表写到文件

在之前的学习中,忘文件中写的内容都是字符串或字符,本节学习如何写入其他各种类型的数据。

回看write和read函数的形式:

ssize_t write(int fd, const void *buf, size_t count);
ssize_t read(int fd, void *buf, size_t count);

其中,第二个参数都是一个无类型的指针,只不过之前一直将这里定义为一个字符串,其实,这个指针可以指向各种形式数据的地址。 

写入一个整数

demo7.c:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>int main()
{int fd; // file descriptionint data = 100;int data2 = 0;fd = open("./file1",O_RDWR|O_CREAT|O_TRUNC, 0600);printf("file description = %d, open successfully!\n",fd);write(fd, &data, sizeof(int));lseek(fd,0,SEEK_SET);read(fd, &data2, sizeof(int));printf("context:%d\n",data2);close(fd); //close after writing return 0;
}

运行代码:

注意,如果此时打开file1:(此时需要使用vi打开)

发现是乱码,但是这并不影响程序运行时的读取和写入

写入一个结构体

demo7.c:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>struct Test
{int i;char c;
};int main()
{int fd; // file descriptionstruct Test data = {30,'k'};struct Test data2;fd = open("./file1",O_RDWR|O_CREAT|O_TRUNC, 0600);printf("file description = %d, open successfully!\n",fd);write(fd, &data, sizeof(struct Test));lseek(fd,0,SEEK_SET);read(fd, &data2, sizeof(struct Test));printf("data.i:%d,data.c=%c\n",data2.i,data2.c);close(fd); //close after writing return 0;
}

运行代码:

 

 

 写入一个结构体数组

demo7.c:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>struct Test
{int i;char c;
};int main()
{int fd; // file descriptionstruct Test data[2] = {{30,'k'},{100,'p'}};struct Test data2[2];fd = open("./file1",O_RDWR|O_CREAT|O_TRUNC, 0600);printf("file description = %d, open successfully!\n",fd);write(fd, &data, sizeof(struct Test)*2);lseek(fd,0,SEEK_SET);read(fd, &data2, sizeof(struct Test)*2);printf("data[0].i:%d,data[0].c=%c\n",data2[0].i,data2[0].c);printf("data[1].i:%d,data[1].c=%c\n",data2[1].i,data2[1].c);close(fd); //close after writing return 0;
}

 运行代码:

写入一个链表 

demo7.c:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>struct Test
{int data;struct Test *next;
};int main()
{int fd; // file descriptionstruct Test head = {20,NULL};struct Test node1 = {30,NULL};struct Test node2 = {40,NULL};head.next = &node1;node1.next = &node2;fd = open("./file1",O_RDWR|O_CREAT|O_TRUNC, 0600);printf("file description = %d, open successfully!\n",fd);write(fd, &head, sizeof(struct Test));write(fd, &node1, sizeof(struct Test));write(fd, &node2, sizeof(struct Test));lseek(fd,0,SEEK_SET);struct Test node_read_1 = {0,NULL};struct Test node_read_2 = {0,NULL};struct Test node_read_3 = {0,NULL};read(fd, &node_read_1, sizeof(struct Test)); read(fd, &node_read_2, sizeof(struct Test));read(fd, &node_read_3, sizeof(struct Test));printf("no.1=%d\n",node_read_1.data);printf("no.2=%d\n",node_read_2.data);printf("no.3=%d\n",node_read_3.data);close(fd);return 0;
}

运行代码:

 

但是以上的代码有点笨,且容错性太低,首先读取和写入应该封装成函数,并且我认为通常在读取链表的时候,不一定知道链表有多少元素,所以应该一边用尾插法动态创建链表一边读取

改进代码demo8.c:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>struct Test
{int data;struct Test *next;
};struct Test *insertBehind(struct Test *head, struct Test *new)
{struct Test *p = head;if(p == NULL){head = new;return head;}while(p->next != NULL){p = p->next;} //将p先移动到链表的尾部p->next = new;return head;
}void writeLink2File(int fd,struct Test *p){while(p != NULL){write(fd, p, sizeof(struct Test));p = p->next;}
}void readLinkFromFile(int fd,struct Test *head){struct Test *new;int size = lseek(fd,0,SEEK_END); //先计算文件大小lseek(fd,0,SEEK_SET); //然后不要忘记把光标移到文件头while(1){new = (struct Test *)malloc(sizeof(struct Test));read(fd, new, sizeof(struct Test));printf("link data:%d\n",new->data);if(lseek(fd,0,SEEK_CUR) == size){ //每次读取一个数据之后,动态创建下一个链表节点之前,都要判断“当前光标是否已经在文件尾”,如果是,说明读取已经完成printf("read finish\n");break;}head = insertBehind(head,new);}}int main()
{int fd; // file descriptionstruct Test head = {20,NULL};struct Test node1 = {30,NULL};struct Test node2 = {40,NULL};head.next = &node1;node1.next = &node2;fd = open("./file1",O_RDWR|O_CREAT|O_TRUNC, 0600);printf("file description = %d, open successfully!\n",fd);writeLink2File(fd,&head);struct Test *head_recv = NULL; //准备创建一个新的链表用于接收readLinkFromFile(fd,head_recv);close(fd);return 0;
}

改进代码运行:

相关文章:

将整数,结构体,结构体数组,链表写到文件

在之前的学习中&#xff0c;忘文件中写的内容都是字符串或字符&#xff0c;本节学习如何写入其他各种类型的数据。 回看write和read函数的形式&#xff1a; ssize_t write(int fd, const void *buf, size_t count); ssize_t read(int fd, void *buf, size_t count); 其中&a…...

UNIX基础知识:UNIX体系结构、登录、文件和目录、输入和输出、程序和进程、出错处理、用户标识、信号、时间值、系统调用和库函数

引言&#xff1a; 所有的操作系统都为运行在其上的程序提供服务&#xff0c;比如&#xff1a;执行新程序、打开文件、读写文件、分配存储区、获得系统当前时间等等 1. UNIX体系结构 从严格意义上来说&#xff0c;操作系统可被定义为一种软件&#xff0c;它控制计算机硬件资源&…...

IDEA2021.3.1-优化设置IDEA2021.3.1-优化设置、快捷方式改为eclipse、快捷键等

IDEA2021.3.1-优化设置IDEA2021.3.1-优化设置、快捷方式改为eclipse、快捷键等 一、主题设置二、鼠标悬浮提示三、显示方法分隔符四、代码提示忽略大小写五、自动导包六、取消单行显示tabs七、设置字体八、配置类文档注释信息模板九、设置文件编码9.1、所有地方设置为UTF-89.2、…...

使用C#的窗体显示与隐藏动画效果方案 - 开源研究系列文章

今天继续研究C#的WinForm的显示动画效果。 上次我们实现了无边框窗体的显示动画效果(见博文&#xff1a;基于C#的无边框窗体动画效果的完美解决方案 - 开源研究系列文章 )&#xff0c;这次介绍的是未在任务栏托盘中窗体的显示隐藏动画效果的实现代码。 1、 项目目录&#xff1b…...

09_Vue3中的 toRef 和 toRefs

toRdf 作用&#xff1a;创建一个 ref 对象&#xff0c;其 value 值指向另一个对象中的某个属性。语法&#xff1a; const name toRef(person,name) 应用&#xff1a;要将响应式对象中的某个属性单独提供给外部使用时。扩展&#xff1a;toRef 与 toRefs 功能一致&#xff0…...

JAVA获取视频音频时长 文件大小 MultipartFileUtil和file转换

java 获取视频时长_java获取视频时长_似夜晓星辰的博客-CSDN博客 <dependency><groupId>ws.schild</groupId><artifactId>jave-all-deps</artifactId><version>2.5.1</version></dependency>Slf4j public class VideoTimeUtil…...

刷题笔记 day9

1658 将 x 减到 0 的最小操作数 解析&#xff1a;1. 当数组的两端的数都大于x时&#xff0c;直接返回 -1。 2. 当数组所有数之和小于 x 时 &#xff0c;直接返回 -1。 3. 数组中可以将 x 消除为0&#xff0c;那么可以从左边减小为 0 &#xff1b;可以从右边减小为 0 &#xff1…...

小白解密ChatGPT大模型训练;Meta开源生成式AI工具AudioCraft

&#x1f989; AI新闻 &#x1f680; Meta开源生成式AI工具AudioCraft&#xff0c;帮助用户创作音乐和音频 摘要&#xff1a;美国公司Meta开源了一款名为AudioCraft的生成式AI工具&#xff0c;可以通过文本提示生成音乐和音频。该工具包含三个核心组件&#xff1a;MusicGen用…...

1 swagger简单案例

1.1 加入依赖 <!--swagger图形化接口--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version> </dependency><dependency><groupId>io.spri…...

Flutter写一个android底部导航栏框架

废话不多说&#xff0c;上代码&#xff1a; import package:flutter/material.dart;void main() {runApp(MyApp()); }class MyApp extends StatelessWidget {overrideWidget build(BuildContext context) {return MaterialApp(title: Bottom Navigation Bar,theme: ThemeData(…...

关于自动化测试用例失败重试的一些思考

自动化测试用例失败重跑有助于提高自动化用例的稳定性&#xff0c;那我们来看一下&#xff0c;python和java生态里都有哪些具体做法&#xff1f; 怎么做 如果是在python生态里&#xff0c;用pytest做测试驱动&#xff0c;那么可以通过pytest的插件pytest-rerunfailures来实现…...

JS逆向之顶像滑块

本教程仅限于学术探讨&#xff0c;也没有专门针对某个网站而编写&#xff0c;禁止用于非法用途、商业活动、恶意滥用技术等&#xff0c;否则后果自负。观看则同意此约定。如有侵权&#xff0c;请告知删除&#xff0c;谢谢&#xff01; 目录 一、接口请求流程 二、C1包 三、ac 四…...

【css】textarea-通过resize:none 禁止拖动设置大小

使用 resize 属性可防止调整 textareas 的大小&#xff08;禁用右下角的“抓取器”&#xff09;&#xff1a; 没有设置resize:none 代码&#xff1a; <!DOCTYPE html> <html> <head> <style> textarea {width: 100%;height: 150px;padding: 12px 20p…...

Linux内核学习小结

网上学习总结的一些资料&#xff0c;加上个人的一些总结。 Linux内核可以分成基础层和应用层。 基础层包括数据结构&#xff0c;内核同步机制&#xff0c;内存管理&#xff0c;任务调度。 应用层包括文件系统&#xff0c;设备和驱动&#xff0c;网络&#xff0c;虚拟化等。文件…...

八、ESP32控制8x8点阵屏

引脚的说明如下 上图中 C表示column 列的意思,所有的C接高电压,即控制esp32中输出1L表示line 行的意思,所有的L接低电压,即控制esp32中输出为01. 运行效果 2. 点阵屏引脚...

使用gitee创建远程maven仓库

1. 创建一个项目作为远程仓库 2. 打包项目发布到远程仓库 id随意&#xff0c;url是打包到哪个文件夹里面 在需要打包的项目的pom中添加 <distributionManagement><repository><id>handsomehuang-maven</id><url>file:D:/workspace/java/2023/re…...

基于C#的应用程序单例唯一运行的完美解决方案 - 开源研究系列文章

今次介绍一个应用程序单例唯一运行方案的代码。 我们知道&#xff0c;有些应用程序在操作系统中需要单例唯一运行&#xff0c;因为程序多开的话会对程序运行效果有影响&#xff0c;最基本的例子就是打印机&#xff0c;只能运行一个实例。这里将笔者单例运行的代码共享出来&…...

2023-08-07力扣今日二题

链接&#xff1a; 剑指 Offer 29. 顺时针打印矩阵 题意&#xff1a; 如题 解&#xff1a; 麻烦的简单题&#xff0c;具体操作类似走地图&#xff0c;使用一个长度四的数组表示移动方向 我这边的思路是如果按正常的方向没有路走了&#xff0c;那转向下一个方向一定有路&am…...

Spring接口ApplicationRunner的作用和使用介绍

在Spring框架中&#xff0c;ApplicationRunner接口是org.springframework.boot.ApplicationRunner接口的一部分。它是Spring Boot中用于在Spring应用程序启动完成后执行特定任务的接口。ApplicationRunner的作用是在Spring应用程序完全启动后&#xff0c;执行一些初始化任务或处…...

奶牛排队 java 思维题

&#x1f468;‍&#x1f3eb; 5133. 奶牛排队 题目描述 约翰的农场有 n n n 头奶牛&#xff0c;每一头奶牛都有一个正整数编号。 不同奶牛的编号不同。 现在&#xff0c;这 n n n 头牛按某种顺序排成一队&#xff0c;每头牛都拿出一张纸条写下了其前方相邻牛的编号以及其…...

51c自动驾驶~合集58

我自己的原文哦~ https://blog.51cto.com/whaosoft/13967107 #CCA-Attention 全局池化局部保留&#xff0c;CCA-Attention为LLM长文本建模带来突破性进展 琶洲实验室、华南理工大学联合推出关键上下文感知注意力机制&#xff08;CCA-Attention&#xff09;&#xff0c;…...

FastAPI 教程:从入门到实践

FastAPI 是一个现代、快速&#xff08;高性能&#xff09;的 Web 框架&#xff0c;用于构建 API&#xff0c;支持 Python 3.6。它基于标准 Python 类型提示&#xff0c;易于学习且功能强大。以下是一个完整的 FastAPI 入门教程&#xff0c;涵盖从环境搭建到创建并运行一个简单的…...

Python基于历史模拟方法实现投资组合风险管理的VaR与ES模型项目实战

说明&#xff1a;这是一个机器学习实战项目&#xff08;附带数据代码文档&#xff09;&#xff0c;如需数据代码文档可以直接到文章最后关注获取。 1.项目背景 在金融市场日益复杂和波动加剧的背景下&#xff0c;风险管理成为金融机构和个人投资者关注的核心议题之一。VaR&…...

排序算法总结(C++)

目录 一、稳定性二、排序算法选择、冒泡、插入排序归并排序随机快速排序堆排序基数排序计数排序 三、总结 一、稳定性 排序算法的稳定性是指&#xff1a;同样大小的样本 **&#xff08;同样大小的数据&#xff09;**在排序之后不会改变原始的相对次序。 稳定性对基础类型对象…...

LLMs 系列实操科普(1)

写在前面&#xff1a; 本期内容我们继续 Andrej Karpathy 的《How I use LLMs》讲座内容&#xff0c;原视频时长 ~130 分钟&#xff0c;以实操演示主流的一些 LLMs 的使用&#xff0c;由于涉及到实操&#xff0c;实际上并不适合以文字整理&#xff0c;但还是决定尽量整理一份笔…...

基于Springboot+Vue的办公管理系统

角色&#xff1a; 管理员、员工 技术&#xff1a; 后端: SpringBoot, Vue2, MySQL, Mybatis-Plus 前端: Vue2, Element-UI, Axios, Echarts, Vue-Router 核心功能&#xff1a; 该办公管理系统是一个综合性的企业内部管理平台&#xff0c;旨在提升企业运营效率和员工管理水…...

毫米波雷达基础理论(3D+4D)

3D、4D毫米波雷达基础知识及厂商选型 PreView : https://mp.weixin.qq.com/s/bQkju4r6med7I3TBGJI_bQ 1. FMCW毫米波雷达基础知识 主要参考博文&#xff1a; 一文入门汽车毫米波雷达基本原理 &#xff1a;https://mp.weixin.qq.com/s/_EN7A5lKcz2Eh8dLnjE19w 毫米波雷达基础…...

深入理解Optional:处理空指针异常

1. 使用Optional处理可能为空的集合 在Java开发中&#xff0c;集合判空是一个常见但容易出错的场景。传统方式虽然可行&#xff0c;但存在一些潜在问题&#xff1a; // 传统判空方式 if (!CollectionUtils.isEmpty(userInfoList)) {for (UserInfo userInfo : userInfoList) {…...

Kubernetes 网络模型深度解析:Pod IP 与 Service 的负载均衡机制,Service到底是什么?

Pod IP 的本质与特性 Pod IP 的定位 纯端点地址&#xff1a;Pod IP 是分配给 Pod 网络命名空间的真实 IP 地址&#xff08;如 10.244.1.2&#xff09;无特殊名称&#xff1a;在 Kubernetes 中&#xff0c;它通常被称为 “Pod IP” 或 “容器 IP”生命周期&#xff1a;与 Pod …...

水泥厂自动化升级利器:Devicenet转Modbus rtu协议转换网关

在水泥厂的生产流程中&#xff0c;工业自动化网关起着至关重要的作用&#xff0c;尤其是JH-DVN-RTU疆鸿智能Devicenet转Modbus rtu协议转换网关&#xff0c;为水泥厂实现高效生产与精准控制提供了有力支持。 水泥厂设备众多&#xff0c;其中不少设备采用Devicenet协议。Devicen…...