-
fread problem
Hi
I am having problem reading file using fread.
I was looking at emule code in which at certain point it read a file server.met
using fread.
I copied that code and build simple VC++ application to see its effect.
When ever i run this code in VC++6 the 1) displays 24 which is right but
2) displays 16908288, may be garbage value (It should be 12 as when i
compile and run actual application 4m which i copied it diplays 24 and then 12).
I ran the code using Borland c ver 3 and it diplays the correct value 24 and
and then 12.
So why i am getting 24 and then 16908288(instead of 12)
The file server.met can be downloded 4m
http://www.zshare.net/download/server-met.html
Code:
struct ServerMet_Struct {
unsigned char ip[4];
unsigned short port;
unsigned int tagcount;
};
FILE* servermet = fopen("server.met", "rb");
unsigned int fservercount;
fread(&fservercount,4,1,servermet);
char buff[10];
itoa(fservercount,buff,10);
1) AfxMessageBox(buff); //or cout<<buff;
ServerMet_Struct sbuffer;
fread(&sbuffer,1,sizeof(ServerMet_Struct),servermet);
itoa(sbuffer.tagcount,buff,10);
2) AfxMessageBox(buff); // or cout<<buff;
-
check out the actual parameters of fread.
Code:
fread(&sbuffer,1,sizeof(ServerMet_Struct),servermet);
ssharish2005
-
The code is 100% same 4m actual coder. I have double checked it.
thanks.
-
Your server.met does not contain PG-13 type material -- don't do that.
Are you aware that structures may be padded?
http://c-faq.com/struct/padding.html
http://msdn.microsoft.com/library/de...vcrefAlign.asp
-
Thanks very much
Thanks very much
I was dissappointed first and start checking project setting where
i find compiler options. My one was not using /Zp1 /wd4200
switches which i think were need for alignment and padding of
structures.