Why file containing 256 bytes is 257 bytes long?

Y

Yandos

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 ¦Çüéâä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.
 
M

mlimber

Yandos said:
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
 
N

Nan Li

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.
 
K

Kyle

Yandos said:
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.
 
R

Roberto Waltman

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. ]
 
Y

Yandos

Kyle said:
^^^^^
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.
 
Y

Yandos

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.
 
M

Mike Smith

Kyle said:
^^^^^
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!
 
E

E. Mark Ping

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!
 
Y

Yandos

(e-mail address removed) (E. Mark Ping) wrote in

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.
 
P

Pete Becker

Yandos said:
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 :((

Don't mess with stdin. It's doing what it's supposed to do. Keep in
mind, text file conventions on different OS's are different. When you
transfer text files from one OS to another you have to make adjustments
to them. ftp knows how to do that (except for some brain-dead Linux
implementations that assume all the world looks just like Linux).
 
Y

Yandos

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


Read about opening files in binary mode.

Thank you, with "wb" it works :)
No again, your file dump is showing 257. That is correct size.

Damn. I was confused with last character on position 0100h, but since it starts on position 0000h which
is also counted, it must be 0101h long :) That CRLF/EOF translation really sucks. I don't understand why
windows still have to use text mode as default - 99% of all programs cannot be run in dos mode and they
are trying to be compatible with CP-M? :)
Hint: look at the 11th and 12th bytes in the previous line ...

Thank you, now I see it :)
Roberto Waltman

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

I'm very grateful for your help, also in the other thread, you helped me a lot :)
Y.
 
P

Pete Becker

Yandos said:
I don't understand why
windows still have to use text mode as default

It's not Windows, it's compilers that target Windows. At least, it's the
ones that conform to the C standard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top