Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Why file containing 256 bytes is 257 bytes long? (http://www.velocityreviews.com/forums/t448397-why-file-containing-256-bytes-is-257-bytes-long.html)

Yandos 09-14-2005 07:22 PM

Why file containing 256 bytes is 257 bytes long?
 
Hello all,

why is the file 257 bytes long in windows xp, when it contains only 256 characters?:

#include <stdio.h>
int main(void) {
int i;
FILE *out;

if ((out = fopen("256.tmp", "w")) == NULL) {
fprintf(stderr, "Cannot open input file.\n");
return 1;
}

for (i=0; i<=255; i++) {
fputc(i, out);
}
fclose(out);
return 0;
}

I have looked into the file using debug and it really contains only 256 characters. But why is the file size
reported by windows xp 257 bytes??

00000000 00 01 02 03 04 05 06 07 08 09 0D 0A 0B 0C 0D 0E .??????????????
00000010 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E ?????????????
00000020 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E ? !"#$%&'()*+,-.
00000030 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E /0123456789:;<=>
00000040 3F 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E ?@ABCDEFGHIJKLMN
00000050 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E OPQRSTUVWXYZ[\]^
00000060 5F 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E _`abcdefghijklmn
00000070 6F 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E opqrstuvwxyz{|}~
00000080 7F 80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E uclOoZ
00000090 8F 90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E CLlLlSsTtL
000000A0 9F A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE cAaEezCs
000000B0 AF B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE ES++Zz
000000C0 BF C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE ++--+-+Aa++---+
000000D0 CF D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE d?DdNe++_TU
000000E0 DF E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE ?NnnRrUt
000000F0 EF F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE *?????uRr
00000100 FF *

This is what shows dir *.tmp in cmd.exe prompt
256 TMP 257 09-14-05 9:21p

Any ideas? ;)

Thanks anyone for kind reply,
Y.

mlimber 09-14-2005 07:36 PM

Re: Why file containing 256 bytes is 257 bytes long?
 
Yandos wrote:
> why is the file 257 bytes long in windows xp, when it contains only 256
> characters?:

[snip]

This is a platform-specific question not related to C++. Try in a
Microsoft forum.

Cheers! --M


Nan Li 09-14-2005 07:37 PM

Re: Why file containing 256 bytes is 257 bytes long?
 
It works on my machine ( winxp and linux). I compiled your program on
linux with g++ and ran it.

Linux
[nan@xxx test]$ wc -c 256.tmp
256 256.tmp

And then, copied this file to a winxp machine, ran dir *.tmp. It
showed 256.


Kyle 09-14-2005 07:38 PM

Re: Why file containing 256 bytes is 257 bytes long?
 
Yandos wrote:
> Hello all,
>
> why is the file 257 bytes long in windows xp, when it contains only 256 characters?:
>
> #include <stdio.h>
> int main(void) {
> int i;
> FILE *out;
>
> if ((out = fopen("256.tmp", "w")) == NULL) {
> fprintf(stderr, "Cannot open input file.\n");
> return 1;
> }
>
> for (i=0; i<=255; i++) {
> fputc(i, out);
> }
> fclose(out);
> return 0;
> }
>
> I have looked into the file using debug and it really contains only 256 characters. But why is the file size
> reported by windows xp 257 bytes??
>
> 00000000 00 01 02 03 04 05 06 07 08 09 0D 0A 0B 0C 0D 0E .??????�????????

^^^^^
open your file in binary mode to prevent end of the line character
translation

> 00000010 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E ¤????ś§?????????
> 00000020 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E ? !"#$%&'()*+,-.
> 00000030 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E /0123456789:;<=>
> 00000040 3F 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E ?@ABCDEFGHIJKLMN
> 00000050 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E OPQRSTUVWXYZ[\]^
> 00000060 5F 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E _`abcdefghijklmn
> 00000070 6F 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E opqrstuvwxyz{|}~
> 00000080 7F 80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E ŚÇüéâäucçlëOoîZÄ
> 00000090 8F 90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E CÉLlôöLlSsÖÜTtL×
> 000000A0 9F A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE cá*óúAa��EeŹzCsŤ
> 000000B0 AF B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE ťŚŚŚŚŚÁÂESŚŚ++Zz
> 000000C0 BF C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE ++--+-+Aa++--Ś-+
> 000000D0 CF D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE ¤d?DËdNÍÎe++Ś_TU
> 000000E0 DF E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE ?ÓßÔNnn��RÚrUýÝt
> 000000F0 EF F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE ´*????§÷¸°¨?uRrŚ
> 00000100 FF
>
> This is what shows dir *.tmp in cmd.exe prompt
> 256 TMP 257 09-14-05 9:21p
>
> Any ideas? ;)
>
> Thanks anyone for kind reply,
> Y.


Roberto Waltman 09-14-2005 07:40 PM

Re: Why file containing 256 bytes is 257 bytes long?
 
On 14 Sep 2005 21:22:20 +0200, Yandos <fakemail@fakeisp.cz> wrote:
>why is the file 257 bytes long in windows xp, when it contains only 256 characters?:


