>now all i need is to fread the structs!
Well, there is a little more to it. As I mentioned, the dynamic allocation will be required before you can read the arrays into memory. And by attaching a file, I meant the file you intend to read.
To give you something to chew on, here is a rather ugly example. I use foo() to create my own file in place of the one you are trying to read but didn't attach. The bar() function is where I read it back. This will be similar to the code you are developing.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void foo(const char *filename)
{
FILE *file = fopen(filename, "wb");
if(file)
{
unsigned int value = 2;
char buffer[BUFSIZ] = "Dave_Sinkula";
fwrite(&value, sizeof(value), 1, file);
value = strlen(buffer);
fwrite(&value, sizeof(value), 1, file);
fwrite(buffer, sizeof(*buffer), value, file);
strcpy(buffer, "cooliced");
value = strlen(buffer);
fwrite(&value, sizeof(value), 1, file);
fwrite(buffer, sizeof(*buffer), value, file);
fclose(file);
}
}
struct sRead
{
unsigned int names;
struct
{
unsigned int length;
char *array;
} *name;
};
void bar(const char *filename)
{
FILE *file = fopen(filename, "rb");
if(file)
{
struct sRead info;
fread(&info.names, sizeof info.names, 1, file);
printf("info.names = %u\n", info.names);
info.name = malloc(info.names * sizeof(*info.name));
if(info.name)
{
unsigned int u;
for(u = 0; u < info.names; ++u)
{
fread(&info.name[u].length, sizeof info.name[0].length, 1, file);
printf("info.name[%u].length = %d\n", u, info.name[u].length);
info.name[u].array = malloc((info.name[u].length * sizeof(*info.name[0].array)) + 1);
if(info.name[u].array)
{
fread(info.name[u].array, info.name[u].length, sizeof(*info.name[0].array), file);
info.name[u].array[info.name[u].length] = '\0';
printf("info.name[%u].array = \"%s\"\n", u, info.name[u].array);
}
}
}
fclose(file);
}
}
int main(void)
{
const char filename[] = "file.txt";
foo(filename);
bar(filename);
return(0);
}
/* my output
info.names = 2
info.name[0].length = 12
info.name[0].array = "Dave_Sinkula"
info.name[1].length = 8
info.name[1].array = "cooliced"
*/
And note that I'm sloppy and didn't free the memory that was dynamically allocated, nor did I check return values for success of fread and fwrite.