Luc Holland wrote:
Hey,
I'm working on a program that reads a binary file. It's opened with
====
if ((f1=fopen(argv[1],"rb"))==NULL) {
fprintf(stderr,"Error opening %s for reading . . .\n",argv[1]);
exit(2);
}
====
The structure of the file is:
unsigned int
unsigned short
unsigned short
unsigned short
unsigned short
unsigned short
<more data>
{eof}
I have a structure:
typedef struct struct_fileinfo {
unsigned int num;
unsigned short ins;
unsigned short del;
unsigned short nex;
unsigned short est;
unsigned short get;
} FILEINFO;
int main(int argc, char** argv) {
FILEINFO data;
FILE* f1;
[......]
I use:
if ((fread(&data,sizeof(FILEINFO),1,f1))!= 1) {
fprintf(stderr,"\n\t\tError reading into fileinfo\n\n");
exit(3);
}
to read a chunk the size of FILEINFO.
When I go to output the data read with
fprintf(stdout,"%hd",data.num);
data.num is type unsigned int. The specifier should be %u.
I dont get a value I'm expecting. Am I using the proper printf
identifier (%hd for unsigned int and %hu for unsigned short)? Should i
be casting the values before output? Any suggestions? OS is Solris 8
on Enterprise 220R.
You need to be aware that the sizeof the individual struct members may
not equate to the sizeof the struct. The struct may have padding
making it larger than the total members size. This would cause
an alignment problem with your code.
See the faq:
http://www.eskimo.com/~scs/C-faq/q2.13.html
If you can't modify the data file, you could write a function
that will read the data file and store the data in a struct.
#include <stdio.h>
#include <stdlib.h>
/* data in data file
unsigned int
unsigned short
unsigned short
unsigned short
unsigned short
unsigned short
<more data>
{eof} */
typedef struct struct_fileinfo {
unsigned int num;
unsigned short ins;
unsigned short del;
unsigned short nex;
unsigned short est;
unsigned short get;
} FILEINFO;
int GetData(FILEINFO *p,const char *fname);
int main(void)
{
const char *fname = "test.dat";
unsigned short i;
unsigned int d = 99;
FILEINFO test = {0,0,0,0,0,0};
FILE *fp;
/* Create a test file */
if((fp = fopen(fname,"wb")) == NULL) exit(EXIT_FAILURE);
if(1 != fwrite(&d,sizeof(d),1,fp)) exit(EXIT_FAILURE);
for(i = 0; i < 5;i++)
if(1 != fwrite(&i,sizeof(i),1,fp)) exit(EXIT_FAILURE);
fclose(fp);
printf("The sizeof the indivual elements: %u\n",
sizeof(short)*5+sizeof(unsigned int));
printf("The sizeof of the struct is: %u\n",
sizeof(FILEINFO));
if(GetData(&test,fname))
printf("test.mum = %u\ntest.ins = %hd\n"
"test.del = %hd\ntest.nex = %hd\n"
"test.est = %hd\ntest.get = %hd\n",
test.num,test.ins,test.del,test.nex,
test.est,test.get);
return 0;
}
int GetData(FILEINFO *p,const char *fname)
{
FILE *fp;
int flag = 1;
if((fp = fopen(fname,"rb")) == NULL) return 0;
if(1 != fread(&p->num,sizeof(p->num),1,fp)) flag = 0;
if(1 != fread(&p->ins,sizeof(p->ins),1,fp)) flag = 0;
if(1 != fread(&p->del,sizeof(p->del),1,fp)) flag = 0;
if(1 != fread(&p->nex,sizeof(p->nex),1,fp)) flag = 0;
if(1 != fread(&p->est,sizeof(p->est),1,fp)) flag = 0;
if(1 != fread(&p->get,sizeof(p->get),1,fp)) flag = 0;
fclose(fp);
return flag;
}
--
Al Bowers
Tampa, Fl USA
mailto:
xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/