最近写c++读写文件的代码写的非常痛苦,于是想出来用python写个自动的,这也是这篇文章出来的原因,写程序到了一定时候,正好也在学习python这么语言,于是就选择它来完成这个工作了。目前只完成了二进制的版本。
下面我就简单说明下,这个生成器是如何运作的,首先我们知道在c++中有些关键词,我们只要将这些词列为关键词,这些词,大致分为两类,一类是直接可以写入写出的,另一类是不可以的,我们把它们分成两个列表存储。
然后设计自己的定义生成文件的格式,我暂时定义为下面的情况:
第一行标识是文件名字,第二行是模式,为了以后加入文本文件做准备,然后就是一些结构体,我们来生成读写这些结构体的代码:
import sys # Load the sys module (导入sys模块) SimpleKeyWordInCplusplus = ['int', 'float', 'char', 'unsigned char', 'long', 'unsigned int', 'unsigned long', 'unsigned float', 'wchar_t', 'double', 'unsigned double'] SpecialKeyWordInCplusplus = ['string', 'wstring', 'bool'] def OutputStructToList(ss,FileNameAndFileMode, TheStructS, FunctionName): lineNumbers = len(ss) for i in range(0, len(ss)): lines = ss[i] if(-1!=lines.find("fileName",0,len(lines))): FileNameSplit= lines.split() FileNameAndFileMode.append(FileNameSplit[1]) if(-1!=lines.find("functionName",0,len(lines))): tmpFunctionName = lines.split() FunctionName.append(tmpFunctionName[1]) if(-1!=lines.find("modle",0,len(lines))): if(-1==lines.find(";",0,len(lines))): FileMode = lines.split() FileNameAndFileMode.append(FileMode[1]) FileNameAndFileMode.append(FileMode[2]) if(-1!=lines.find("struct",0,len(lines))): if((i+1)<lineNumbers): nextLines = ss[i+1] if(-1!=nextLines.find("{", 0, len(nextLines))): tmp = [] StructName = lines.split()[1]; TheStructS.append(StructName) for j in range(i+2,lineNumbers): theStructLine = ss[j] if(-1!=theStructLine.find('}',0,len(theStructLine))): break else: theStructLine=theStructLine.replace(';', ' ') theStructLine=theStructLine.replace('[', ' ') theStructLine=theStructLine.replace(']', ' ') StructItem = theStructLine.split(); tmp.append(StructItem) TheStructS.append(tmp) def OutWriteFileFunction(FileNameAndFileMode, TheStructS, FunctionName,fp2): structNumber = len(TheStructS)//2 for k in range(0, structNumber): fp2.write('\n') fp2.write('int\t'+'Write_'+TheStructS[2*k]+'('+'FILE'+'\t*fp,'+'const\t'+TheStructS[2*k]) fp2.write('\t&in'+TheStructS[2*k]) fp2.write(')\n') fp2.write('{\n') structItemsIndex = 2*k+1; CurrentStructItemNumber = len(TheStructS[structItemsIndex]) for i in range(0, CurrentStructItemNumber): if(TheStructS[structItemsIndex][i][0] in SimpleKeyWordInCplusplus): fp2.write('\tfwrite(&in'+TheStructS[2*k]+'.') fp2.write(TheStructS[structItemsIndex][i][1]+',') fp2.write('sizeof('+TheStructS[structItemsIndex][i][0]+')\t,') if(3==len(TheStructS[structItemsIndex][i])): fp2.write(TheStructS[structItemsIndex][i][2]+'\t,fp);\n') else: fp2.write('1'+'\t,fp);\n') elif(SpecialKeyWordInCplusplus[0]==TheStructS[structItemsIndex][i][0]): if(3==len(TheStructS[structItemsIndex][i])): stringNumbers = TheStructS[structItemsIndex][i][2]; fp2.write('\tint stringNumbers = '+stringNumbers+';\n') fp2.write('\tfwrite(&stringNumbers,\tsizeof(int)\t,1\t,fp)\n') fp2.write('\tfor(int i=0;i<'+stringNumbers+',i++)\n') fp2.write('\t{\n') fp2.write('\t\t') fp2.write('int theCurrentStringlength_v = in'+TheStructS[2*k]+'.') fp2.write(TheStructS[structItemsIndex][i][1]+'[i].length();\n') fp2.write('\t\tfwrite(&theCurrentStringlength_v,\tsizeof(int)\t,1\t,fp);\n') fp2.write('\t\tfwrite(&in'+TheStructS[2*k]+'.') fp2.write(TheStructS[structItemsIndex][i][1]+'[i].c_str(),') fp2.write('\tsizeof(char)\t,theCurrentStringlength_v\t,fp);\n') fp2.write('\t}\n') else: fp2.write('\tint theCurrentStringlength = in'+TheStructS[2*k]+'.') fp2.write(TheStructS[structItemsIndex][i][1]+'.length();\n') fp2.write('\tfwrite(&theCurrentStringlength,\tsizeof(int)\t,1\t,fp);\n') fp2.write('\tfwrite(&in'+TheStructS[2*k]+'.') fp2.write(TheStructS[structItemsIndex][i][1]+'.c_str(),') fp2.write('\tsizeof(char)\t,theCurrentStringlength\t,fp);\n') elif(SpecialKeyWordInCplusplus[1]==TheStructS[structItemsIndex][i][0]): if(3==len(TheStructS[structItemsIndex][i])): wstringNumbers = TheStructS[structItemsIndex][i][2]; fp2.write('\tint wstringNumbers = '+wstringNumbers+';\n') fp2.write('\tfwrite(&wstringNumbers,\tsizeof(int)\t,1\t,fp);\n') fp2.write('\tfor(int i=0;i<'+wstringNumbers+',i++)\n') fp2.write('\t{\n') fp2.write('\t\t') fp2.write('int theCurrentWStringlength_v = in'+TheStructS[2*k]+'.') fp2.write(TheStructS[structItemsIndex][i][1]+'[i].length();\n') fp2.write('\t\tfwrite(&theCurrentWStringlength_v,\tsizeof(int)\t,1\t,fp);\n') fp2.write('\t\tfwrite(&'+TheStructS[structItemsIndex][i][1]+'[i].c_str(),') fp2.write('\tsizeof(wchar_t)\t,theCurrentWStringlength_v\t,fp);\n') fp2.write('\t}\n') else: fp2.write('\tint theCurrentWStringlength = in'+TheStructS[2*k]+'.') fp2.write(TheStructS[structItemsIndex][i][1]+'.length();\n') fp2.write('\tfwrite(&theCurrentWStringlength,\tsizeof(int)\t,1\t,fp);\n') fp2.write('\tfwrite(&in'+TheStructS[2*k]+'.') fp2.write(TheStructS[structItemsIndex][i][1]+'.c_str(),') fp2.write('\tsizeof(wchar_t)\t,theCurrentWStringlength\t,fp);\n') elif(SpecialKeyWordInCplusplus[2]==TheStructS[structItemsIndex][i][0]): if(3==len(TheStructS[structItemsIndex][i])): boolNumbers = TheStructS[structItemsIndex][i][2]; fp2.write('\tint boolNumbers = '+boolNumbers+';\n') fp2.write('\tfwrite(&boolNumbers,\tsizeof(int)\t,1\t,fp);\n') fp2.write('\tfor(int i=0;i<'+boolNumbers+',i++)\n') fp2.write('\t{\n') fp2.write('\t\t') fp2.write('char boolValue = in'+TheStructS[2*k]+'.') fp2.write(TheStructS[structItemsIndex][i][1]+'[i]?1:-1;\n') fp2.write('\t\tfwrite(&boolValue,\tsizeof(char)\t,1\t,fp);\n') fp2.write('\t}\n') else: fp2.write('\tchar boolValue = in'+TheStructS[2*k]+'.') fp2.write(TheStructS[structItemsIndex][i][1]+'[i]?1:-1;\n') fp2.write('\tfwrite(&boolValue,\tsizeof(char)\t,1\t,fp);\n') else: fp2.write('\tWrite_'+TheStructS[structItemsIndex][i][0]) fp2.write('(fp,in'+TheStructS[2*k]+'.') fp2.write(TheStructS[structItemsIndex][i][1]+');\n') fp2.write('\n') fp2.write('\treturn 0;\n') fp2.write('}\n') def OutReadFileFunction(FileNameAndFileMode, TheStructS, FunctionName, fp2): structNumber = len(TheStructS)//2 for k in range(0, structNumber): fp2.write('\n') fp2.write('int\t'+'Read_'+TheStructS[2*k]+'('+'FILE'+'\t*fp,'+'\t'+TheStructS[2*k]) fp2.write('\t&out'+TheStructS[2*k]+'') fp2.write(')\n') fp2.write('{\n') structItemsIndex = 2*k+1; CurrentStructItemNumber = len(TheStructS[structItemsIndex]) for i in range(0, CurrentStructItemNumber): if(TheStructS[structItemsIndex][i][0] in SimpleKeyWordInCplusplus): fp2.write('\tfread(&'+'out'+TheStructS[2*k]+'.') fp2.write(TheStructS[structItemsIndex][i][1]+',') fp2.write('sizeof('+TheStructS[structItemsIndex][i][0]+')\t,') if(3==len(TheStructS[structItemsIndex][i])): fp2.write(TheStructS[structItemsIndex][i][2]+'\t,fp);\n') else: fp2.write('1'+'\t,fp)\n') elif(SpecialKeyWordInCplusplus[0]==TheStructS[structItemsIndex][i][0]): if(3==len(TheStructS[structItemsIndex][i])): fp2.write('\tint stringNumbers;\n') fp2.write('\tfread(&stringNumbers,\tsizeof(int)\t,1\t,fp);\n') fp2.write('\tfor(int i=0;i<stringNumbers,i++)\n') fp2.write('\t{\n') fp2.write('\t\t') fp2.write('int theCurrentStringlength_v;\n') fp2.write('\t\tfread(&theCurrentStringlength_v,\tsizeof(int)\t,1\t,fp);\n') fp2.write('\t\tchar* tmpCharBuffer_v = new char[theCurrentStringlength_v+1];\n') fp2.write('\t\tmemset(tmpCharBuffer_v, 0, theCurrentStringlength_v);\n') fp2.write('\t\tfread(tmpCharBuffer_v') fp2.write('\tsizeof(char)\t,theCurrentStringlength_v\t,fp);\n') fp2.write('\t\tout'+TheStructS[2*k]+'.'+TheStructS[structItemsIndex][i][1]+'[i]=') fp2.write('tmpCharBuffer_v;\n') fp2.write('\t\tdelete []tmpCharBuffer_v;\n') fp2.write('\t}\n') else: fp2.write('\tint theCurrentStringlength = out'+TheStructS[2*k]+'.') fp2.write(TheStructS[structItemsIndex][i][1]+'.length();\n') fp2.write('\tfread(&theCurrentStringlength,\tsizeof(int)\t,1\t,fp);\n') fp2.write('\tchar* tmpCharBuffer = new char[theCurrentStringlength+1];\n') fp2.write('\tmemset(tmpCharBuffer, 0, theCurrentStringlength);\n') fp2.write('\tfread(tmpCharBuffer') fp2.write('\tsizeof(char)\t,theCurrentStringlength\t,fp);\n') fp2.write('\tout'+TheStructS[2*k]+'.'+TheStructS[structItemsIndex][i][1]+'=') fp2.write('tmpCharBuffer;\n') fp2.write('\tdelete []tmpCharBuffer;\n') elif(SpecialKeyWordInCplusplus[1]==TheStructS[structItemsIndex][i][0]): if(3==len(TheStructS[structItemsIndex][i])): fp2.write('\tint stringNumbers;\n') fp2.write('\tfread(&stringNumbers,\tsizeof(int)\t,1\t,fp);\n') fp2.write('\tfor(int i=0;i<stringNumbers,i++)\n') fp2.write('\t{\n') fp2.write('\t\t') fp2.write('int theCurrentWStringlength_v;\n') fp2.write('\t\tfread(&theCurrentWStringlength_v,\tsizeof(int)\t,1\t,fp);\n') fp2.write('\t\twchar_t* tmpwCharBuffer_v = new wchar_t[theCurrentWStringlength_v+1];\n') fp2.write('\t\tmemset(tmpwCharBuffer_v, 0, theCurrentWStringlength_v*sizeof(wchar_t));\n') fp2.write('\t\tfread(tmpwCharBuffer_v') fp2.write('\tsizeof(wchar_t)\t,theCurrentWStringlength_v\t,fp);\n') fp2.write('\t\tout'+TheStructS[2*k]+'.'+TheStructS[structItemsIndex][i][1]+'[i]=') fp2.write('tmpwCharBuffer_v;\n') fp2.write('\t\tdelete []tmpwCharBuffer_v;\n') fp2.write('\t}\n') else: fp2.write('\tint theCurrentStringlength = out'+TheStructS[2*k]+'.') fp2.write(TheStructS[structItemsIndex][i][1]+'.length();\n') fp2.write('\tfread(&theCurrentStringlength,\tsizeof(int)\t,1\t,fp);\n') fp2.write('\twchar_t* tmpwCharBuffer = new wchar_t[theCurrentStringlength+1];\n') fp2.write('\tmemset(tmpwCharBuffer, 0, theCurrentStringlength*sizeof(wchar_t));\n') fp2.write('\tfread(tmpwCharBuffer') fp2.write('\tsizeof(wchar_t)\t,theCurrentStringlength\t,fp);\n') fp2.write('\tout'+TheStructS[2*k]+'.'+TheStructS[structItemsIndex][i][1]+'=') fp2.write('tmpwCharBuffer;\n') fp2.write('\tdelete []tmpwCharBuffer;\n') elif(SpecialKeyWordInCplusplus[2]==TheStructS[structItemsIndex][i][0]): if(3==len(TheStructS[structItemsIndex][i])): fp2.write('\tint boolNumbers;\n') fp2.write('\tfread(&boolNumbers,\tsizeof(int)\t,1\t,fp);\n') fp2.write('\tfor(int i=0;i<boolNumbers,i++)\n') fp2.write('\t{\n') fp2.write('\t\t') fp2.write('char boolValue_v;\n') fp2.write('\t\tfread(&boolValue_v,\tsizeof(char)\t,1\t,fp);\n') fp2.write('\t\tout'+TheStructS[2*k]+'.'+TheStructS[structItemsIndex][i][1]) fp2.write('[i]=((1==boolValue_v)?true:false);\n') fp2.write('\t}\n') else: fp2.write('\tchar boolValue;\n') fp2.write('\tfread(&boolValue,\tsizeof(char)\t,1\t,fp);\n') fp2.write('\tout'+TheStructS[2*k]+'.'+TheStructS[structItemsIndex][i][1]) fp2.write('=((1==boolValue)?true:false);\n') else: fp2.write('\tRead_'+TheStructS[structItemsIndex][i][0]) fp2.write('(fp,out'+TheStructS[2*k]+'.') fp2.write(TheStructS[structItemsIndex][i][1]+');\n') fp2.write('\n') fp2.write('\treturn 0;\n') fp2.write('}\n') def outFinalWriteAndRead(FileNameAndFileMode,fp2): fp2.write('\n') fp2.write('int WriteFinal(string strPath,'+'YourParam param)\n') fp2.write('{\n') fp2.write('\tFILE *fp;\n') fp2.write('\tfp= fopen(filename.c_str(),"'+FileNameAndFileMode[1]+'");\n') fp2.write('\tif(NULL==fp)\n') fp2.write('\t{\n') fp2.write('\t\treturn -1;\n') fp2.write('\t}\n') fp2.write('\t//write Your Code Here!\n') fp2.write('\n\n\n\n\n\n') fp2.write('\tfclose(fp);\n') fp2.write('\treturn 0;\n') fp2.write('}\n') fp2.write('\n') fp2.write('int ReadFinal(string strPath,'+'YourParam param)\n') fp2.write('{\n') fp2.write('\tFILE *fp;\n') fp2.write('\tfp= fopen(filename.c_str(),"'+FileNameAndFileMode[2]+'");\n') fp2.write('\tif(NULL==fp)\n') fp2.write('\t{\n') fp2.write('\t\treturn -1;\n') fp2.write('\t}\n') fp2.write('\t//write Your Code Here!\n') fp2.write('\n\n\n\n\n\n') fp2.write('\tfclose(fp);\n') fp2.write('\treturn 0;\n') fp2.write('}\n') def outFunction(src, dest): fp = open(src, 'r+') ss = fp.readlines(); fp.close() print(len(ss)) FileNameAndFileMode =[] FunctionName =[] TheStructS = [] OutputStructToList(ss,FileNameAndFileMode , TheStructS, FunctionName) fp2 = open(dest+FileNameAndFileMode[0]+'.cpp','w+') fp2.write('#include'+'\t'+'<string>\n') fp2.write('using namespace std;\n') fp2.write('#include'+'\t'+'"'+FileNameAndFileMode[0]+'.h'+'"\n') OutWriteFileFunction(FileNameAndFileMode, TheStructS, FunctionName, fp2) OutReadFileFunction(FileNameAndFileMode, TheStructS, FunctionName, fp2) outFinalWriteAndRead(FileNameAndFileMode,fp2) fp2.flush() fp2.close() outFunction('N:/pythonProject/1.txt', 'N:/pythonProject/')
这是我的python代码,python的版本是3.3,这样我们就可以生成自己想要的读取文件的代码了,然后我们看下生成的c++代码:
#include <string> using namespace std; #include "fileName.h" int Write_sss(FILE *fp,const sss &inputStruct) { fwrite(&inputStruct.a,sizeof(int) ,15 ,fp); fwrite(&inputStruct.b,sizeof(float) ,13 ,fp); fwrite(&inputStruct.c,sizeof(char) ,1 ,fp); return 0; } int Write_ff(FILE *fp,const ff &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; } int Write_wff(FILE *fp,const wff &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; } int Write_booltest(FILE *fp,const booltest &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; } int Write_dd(FILE *fp,const dd &inputStruct) { Write_sss(fp,inputStruct.aa); Write_ff(fp,inputStruct.bb); return 0; } int Read_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; } int Read_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_v sizeof(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(tmpCharBuffer sizeof(char) ,theCurrentStringlength ,fp); inputStruct.f=tmpCharBuffer; delete []tmpCharBuffer; return 0; } int Read_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_v sizeof(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(tmpwCharBuffer sizeof(wchar_t) ,theCurrentStringlength ,fp); inputStruct.f=tmpwCharBuffer; delete []tmpwCharBuffer; return 0; } int Read_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; } int Read_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; }
代码看起来还是有些凌乱,不过大家有兴趣可以重写写一下python代码,把他变得更加健壮!好了我又丢了一块板砖出来。希望大家喜欢!