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.
C++orC.