Hi,
Thanks for your help. I was using wrong slashes and the code for TC also misses the fwrite command. fwrite is returning 1 maybe because its 1 record.
fread() and fwrite() return the number of items successfully read or
written (i.e., not the number of characters).
If I check the properties file size is 40 bytes.
Code:
struct lib{
char bname[30];
int id;
float price;
};
Hi,
Thanks for your attention. I dont know how this sizeof works in VC but in turboc its giving me 36.
The sizeof() works as its supposed to work.
The number of bytes that structure takes up is sizeof(that struct). It isn't 36, 38, 40, or whatever other "hard-coded" number you can think up. If you get an interview question, and the interviewer shows you a struct or class and asks you "how big is this struct s, in bytes?", there is only one right answer -- sizeof( s ). If you sit there and start counting the members and adding up a number, a competent interviewer will clearly see you're either a beginner or not a very good C or C++ programmer.
The compiler is free to add whatever space in-between and/or after the members. Usually, the compiler will attempt to align the members in memory so that it is advantageous for the underlying OS to access that member. Therefore the total number of bytes that the struct takes up is whatever the compiler says it is, because it is the compiler that is responsible for laying out the members in memory in whichever way it sees fit.
Last, as Philip pointed out, ints, floats, doubles, etc. can be different sizes depending on compiler and environment. Unlike Java, C++ has no set size for data types (except for char). There are rules in C++ stating which types must be at least as large as another type, but no concrete number as to how large an individual data type should be (again, except for char, which must be 1 byte).
Regards,
Paul McKenzie
Last edited by Paul McKenzie; April 18th, 2011 at 01:37 PM.
Once, I was asked at an interview "Which is the size of char, short, int, and long".
My answer was "I don't care because I have the sizeof operator". "Hmmm... depends..." said the interviewer and I didn't get the job.
Resuming by short what Paul already stated:
The only one C/C++ type for which the sizeof is guaranteed known is (signed, unsigned) char.
The rest are implementation-defined, i.e. depend on each particular compiler.
Last edited by ovidiucucu; April 18th, 2011 at 02:11 PM.
Reason: typo
Bookmarks