Hi.
I typed a file of data directly int a text document, made sure not a single whitespace was present and also made sure no extra newlines were present. I then saved it as a different file extension ".dat" so that I could fread it as a binary file.
Unfortunately, as I fread the data into structures and sent it to the console I found many errors. It read too many pieces of data into each structure thus leaving garbled garbage at the end of the printout.
Here are the facts:
the structure:
typedef struct atom{
char elem[25];
char symb[25];
int numb;
float mass;
float dens;
float melt;
float boil;
float spht;
};
I then declared an array of these structures like so:
atom atom_array[max_num/8];
...where "max_num" is the number of lines I counted in the file (no newline spacing is in the file and I printed out "max_num"s value---it was correct)...
I read it in like this:
int t;
for( t=0; t<(max_num/8); t++)
{ fread(&atomo[t], sizeof(atom), 1, fr); }
...where "fr" is the "FILE" pointer...
And printed it out like this:
for( t=0;t<max_num/8;t++){
printf("%s \n%s \n%i \n%.2f \n%.2f \n%.2f \n%.2f \n%.2f \n",atomo[t].elem, atomo[t].symb, atomo[t].numb, atomo[t].mass, atomo[t].dens, atomo[t].melt, atomo[t].boil, atomo[t].spht); }
So what went wrong? Any help is appreciated...