473,385 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.
Sep 14 '05 #1
12 3122
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

Sep 14 '05 #2
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.

Sep 14 '05 #3
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.

Sep 14 '05 #4
On 14 Sep 2005 21:22:20 +0200, Yandos <fa******@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. ]
Sep 14 '05 #5
Kyle <in*****@e.mail> wrote in news:dg**********@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.
Sep 14 '05 #6
"Nan Li" <na******@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.
Sep 14 '05 #7
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
Sep 14 '05 #8
In article <11*************@news.supernews.com>,
Mike Smith <mi*****************@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
em****@soda.CSUA.Berkeley.EDU
Sep 14 '05 #9
em****@soda.csua.berkeley.edu (E. Mark Ping) wrote in
news:dg**********@agate.berkeley.edu:
In article <11*************@news.supernews.com>,
Mike Smith <mi*****************@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.
Sep 14 '05 #10
Yandos wrote:

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

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Sep 14 '05 #11
Roberto Waltman <us****@rwaltman.net> wrote in
news:2s********************************@4ax.com:
On 14 Sep 2005 21:22:20 +0200, Yandos <fa******@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.


Thank you, with "wb" it works :)
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.


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? :)

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


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.
Sep 14 '05 #12
Yandos wrote:
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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Sep 15 '05 #13

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

Similar topics

2
by: Jeremy Jones | last post by:
I've written a piece of code that iterates through a list of items and determines the filename to write some piece of data to based on something in the item itself. Here is a small example piece...
7
by: Joseph | last post by:
Hi, I'm having bit of questions on recursive pointer. I have following code that supports upto 8K files but when i do a file like 12K i get a segment fault. I Know it is in this line of code. ...
5
by: Chathu | last post by:
Hello everyone........... I have a problem on retriving a content of a binary file I wrote into. My program user structures, dynamic allocation of memory and files. I take the infomation into a...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
2
by: Yuriy | last post by:
I have a .NET server button on a form. When the user clicks it, I'd like to (behind the scenes) create a pdf file containing the data that they're currently viewing, then somehow activate the "Do...
1
by: Roy | last post by:
Hi, I have a problem that I have been working with for a while. I need to be able from server side (asp.net) to detect that the file i'm streaming down to the client is saved...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
4
by: kev | last post by:
Hi folks, I have created a database to store information on equipments. During the first level of registration, there is a form that i need the user to fill up details on the equipment testing....
15
by: patf | last post by:
Hi - experienced programmer but this is my first Python program. This URL will retrieve an excel spreadsheet containing (that day's) msci stock index returns. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.