且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-06-23 02:55:51

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

/*************************************************************************
  > File Name: change.cpp
  > Author: gaopeng
  > Mail: gaopp_200217@163.com
  > 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: gaopp_200217@163.com
  > 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: gaopp_200217@163.com
  > 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