当前位置: 首页 > 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;每头牛都拿出一张纸条写下了其前方相邻牛的编号以及其…...

未来机器人的大脑:如何用神经网络模拟器实现更智能的决策?

编辑&#xff1a;陈萍萍的公主一点人工一点智能 未来机器人的大脑&#xff1a;如何用神经网络模拟器实现更智能的决策&#xff1f;RWM通过双自回归机制有效解决了复合误差、部分可观测性和随机动力学等关键挑战&#xff0c;在不依赖领域特定归纳偏见的条件下实现了卓越的预测准…...

centos 7 部署awstats 网站访问检测

一、基础环境准备&#xff08;两种安装方式都要做&#xff09; bash # 安装必要依赖 yum install -y httpd perl mod_perl perl-Time-HiRes perl-DateTime systemctl enable httpd # 设置 Apache 开机自启 systemctl start httpd # 启动 Apache二、安装 AWStats&#xff0…...

Qt Widget类解析与代码注释

#include "widget.h" #include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) {ui->setupUi(this); }Widget::~Widget() {delete ui; }//解释这串代码&#xff0c;写上注释 当然可以&#xff01;这段代码是 Qt …...

最新SpringBoot+SpringCloud+Nacos微服务框架分享

文章目录 前言一、服务规划二、架构核心1.cloud的pom2.gateway的异常handler3.gateway的filter4、admin的pom5、admin的登录核心 三、code-helper分享总结 前言 最近有个活蛮赶的&#xff0c;根据Excel列的需求预估的工时直接打骨折&#xff0c;不要问我为什么&#xff0c;主要…...

基于当前项目通过npm包形式暴露公共组件

1.package.sjon文件配置 其中xh-flowable就是暴露出去的npm包名 2.创建tpyes文件夹&#xff0c;并新增内容 3.创建package文件夹...

在四层代理中还原真实客户端ngx_stream_realip_module

一、模块原理与价值 PROXY Protocol 回溯 第三方负载均衡&#xff08;如 HAProxy、AWS NLB、阿里 SLB&#xff09;发起上游连接时&#xff0c;将真实客户端 IP/Port 写入 PROXY Protocol v1/v2 头。Stream 层接收到头部后&#xff0c;ngx_stream_realip_module 从中提取原始信息…...

Angular微前端架构:Module Federation + ngx-build-plus (Webpack)

以下是一个完整的 Angular 微前端示例&#xff0c;其中使用的是 Module Federation 和 npx-build-plus 实现了主应用&#xff08;Shell&#xff09;与子应用&#xff08;Remote&#xff09;的集成。 &#x1f6e0;️ 项目结构 angular-mf/ ├── shell-app/ # 主应用&…...

回溯算法学习

一、电话号码的字母组合 import java.util.ArrayList; import java.util.List;import javax.management.loading.PrivateClassLoader;public class letterCombinations {private static final String[] KEYPAD {"", //0"", //1"abc", //2"…...

LINUX 69 FTP 客服管理系统 man 5 /etc/vsftpd/vsftpd.conf

FTP 客服管理系统 实现kefu123登录&#xff0c;不允许匿名访问&#xff0c;kefu只能访问/data/kefu目录&#xff0c;不能查看其他目录 创建账号密码 useradd kefu echo 123|passwd -stdin kefu [rootcode caozx26420]# echo 123|passwd --stdin kefu 更改用户 kefu 的密码…...

C++_哈希表

本篇文章是对C学习的哈希表部分的学习分享 相信一定会对你有所帮助~ 那咱们废话不多说&#xff0c;直接开始吧&#xff01; 一、基础概念 1. 哈希核心思想&#xff1a; 哈希函数的作用&#xff1a;通过此函数建立一个Key与存储位置之间的映射关系。理想目标&#xff1a;实现…...