It is 257 bytes long because it contains 257 characters, not 256.

> if ((out = fopen("256.tmp", "w")) == NULL) {


Read about opening files in binary mode.

>I have looked into the file using debug and it really contains only 256 characters. But why is the file size
>reported by windows xp 257 bytes??

No again, your file dump is showing 257. That is correct size.
>
> 00000000 00 01 02 03 04 05 06 07 08 09 0D 0A 0B 0C 0D 0E .??????????????


Hint: look at the 11th and 12th bytes in the previous line ...

> 00000010 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E ?????????????
> 00000020 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E ? !"#$%&'()*+,-.


Roberto Waltman

[ Please reply to the group, ]
[ return address is invalid. ]

Yandos 09-14-2005 07:59 PM

Re: Why file containing 256 bytes is 257 bytes long?
 
Kyle <invalid@e.mail> wrote in news:dg9u9s$2o1$1@atlantis.news.tpi.pl:

>> 00000000 00 01 02 03 04 05 06 07 08 09 0D 0A 0B 0C 0D 0E
>> .??????dz ????????

> ^^^^^
> open your file in binary mode to prevent end of the line character
> translation


Oh yeah, thank you! I have not noticed that :) Now i will yet have to learn how to open file in binary
mode...

My thanks also to Roberto Waltman ;)

And sorry if my question was too stupid, I'm at absolute beginning of c. All I have is c++ compiler and
this group :) I'm happy I found here nice people.

Y.

Yandos 09-14-2005 08:15 PM

Re: Why file containing 256 bytes is 257 bytes long?
 
"Nan Li" <nan.li.g@gmail.com> wrote in news:1126726661.125090.97950
@o13g2000cwo.googlegroups.com:

> It works on my machine ( winxp and linux). I compiled your program on
> linux with g++ and ran it.
>
> Linux
> [nan@xxx test]$ wc -c 256.tmp
> 256 256.tmp
>
> And then, copied this file to a winxp machine, ran dir *.tmp. It
> showed 256.
>


It is strange. I see there physicaly only 256 bytes, but XP reports 257. I will try to ask in some microsoft
releated group. Thank you for kind help with testing ;)

Y.

Mike Smith 09-14-2005 09:54 PM

Re: Why file containing 256 bytes is 257 bytes long?
 
Kyle wrote:
> Yandos wrote:
>
>> Hello all,
>>
>> why is the file 257 bytes long in windows xp, when it contains only
>> 256 characters?:
>>
>> #include <stdio.h>
>> int main(void) {
>> int i;
>> FILE *out;
>>
>> if ((out = fopen("256.tmp", "w")) == NULL) {
>> fprintf(stderr, "Cannot open input file.\n");
>> return 1;
>> }
>>
>> for (i=0; i<=255; i++) {
>> fputc(i, out);
>> }
>> fclose(out);
>> return 0;
>> }
>>
>> I have looked into the file using debug and it really contains only
>> 256 characters. But why is the file size reported by windows xp 257
>> bytes??
>>
>> 00000000 00 01 02 03 04 05 06 07 08 09 0D 0A 0B 0C 0D 0E
>> .??????�????????

>
> ^^^^^
> open your file in binary mode to prevent end of the line character
> translation


Damn! That's a good one. That could a Gimpel C-Lint question of the month!

--
Mike Smith

E. Mark Ping 09-14-2005 09:57 PM

Re: Why file containing 256 bytes is 257 bytes long?
 
In article <11ih6vvd4o4ch2e@news.supernews.com>,
Mike Smith <mikeUNDERSCOREsmith@acm.org> wrote:
>Kyle wrote:
>> open your file in binary mode to prevent end of the line character
>> translation

>
>Damn! That's a good one. That could a Gimpel C-Lint question of the month!


It's even better than that. In my experience, gcc (could be an older
version) defaults to opening in binary mode, MSVC defaults to text
mode. Yay!
--
Mark Ping
emarkp@soda.CSUA.Berkeley.EDU

Yandos 09-14-2005 11:15 PM

Re: Why file containing 256 bytes is 257 bytes long?
 
emarkp@soda.csua.berkeley.edu (E. Mark Ping) wrote in
news:dga6d7$12b$1@agate.berkeley.edu:

> In article <11ih6vvd4o4ch2e@news.supernews.com>,
> Mike Smith <mikeUNDERSCOREsmith@acm.org> wrote:
>>Kyle wrote:
>>> open your file in binary mode to prevent end of the line character
>>> translation

>>
>>Damn! That's a good one. That could a Gimpel C-Lint question of the
>>month!

>
> It's even better than that. In my experience, gcc (could be an older
> version) defaults to opening in binary mode, MSVC defaults to text
> mode. Yay!


Just tested it on Dev-C++ and it uses stdin and stdout also in text mode :( How the hell should I create
portable code? I can use setmode(fileno(stdin), O_BINARY); but that won't work on linux I think :((

It is off topic here, but before I will start other thread at other grou, maybe someone knows how to change
default mode in Dev-C++ (using some compiler directive?) without use of nonportable setmode ;)

Thnaks in advance :)
Y.


All times are GMT. The time now is 04:44 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.