C++读写文件代码自动生成器(python3完成)
最近写c++读写文件的代码写的非常痛苦,于是想出来用python写个自动的,这也是这篇文章出来的原因,写程序到了一定时候,正好也在学习python这么语言,于是就选择它来完成这个工作了。目前只完成了二进制的版本。
下面我就简单说明下,这个生成器是如何运作的,首先我们知道在c++中有些关键词,我们只要将这些词列为关键词,这些词,大致分为两类,一类是直接可以写入写出的,另一类是不可以的,我们把它们分成两个列表存储。
然后设计自己的定义生成文件的格式,我暂时定义为下面的情况:

第一行标识是文件名字,第二行是模式,为了以后加入文本文件做准备,然后就是一些结构体,我们来生成读写这些结构体的代码:
#include<string>using namespace std;#include"fileName.h"intWrite_sss(FILE*fp,constsss&inputStruct){fwrite(&inputStruct.a,sizeof(int),15,fp);fwrite(&inputStruct.b,sizeof(float),13,fp);fwrite(&inputStruct.c,sizeof(char),1,fp);return 0;}intWrite_ff(FILE*fp,constff&inputStruct){int stringNumbers = 10;fwrite(&stringNumbers,sizeof(int),1,fp)for(int i=0;i<10,i++){int theCurrentStringlength_v = inputStruct.d[i].length();fwrite(&theCurrentStringlength_v,sizeof(int),1,fp);fwrite(&inputStruct.d[i].c_str(),sizeof(char),theCurrentStringlength_v,fp);}int theCurrentStringlength = inputStruct.f.length();fwrite(&theCurrentStringlength,sizeof(int),1,fp);fwrite(&inputStruct.f.c_str(),sizeof(char),theCurrentStringlength,fp);return 0;}intWrite_wff(FILE*fp,constwff&inputStruct){int wstringNumbers = 10;fwrite(&wstringNumbers,sizeof(int),1,fp);for(int i=0;i<10,i++){int theCurrentWStringlength_v = inputStruct.d[i].length();fwrite(&theCurrentWStringlength_v,sizeof(int),1,fp);fwrite(&d[i].c_str(),sizeof(wchar_t),theCurrentWStringlength_v,fp);}int theCurrentWStringlength = inputStruct.f.length();fwrite(&theCurrentWStringlength,sizeof(int),1,fp);fwrite(&inputStruct.f.c_str(),sizeof(wchar_t),theCurrentWStringlength,fp);return 0;}intWrite_booltest(FILE*fp,constbooltest&inputStruct){int boolNumbers = 10;fwrite(&boolNumbers,sizeof(int),1,fp);for(int i=0;i<10,i++){char boolValue = inputStruct.ffss[i]?1:-1;fwrite(&boolValue,sizeof(char),1,fp);}char boolValue = inputStruct.ssfd[i]?1:-1;fwrite(&boolValue,sizeof(char),1,fp);return 0;}intWrite_dd(FILE*fp,constdd&inputStruct){Write_sss(fp,inputStruct.aa);Write_ff(fp,inputStruct.bb);return 0;}intRead_sss(FILE*fp,sss&inputStruct){fread(&inputStruct.a,sizeof(int),15,fp);fread(&inputStruct.b,sizeof(float),13,fp);fread(&inputStruct.c,sizeof(char),1,fp)return 0;}intRead_ff(FILE*fp,ff&inputStruct){int stringNumbers;fread(&stringNumbers,sizeof(int),1,fp);for(int i=0;i<stringNumbers,i++){int theCurrentStringlength_v;fread(&theCurrentStringlength_v,sizeof(int),1,fp);char* tmpCharBuffer_v = new char[theCurrentStringlength_v+1];memset(tmpCharBuffer_v, 0, theCurrentStringlength_v);fread(tmpCharBuffer_vsizeof(char),theCurrentStringlength_v,fp);inputStruct.d[i]=tmpCharBuffer_v;delete []tmpCharBuffer_v;}int theCurrentStringlength = inputStruct.f.length();fread(&theCurrentStringlength,sizeof(int),1,fp);char* tmpCharBuffer = new char[theCurrentStringlength+1];memset(tmpCharBuffer, 0, theCurrentStringlength);fread(tmpCharBuffersizeof(char),theCurrentStringlength,fp);inputStruct.f=tmpCharBuffer;delete []tmpCharBuffer;return 0;}intRead_wff(FILE*fp,wff&inputStruct){int stringNumbers;fread(&stringNumbers,sizeof(int),1,fp);for(int i=0;i<stringNumbers,i++){int theCurrentWStringlength_v;fread(&theCurrentWStringlength_v,sizeof(int),1,fp);wchar_t* tmpwCharBuffer_v = new wchar_t[theCurrentWStringlength_v+1];memset(tmpwCharBuffer_v, 0, theCurrentWStringlength_v*sizeof(wchar_t));fread(tmpwCharBuffer_vsizeof(wchar_t),theCurrentWStringlength_v,fp);inputStruct.d[i]=tmpwCharBuffer_v;delete []tmpwCharBuffer_v;}int theCurrentStringlength = inputStruct.f.length();fread(&theCurrentStringlength,sizeof(int),1,fp);wchar_t* tmpwCharBuffer = new wchar_t[theCurrentStringlength+1];memset(tmpwCharBuffer, 0, theCurrentStringlength*sizeof(wchar_t));fread(tmpwCharBuffersizeof(wchar_t),theCurrentStringlength,fp);inputStruct.f=tmpwCharBuffer;delete []tmpwCharBuffer;return 0;}intRead_booltest(FILE*fp,booltest&inputStruct){int boolNumbers;fread(&boolNumbers,sizeof(int),1,fp);for(int i=0;i<boolNumbers,i++){char boolValue_v;fread(&boolValue_v,sizeof(char),1,fp);inputStruct.ffss[i]=((1==boolValue_v)?true:false);}char boolValue;fread(&boolValue,sizeof(char),1,fp);inputStruct.ssfd=((1==boolValue)?true:false);return 0;}intRead_dd(FILE*fp,dd&inputStruct){Read_sss(fp,inputStruct.aa);Read_ff(fp,inputStruct.bb);return 0;}int WriteFinal(string strPath,YourParam param){FILE *fp;fp= fopen(filename.c_str(),"wb");if(NULL==fp){return -1;}//write Your Code Here!fclose(fp);return 0;}int ReadFinal(string strPath,YourParam param){FILE *fp;fp= fopen(filename.c_str(),"rb");if(NULL==fp){return -1;}//write Your Code Here!fclose(fp);return 0;}