博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C/C++使用VOID指针保存结构体数据到二进制文件并且读取
阅读量:7105 次
发布时间:2019-06-28

本文共 3156 字,大约阅读时间需要 10 分钟。

只是演示,细节没有过多注意
程序如下:
主程序:

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

  > 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<<"a.out name id sorce grade *fd"<<ENDL;
        S_STR s_t;

        if(argc < 6)

        {
                cout<<"a.out name id sorce grade *fd"<<ENDL;
                exit(1);
        }

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

        {
                cout<<"name is load OK!"<<ENDL;
        }

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

        {
                cout<<"id is load OK!"<<ENDL;
        }

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

        {
                cout<<"sorce is load OK!"<<ENDL;
        }

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

        {
                cout<<"grade is load OK!"<<ENDL;
        }

    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<<"copy ok!"<<ENDL;
        }
        if((fd = fopen(file,"w")) == NULL)
        {
                cout<<"open file is error"<<ENDL;
                exit(10);
        }
   
        cout<<"you data will load in file "<<FILE<<ENDL;
    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<<"read open file is error"<<ENDL;
                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 <<ENDL;
        cout << my_st->id <<ENDL;
        cout << my_st->sorce <<ENDL;
        cout << my_st->grade <<ENDL;

        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

转载地址:http://mvjhl.baihongyu.com/

你可能感兴趣的文章
Linux服务器中木马(肉鸡)手工清除方法(转)
查看>>
用易语言编写的fireman微型浏览器(个人专属)
查看>>
ELK(elasticsearch+logstash+kibana)+redis实现nginx 日志的分析
查看>>
书店管理系统之分页
查看>>
面试题:接口和抽象类的区别
查看>>
Windows Server 2012显示桌面图标
查看>>
Linux下Socket编程
查看>>
字符型图片验证码识别完整过程及Python实现
查看>>
puppet的hash合并函数
查看>>
OGG运维优化脚本(十八)-进程操作类--全进程启停
查看>>
vsftp 配置文件说明
查看>>
科技日报 永中软件获“CICE金慧奖”优秀名企奖
查看>>
Python学习笔记-模块介绍(二)-模块导入和执行
查看>>
Cocos数据篇[3.4](6) ——SQLite3数据库基础用法
查看>>
APP刷量黑色收入年过百万:开发者急功近利
查看>>
zabbix使用自动发现功能监控服务器各JVM进程状态
查看>>
我的友情链接
查看>>
How To Do Math Using PowerShell, Part 1 and Part 2
查看>>
c++中模板函数和非模板函数的重载
查看>>
ospf基本配置
查看>>