C/C++使用VOID指標儲存結構體資料到二進位制檔案並且讀取

gaopengtttt發表於2016-06-03
只是演示,細節沒有過多注意
程式如下:
主程式:

/*************************************************************************
  > 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;< p>

        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

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7728585/viewspace-2113717/,如需轉載,請註明出處,否則將追究法律責任。

相關文章