2

My objective is to read from a binary file over a hundred of 'sequences' (non-technical term), each comprising of a char1 (length of the string to follow), string1, char2, string2. The key things here seem to be dynamic memory allocation, pointers and looping. This is how I did it:

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

}

Unfortunately this does not work and I do not even know were to start debugging. Any advice will be much appreciated.

1
  • 1
    Advice: Choose either C++ or C. Commented Oct 9, 2012 at 4:14

3 Answers 3

1
  • Make sure to allocate the string arrays using sizeof(char*) not sizeof(char).
  • Perhaps use unsigned char for lengths, to avoid sign confusion.
  • Allocate one more character for trailing '\0'.
  • Add trailing null byte using ColumnName[ctr][ColumnNameLength[ctr]] = '\0'.
  • Add some error checking in case malloc returnins NULL.
  • Add error checking in case fread returns something other than the length.
  • In future questions, be more specific about what actually fails.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. Now I feel I am getting somewhere.
1

The first bug i see in your code is using <= instead of < , you have ColumnNameLength chars to go over, thus from index 0 to index ColumnNameLength -1.

It's weird to me that you are using a pointer to pointer instead of using a char array for saving the string.

3 Comments

pointer of pointers is a common way in C to hold a dynamic array of strings.
Maybe i didn't pay attention to what he meant, but if he meant array of strings then it should be pointer of pointers.
char **ColumnName is a pointer of pointers. As is char **DataType
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';

}

1 Comment

Cheers for saving me the trouble of applying the above comments ;) It compiles nicely

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.