本文介绍了fasteste填充结构的方式。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 从我之前的帖子... 如果我有结构, struct sFileData { char * sSomeString1; char * sSomeString2; int iSomeNum1; int iSomeNum2; sFileData(){...}; ~sFileData(){...}; sFileData(const sFileData&){... }; const sFileData operator =(const sFileData& s){...} }; 我读过文件如下 FILE * f = fopen(szPath," rb"); int nLineSize = 190; BYTE b [nLineSize + 1]; fread(b,sizeof(BYTE),nLineSize,f); int numofrecords = atoi(b); //第一行只是记录数量, //读取数据本身。 while(fread(b,sizeof(BYTE),nLineSize, f)== nLineSize) { //填充数据 //每个项目的位置已知 // sString1 = 0-> 39,数据后填空格填空 // sString2 = 40-> 79,数据后填空格填空 // iNum1 = 80-> 99,数据后填空格填空 // iNum2 = 100->结束,数据后填空格填空 } 什么是将数据填充到数组中的最佳方法,(向量)? 非常感谢。 Simon。 解决方案 我认为nLineSize大于100.然后,一旦你知道结构的数量就符合 // yourvector.reserve( numofrecords); //自己读取数据 while(fread(...) { yourvector.push_back( sFileData( std :: string( b,b + 40).c_str(), std :: string(b + 40,b + 80).c_str(), strtol(std :: string(b + 80,b + 100).c_str(),10,0), strtol(std :: string(b + 100,b + nLineSize).c_str(),10, 0) ) ); } 您需要为其创建另一个构造函数你的''sFileData'', 将需要两个指向const char的指针,以及两个整数(或long): sFileData(char const *, char const *,int,int); 拿出那些指针并从中提取C字符串以创建 成员。 一般来说,我认为最好让''std :: string''作为成员代替 ''char *''。如果你做了那个 切换,你可能需要修复你的其余课程。 V 这种方式是_not _ fast,因为有大量不必要的内存 分配。 Simon,你从一开始就有了正确的想法,但 数据结构可以修改为: struct sFileData { char sSomeString1 [40]; char sSomeString2 [40]; int iSomeNum1; int iSomeNum2 ; .... }; 然后,您可以使用数组或向量。由于你提前知道了大小 ,你可以创建一个数组: struct sFileData array [numofrecords]; //读取数据本身。 int i = 0; while(fread(b,sizeof(BYTE),nLineSize,f)== nLineSize) { array [i] = *(struct sFileData *)& b; ++ i; } From my previous post... If I have a structure, struct sFileData{char*sSomeString1;char*sSomeString2;int iSomeNum1;int iSomeNum2;sFileData(){...};~sFileData(){...};sFileData(const sFileData&){...};const sFileData operator=( const sFileData &s ){...}}; I read the file as follows FILE *f = fopen( szPath, "rb" ); int nLineSize = 190;BYTE b[nLineSize+1]; fread( b, sizeof(BYTE), nLineSize, f );int numofrecords = atoi( b ); // first line is num of records only, // read the data itself.while( fread( b, sizeof(BYTE), nLineSize, f ) == nLineSize ){// fill data// The locations of each items is known// sString1 = 0->39, with blank spaces filler after data// sString2 = 40->79, with blank spaces filler after data// iNum1 = 80->99, with blank spaces filler after data// iNum2 = 100->end, with blank spaces filler after data} what would be the best way to fill the data into an array, (vector)? Many thanks. Simon. 解决方案 I presume nLineSize is greater than 100. Then, something in line with // as soon as you know the number of structuresyourvector.reserve(numofrecords); // read the data themselveswhile (fread(... ){yourvector.push_back(sFileData(std::string(b, b+40).c_str(),std::string(b+40, b+80).c_str(),strtol(std::string(b+80,b+100).c_str(),10,0),strtol(std::string(b+100,b+nLineSize).c_str(),10,0 )));} You will need to create another constructor for your ''sFileData'',which will take two pointers to const char, and two ints (or longs): sFileData(char const*, char const*, int, int); Take those pointers and extract the C strings from them to create yourmembers. In general, I think it''s better to have ''std::string'' as members insteadof ''char*''. You may need to fix the rest of your class if you make thatswitch. V 这篇关于fasteste填充结构的方式。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-10 01:55