Well, hello.

I have a problem with reading data from binary files into arrays.

For example, let's say that we have d.bin file which contains

pairs of strings with max lenght of 32 chars and double values like this:

string12.334343
AS34.34
something3

where the string always takes up 32 bytes.

Basically, the binary file has a series of types record defined like this:

typedef struct{
char name[32];
double value;
}record;

The question is how would I fread the strings into one array and values into another so that

the namearray would contain (string AS something)

and the valuearray (12.334343 34.34 3) ??

Since the input is sure to be divided only between string and value, and that there won't be any input like stri3.1415ng, keep on looking at the input until you find an integer. At which point, what you have read so far can be put into the structure's array component. The rest of the input can be converted using atof().

I am not quite sure what do you mean.

I could do this:

record test; //declaration of test which is type record

FILE* db=fopen("d.bin","rb");

fread(test.name,sizeof(test.name),1,db)

But from that point how do I put test.name inside an array of strings??

Tried something like this:

for(i=0;(not sure which would be the right condition);i++){
     
    fread(test.name,sizeof(test.name),1,db)
    name[i]=test.name;
}

And it didn't work.

Why do you need test? Read directly to where you want it to be. Of course, you can also memcpy(name[i], test.name, sizeof(test.name)) .

You could give something similar to this construct a shot:

SOMETYPE inputbuffer[32];
i = 0;
do{
    fread(&inputbuffer[i], sizeof(char), 1, stream);
}while(inputbuffer[i++] != '\n'); // Until we reach the end of a line

At this point, you have yourself something to fill an instance of a record. You can then inspect what you have inside the inputbuffer and determine if it belongs to either .name or .value .

@asrockw7
Not sure, but I think that binary files don't have '\n'. I wrote it like that for it to be more transparent.

I tried the following. It doesn't work for the last entry for some reason. Can anyone tell me why?

db=fopen("d.bin","rb"); if(!db) exit(1);
    pgr=1;
    fseek(db,0,SEEK_SET);

    for(j=0;;j++){
    pgr=fread(&structs[j],sizeof(struct record),1,db);
    //printf("%s %lf\n",structs[j].naziv,structs[j].value);
    if (pgr!=1) break;
    }

I always work on .txt so I don't know. lol

How do you know that each line of data will fit in your single instance of struct? Say you have a line shorter than sizeof struct record, then your fread would eat through the next line which you intend to belong to another instance of struct.

Your data will not get automatically get sorted in the structure. fread does a direct binary read, and a structure has inconsistent internal representation. This is why you cannot assign two seemingly similar structures of different type(Say they both only contain two instances of int but are different definitions), there is no guarantee.

You are only testing for EOF, or an error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.