c语言结构体转二进制文件,C/C++使用VOID指针保存结构体数据到二进制文件并且读取...

这是一个C++程序示例,演示如何将包含姓名、ID、源和等级的结构体数据写入文件,并从文件读取。程序通过`f_prt`函数写入数据,通过`f_read`函数读取数据,成功执行后会在当前目录创建名为`tdata`的文件,存储输入的数据。
摘要由CSDN通过智能技术生成

只是演示,细节没有过多注意

程序如下:

主程序:

/*************************************************************************

> File Name: change.cpp

> Author: gaopeng

> Mail:

> Created Time: Sun 29 May 2016 05:11:34 PM CST

************************************************************************/

#ifndef PAR

#define PAR

#include

#include

#include

#include

using namespace std;

typedef unsigned int UINT;

typedef struct stuna

{

char name[20];

UINT  id;

UINT  sorce;

char grade[20];

} S_STR;

#endif

#include"fprt.h"

#include"fred.h"

int main(int argc,char *argv[])

{

cout<

S_STR s_t;

if(argc < 6)

{

cout<

exit(1);

}

if(strcpy(s_t.name,argv[1]) == s_t.name)

{

cout<

}

if(sscanf(argv[2],"%d",&(s_t.id)) == 1)

{

cout<

}

if(sscanf(argv[3],"%d",&(s_t.sorce)) == 1)

{

cout<

}

if(strcpy(s_t.grade,argv[4]) == s_t.grade)

{

cout<

}

f_prt(&s_t,sizeof(S_STR),argv[5]);

f_read(argv[5],sizeof(S_STR));

return 0;

}

两个函数

#include"fprt.h" 这个函数用来写入结构体数据到文件

/*************************************************************************

> File Name: fprt.h

> Author: gaopeng

> Mail:

> Created Time: Sun 29 May 2016 07:16:28 PM CST

************************************************************************/

#ifndef PAR

#define PAR

#include

#include

#include

#include

using namespace std;

typedef unsigned int UINT;

typedef struct stuna

{

char name[20];

UINT  id;

UINT  sorce;

char grade[20];

} S_STR;

#endif

#ifndef VOID

#define VOID

typedef void* VP;

#endif

int f_prt(const S_STR* s_in,UINT sz,const char *file  )

{

FILE *fd;

VP p = (void *)malloc(sz);

if(memcpy(p ,(void *)(s_in),sz) == p)

{

cout<

}

if((fd = fopen(file,"w")) == NULL)

{

cout<

exit(10);

}

cout<

fwrite(p,1,sz,fd);

fclose(fd);

free(p);

return 0;

}

#include"fred.h" 这个函数用来读取

/*************************************************************************

> File Name: fprt.h

> Author: gaopeng

> Mail:

> Created Time: Sun 29 May 2016 07:16:28 PM CST

************************************************************************/

#ifndef PAR

#define PAR

#include

#include

#include

#include

using namespace std;

typedef unsigned int UINT;

typedef struct stuna

{

char name[20];

UINT  id;

UINT  sorce;

char grade[20];

} S_STR;

#endif

#ifndef VOID

#define VOID

typedef void* VP;

#endif

int f_read(const char *file,UINT sz  )

{

FILE *fd;

VP p = (void *)malloc(sz);

if((fd = fopen(file,"r")) == NULL)

{

cout<

exit(11);

}

fread(p,1,sz,fd);

S_STR *my_st = (S_STR *)p;

cout << "read data from file " << file << endl;

cout << my_st->name <

cout << my_st->id <

cout << my_st->sorce <

cout << my_st->grade <

fclose(fd);

free(p);

return 0;

}

比如你想把小明的ID和分数以及评级存储到文件tdata中

./a.out xiaoming 100 100 good tdata

程序执行如下:

a.out name id sorce grade *fd

name is load OK!

id is load OK!

sorce is load OK!

grade is load OK!

copy ok!

you data will load in file tdata

read data from file tdata

xiaoming

100

100

good

这样就在执行目录下生产了tdata文件,我们可以用cdump -Cv 查看tdata

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值