我的目标是从二进制文件中读取超过一百个“序列”(非技术术语),每个序列都包含一个 char1(要跟随的字符串的长度)、string1、char2、string2。这里的关键似乎是动态内存分配、指针和循环。我是这样做的:
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
}
不幸的是,这不起作用,我什至不知道要开始调试。任何建议将不胜感激。