我的目标是从二进制文件中读取超过一百个“序列”(非技术术语),每个包含一个char1(要跟随的字符串的长度) ,string1,char2,string2。这里关键的东西似乎是动态内存分配,指针和循环。这是我的做法:带有多个字符串和字符的C/C++二进制文件读取
char *ColumnNameLength = (char *) malloc(Repetitions * sizeof(char));
char *DataTypeLength = (char *) malloc(Repetitions * sizeof(char));
char **ColumnName = (char **) malloc(Repetitions * sizeof(char));
char **DataType = (char **) malloc(Repetitions * sizeof(char));
for (int ctr = 0; ctr <= Repetitions ; ColumnNameLength[ctr] = DataTypeLength[ctr] = NULL, ctr++)
;
for (int ctr = 0; ctr <= Repetitions ; *(ColumnName+ctr) = DataType[ctr] = NULL, ctr++)
;
for (int ctr = 0; ctr <= FieldCount; ctr++)
{
fread((ColumnNameLength + ctr), sizeof(char), 1, pInfile);
*(ColumnName + ctr) = (char *) malloc(ColumnNameLength[ctr] * sizeof(char));
fread(ColumnName[ctr], sizeof(char), ColumnNameLength[ctr], pInfile);
//I should add '\0' at the end of each read string, but no idea how
fread((DataTypeLength + ctr), sizeof(char), 1, pInfile);
*(DataType + ctr) = (char *) malloc(DataTypeLength[ctr] * sizeof(char));
fread(&DataType[ctr], sizeof(char), DataTypeLength[ctr], pInfile);
//I should add '\0' at the end of each read string, but no idea how
}
不幸的是,这不起作用,我甚至不知道要开始调试。任何意见将不胜感激。
建议:选择“C++”或“C”。 –
我会记住这一点,欢呼。 –