2012-10-09 65 views
2

我的目标是从二进制文件中读取超过一百个“序列”(非技术术语),每个包含一个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 

} 

不幸的是,这不起作用,我甚至不知道要开始调试。任何意见将不胜感激。

+1

建议:选择“C++”或“C”。 –

+0

我会记住这一点,欢呼。 –

回答

1
  • 确保使用sizeof(char*)而不是sizeof(char)分配字符串数组。
  • 或许使用unsigned char作为长度,以避免标志混淆。
  • 为尾随分配一个字符'\0'
  • 使用ColumnName[ctr][ColumnNameLength[ctr]] = '\0'添加尾随空字节。
  • 添加一些错误检查,以防malloc返回NULL
  • 添加错误检查的情况下fread返回长度以外的东西。
  • 在未来的问题中,要更具体地说明实际失败的原因。
+0

非常感谢。现在我觉得我正在某个地方。 –

0
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]+1) * 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 
    ColumnName[ctr][ColumnNameLength[ctr]] = '\0'; 

    fread((DataTypeLength + ctr), sizeof(char), 1, pInfile); 

    DataType[ctr] = (char *) malloc((DataTypeLength[ctr]+1) * 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 
    DataType[ctr][DataTypeLength[ctr]] = '\0'; 

} 
+0

为节省我应用上述注释的麻烦而欢呼;)它编译得很好 –

1

第一个错误我在你的代码中看到正在使用< =代替<,你必须ColumnNameLength字符走了过来,因此从指数0至指数ColumnNameLength -1

对我来说,你正在使用指向指针的指针而不是使用char数组来保存字符串,这很奇怪。

+0

指针指针是C中常用的一种方法来保存动态数组字符串。 –

+0

也许我没有注意他的意思,但如果他的意思是字符串数组,那么它应该是指针的指针。 – Xee

+0

'char ** ColumnName' _is_指针的指针。正如'char ** DataType' –

相关问题