473,785 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Four or Two Bytes?

Does anybody know the answer to the following? An unsigned short is 2
bytes long. Why is the following file created as 4 bytes instead of
2?

file = fopen("data.bin ", "wb");
if (file != NULL)
{
unsigned short s = 65535;
printf("Size of unsigned short: %d bytes\n", sizeof(unsigned
short)); // 2 bytes - OK
fwrite(&s, sizeof(unsigned short), sizeof(s), file);
fclose(file); // File written as 4 bytes... hmm
}

- Hahnemann

Jun 27 '08 #1
33 1828
Hahnemann <ha************ *@gmail.comwrit es:
>Does anybody know the answer to the following? An unsigned short is 2
bytes long. Why is the following file created as 4 bytes instead of
2?
>file = fopen("data.bin ", "wb");
if (file != NULL)
{
unsigned short s = 65535;
printf("Size of unsigned short: %d bytes\n", sizeof(unsigned
short)); // 2 bytes - OK
fwrite(&s, sizeof(unsigned short), sizeof(s), file);
fclose(file); // File written as 4 bytes... hmm
}

Because you're asking it to?
Check the 3rd parameter of fwrite().

--
Chris.
Jun 27 '08 #2
On May 29, 9:39*pm, Chris McDonald <ch...@csse.uwa .edu.auwrote:
Hahnemann <hahnemann.or.. .@gmail.comwrit es:
Does anybody know the answer to the following? An unsigned short is 2
bytes long. *Why is the following file created as 4 bytes instead of
2?
file = fopen("data.bin ", "wb");
if (file != NULL)
{
* *unsigned short s = 65535;
* *printf("Size of unsigned short: %d bytes\n", sizeof(unsigned
short)); // 2 bytes - OK
* *fwrite(&s, sizeof(unsigned short), sizeof(s), file);
* *fclose(file); // File written as 4 bytes... hmm
}

Because you're asking it to?
Check the 3rd parameter of fwrite().

--
Chris.
But notice that:

printf("Size of s: %d bytes\n", sizeof(s)) ;

returns 2 bytes. How can I make the file 2 bytes using:

unsigned short x = 65535;

Is this even possible?
Jun 27 '08 #3
"Hahnemann" <ha************ *@gmail.comwrot e in message
news:2f******** *************** ***********@d1g 2000hsg.googleg roups.com...
Does anybody know the answer to the following? An unsigned short is 2
bytes long. Why is the following file created as 4 bytes instead of
2?

file = fopen("data.bin ", "wb");
if (file != NULL)
{
unsigned short s = 65535;
printf("Size of unsigned short: %d bytes\n", sizeof(unsigned
short)); // 2 bytes - OK
fwrite(&s, sizeof(unsigned short), sizeof(s), file);
fwrite(
pointer to buffer ... ok
size of an element of that buffer ... 2
number of elements to write ... 2
handle for file write should occur to ... ok

So, 2 elements of 2 bytes each should write four bytes.
fclose(file); // File written as 4 bytes... hmm
So, fwrite worked correctly. More or less, since it wrote out 4 bytes from
a 2 byte object. It could just about as easily have crashed when it went
beyond the bounds of the object.
}
- Bill

Jun 27 '08 #4
Hahnemann <hahnemann.or.. .@gmail.comwrot e:
Chris McDonald <ch...@csse.uwa .edu.auwrote:
Hahnemann <hahnemann.or.. .@gmail.comwrit es:
Does anybody know the answer to the following?
An unsigned short is 2 bytes long.
You mean _if_?
Why is the following file created as 4 bytes
instead of 2?
Because 2 lots of 2 bytes is 4 bytes.
* *unsigned short s = 65535;
* *fwrite(&s, sizeof(unsigned short), sizeof(s), file);
Because you're asking it to?
Check the 3rd parameter of fwrite().

But notice that:

printf("Size of s: %d bytes\n", sizeof(s)) ;

returns 2 bytes. *How can I make the file 2 bytes
using:
By only outputting 2 bytes once, instead of twice.
unsigned short x = 65535;

Is this even possible?
Hangliding is a topic best researched _before_ you
through yourself off the cliff. ;)

--
Peter
Jun 27 '08 #5
Hahnemann <ha************ *@gmail.comwrit es:
On May 29, 9:39*pm, Chris McDonald <ch...@csse.uwa .edu.auwrote:
>Hahnemann <hahnemann.or.. .@gmail.comwrit es:
>Does anybody know the answer to the following? An unsigned short is 2
bytes long. *Why is the following file created as 4 bytes instead of
2?
file = fopen("data.bin ", "wb");
if (file != NULL)
{
* *unsigned short s = 65535;
* *printf("Size of unsigned short: %d bytes\n", sizeof(unsigned
short)); // 2 bytes - OK
* *fwrite(&s, sizeof(unsigned short), sizeof(s), file);
* *fclose(file); // File written as 4 bytes... hmm
}

