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?的更多相关文章

  1. 简明python教程 --C++程序员的视角(八):标准库

    os模块 这个模块包含普遍的操作系统功能. 如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的.一个例子就是使用os.sep可以取代操作系统特定的路径分割符. os.system() 执行li ...

  2. Non Lasting Storage File System、procfs、sysfs

    catalog . 引言 . proc文件系统 . 简单的文件系统 . sysfs 0. 引言 传统上,文件系统用于在块设备上持久存储数据,但也可以使用文件系统来组织.提供.交换并不存储在块设备上的信 ...

  3. file.go

        //    return int64(f.offset), errors.New("offset > file.size")     //}else {     // ...

  4. 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 ...

  5. 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 ...

  6. 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 [ ...

  7. 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 ...

  8. iOS开发系列--Swift进阶

    概述 上一篇文章<iOS开发系列--Swift语言>中对Swift的语法特点以及它和C.ObjC等其他语言的用法区别进行了介绍.当然,这只是Swift的入门基础,但是仅仅了解这些对于使用S ...

  9. Usual tiny skills & solutions

    Ubuntu and Win10 - double OS 2016-02-21 Yesterday I helped my friend install Ubuntu (14.04 LTS) on h ...

随机推荐

  1. Jedis操作redis(转)

    package org.jzkangta.jedis; import java.util.ArrayList; import java.util.HashMap; import java.util.I ...

  2. Linux性能监测:CPU篇(转)

    http://os.51cto.com/art/201012/239880.htm CPU 的占用主要取决于什么样的资源正在 CPU 上面运行,比如拷贝一个文件通常占用较少 CPU,因为大部分工作是由 ...

  3. 关于闭包与for循环的理解

    function createFunction1(){ for(var i=0;i<5;i++){ function s(){ console.log(i); } s(); } } create ...

  4. CSS3秘笈复习:十三章&十四章&十五章&十六章&十七章

    第十三章 1.在使用浮动时,源代码的顺序非常重要.浮动元素的HTML必须处在要包围它的元素的HTML之前. 2.清楚浮动: (1).在外围div的底部添加一个清除元素:clear属性可以防止元素包围浮 ...

  5. 【Sort】希尔排序

    希尔排序(ShellSort),缩小增量排序,使用希尔增量时最坏运行时间O(n^2),不同的增量会对运行时间产生显著影响. void shellsort(int *nums,int n) { int ...

  6. jquery makearray()使用

    makearray(),转换一个类似数组的对象成为真正的JavaScript数组.首先看看jquery中array的定义 makeArray: function( arr, results ) { v ...

  7. Android 中执行定时任务 Timer + TimerTask

    1. new Timer().schedule(new TimerTask() { @Override public void run() { //任务代码 } }, 0, 5000);

  8. soj 2543 完全二叉树

    1000. 完全二叉树     Total: 338 Accepted: 81                 Time Limit: 1sec    Memory Limit:256MB Descr ...

  9. Cross Product

    Cross Product These are two vectors: They can be multiplied using the "Cross Product" (als ...

  10. shell 之awk 关联数组高级应用

    最近由于数据迁移过,有些用户信息需要再次确认下,也许数据量比较大,但是需要最终确认的比如说是用户ID和其对应的用户积分数,这样就会导致出现文本a(老的数据),文本b(新的数据).比如 这是文本a.tx ...