how to write a struct to a file directly?
Using write and read system call. Following is an example:
blk.h:
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h> struct data{
char s[];
int d1;
char c;
}; #define BLK_SIZE 25
writeb.c:
#include "blk.h" int main(){
struct data *d= malloc(BLK_SIZE);
d->d1= ;
d->c= 'a';
strcpy(d->s, "first try.."); int fd= open("blk.dat", O_RDWR | O_CREAT | O_EXCL, S_IREAD | S_IWRITE);
write(fd, d, BLK_SIZE);
printf("writed!\n");
close(fd);
return ;
}
readb.c:
include "blk.h" int main(){
int fd;
struct data *d;
fd= open("blk.dat", O_RDWR);
read(fd, d, BLK_SIZE);
printf("%d, %c, %s\n", d->d1, d->c, d->s);
close(fd);
return ;
}
how to write a struct to a file directly?的更多相关文章
- 简明python教程 --C++程序员的视角(八):标准库
os模块 这个模块包含普遍的操作系统功能. 如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的.一个例子就是使用os.sep可以取代操作系统特定的路径分割符. os.system() 执行li ...
- Non Lasting Storage File System、procfs、sysfs
catalog . 引言 . proc文件系统 . 简单的文件系统 . sysfs 0. 引言 传统上,文件系统用于在块设备上持久存储数据,但也可以使用文件系统来组织.提供.交换并不存储在块设备上的信 ...
- file.go
// return int64(f.offset), errors.New("offset > file.size") //}else { // ...
- Downloading files from a server to client, using ASP.Net, when file size is too big for MemoryStream using Generic Handlers (ashx)
Currently, I was trying to write an ASP.Net application that involved a user clicking a ASP.Net butt ...
- Even uploading a JPG file can lead to Cross-Site Content Hijacking (client-side attack)!
Introduction: This post is going to introduce a new technique that has not been covered previously i ...
- HDFS relaxes a few POSIX requirements to enable streaming access to file system data
https://hadoop.apache.org/docs/r2.7.2/hadoop-project-dist/hadoop-hdfs/HdfsDesign.html Introduction [ ...
- how browser handler file:/// link
1. why browser can only open .txt file directly, pop up open or save dialog for others? 2. html cann ...
- iOS开发系列--Swift进阶
概述 上一篇文章<iOS开发系列--Swift语言>中对Swift的语法特点以及它和C.ObjC等其他语言的用法区别进行了介绍.当然,这只是Swift的入门基础,但是仅仅了解这些对于使用S ...
- Usual tiny skills & solutions
Ubuntu and Win10 - double OS 2016-02-21 Yesterday I helped my friend install Ubuntu (14.04 LTS) on h ...
随机推荐
- tablespace
CREATE [UNDO] TABLESPACE tablespace_name [DATAFILE datefile_spec1 [,datefile_spec2] ...... [{MININUM ...
- 谷歌Dremel即时数据分析解决方案
Hadoop技术已经无处不在.不管是好是坏,Hadoop已经成为大数据的代名词.短短几年间,Hadoop从一种边缘技术成为事实上的标准.看来,不仅现在Hadoop是企业大数据的标准,而且在未来,它的地 ...
- win32api大全
win32api大全 http://files.cnblogs.com/files/landv/Win32API%E5%A4%A7%E5%85%A8.zip
- 第八十二节,CSS3过渡效果
CSS3过渡效果 学习要点: 1.过渡简介 2.transition-property 3.transition-duration 4.transition-timing-function 5.tra ...
- CSS3秘笈复习:第八章
一.背景的所有属性: 属性 作用 可选项 1.background-image 定义一张图片 url(...) 2.background-repeat 控制重复 no-repeat | repeat- ...
- display: inline-block兼容性写法
display:inline-block;*display:inline;*zoom:1;
- C# 读书笔记之类与结构体
类和结构体都包括数据和操作数据的方法 类的定义形式 class PhoneCustomer{public const string DayOfSendingBill = "Monday&qu ...
- Java IO 转换流 字节转字符流
Java IO 转换流 字节转字符流 @author ixenos 字节流 输入字节流:---------| InputStream 所有输入字节流的基类. 抽象类.------------| Fil ...
- MongoDB文档基本操作
一.插入文档 使用insert()或save()方法向集合插入文档 >db.COLLECTION_NAME.insert(document) 详细用法可以参考MongoDB菜鸟教程 二.查找文档 ...
- 如何使用自定义消息?--ESFramework 4.0 快速上手(04)
在ESFramework 4.0 快速上手一文中,我们讲述了如何使用Rapid引擎可以快速地上手ESFramework开发,文中介绍了使用ESPlus.Application.CustomizeInf ...