Because you're asking it to?
Check the 3rd parameter of fwrite().

But notice that:

printf("Size of s: %d bytes\n", sizeof(s)) ;

returns 2 bytes.
Yes, but ...

"%d" expects an argument of type int; you're giving it an argument of
type size_t. You should cast the value to int (one of the few cases
where a cast is appropriate):

printf("Size of s: %d bytes\n", (int)sizeof s);

And though unsigned short is 2 bytes on your implementation, that's
not guaranteed; it could be different on other implementations .
How can I make the file 2 bytes using:

unsigned short x = 65535;

Is this even possible?
Certainly.

fwrite() writes a specified number of elements. In your case, the
element(s) are of type unsigned short. The second argument is the
size of each element (you're passing ``sizeof(unsign ed short)'', which
is ok). The third argument is the number of elements (you're passing
``sizeof(s)''). How many elements do you want to write (hint: 1).

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #6
On Fri, 30 May 2008 04:59:22 +0100, Keith Thompson <ks***@mib.orgw rote:
"%d" expects an argument of type int; you're giving it an argument of
type size_t. You should cast the value to int (one of the few cases
where a cast is appropriate):

printf("Size of s: %d bytes\n", (int)sizeof s);
Generally, isn't it better to use

printf("Size of s: %lu bytes\n", (unsigned long)sizeof s);

as recommended in the answer to FAQ question 7.15?

--
Martin

Jun 27 '08 #7
Martin wrote:
On Fri, 30 May 2008 04:59:22 +0100, Keith Thompson <ks***@mib.or g>
wrote:
>"%d" expects an argument of type int; you're giving it an argument of
type size_t. You should cast the value to int (one of the few cases
where a cast is appropriate):

printf("Size of s: %d bytes\n", (int)sizeof s);

Generally, isn't it better to use

printf("Size of s: %lu bytes\n", (unsigned long)sizeof s);

as recommended in the answer to FAQ question 7.15?
Guess Keith's point is that you should cast it to whatever the format
specified expects.

Bye, Jojo
Jun 27 '08 #8
On Fri, 30 May 2008 10:19:31 +0100, Joachim Schmitz
<no*********@sc hmitz-digital.dewrote :
Guess Keith's point is that you should cast it to whatever the format
specified expects.
I'll wait for Keith's response, but I would say that sometimes it may be
better to change the format specified. Casting an unsigned type to signed
type of equal or less width has the potential for data loss.

--
Martin

Jun 27 '08 #9
Martin wrote:
On Fri, 30 May 2008 10:19:31 +0100, Joachim Schmitz
<no*********@sc hmitz-digital.dewrote :
>Guess Keith's point is that you should cast it to whatever the format
specified expects.

I'll wait for Keith's response, but I would say that sometimes it may
be better to change the format specified. Casting an unsigned type to
signed type of equal or less width has the potential for data loss.
Yes it might have, but how likely is it that a data type is really larger
than INT_MAX?
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2266
by: William S. Huizinga | last post by:
I've got an array.array of unsigned char and would like to make a slice of that array (e.g. a) become one long like I would in "C" : l = ((unsigned long *) (&a)); I have been getting what I want in this sort of manner : l = 0L l = a l += a << 8
11
5226
by: fivelitermustang | last post by:
Actually, how would I go about allocating a four-dimensional dynamic array? I only know how to make two dimensional dynamic arrays: double **v; v = new double*; for (int i=0; i<C; i++) { v = new double; }
1
1769
by: Chris Lutka | last post by:
I've been racking my brains all day over this. And I'm not the best at SQL either. I need a query that will produce the following results: Product,Warehouse,Sold_LastYear,Sold_ThisYear,Sold_3Months,Sold_MTD I've got four queries to give me the Sold_* fields. Their fields are Product, Warehouse, and Units. I also have a table which these queries originated from. The table UV_SPPROD (I didn't name it) has Product, Warehouse, Period...
19
5887
by: Lorenzo J. Lucchini | last post by:
My code contains this declaration: : typedef union { : word Word; : struct { : byte Low; : byte High; : } Bytes; : } reg;
1
2473
by: les | last post by:
If all are unsigned integers, then why not make it easy on yourself and just multiply each by a scaler to effect the shift? ie each position is just multiplied by 128^n-1 byte1 + byte2*128 + byte3 *(128 *128) + byte4 * (128 * 128 *128) >-----Original Message-----
6
2198
by: lovecreatesbeauty | last post by:
/* It seems that when an int with width of four bytes is assigned to a one byte width char, the first three bytes from left to right are discarded and the rightest byte is assigned to that char. So, I can just assign an int to a char to retrieve the rightest byte of that int, and I also use this method plus right-shift operation to get the leftest byte of an int.
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2893
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.