本文共 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 #includeusing 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 #includeusing 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; #endifint 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 #includeusing 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; #endifint 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/