473,765 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ?@ABCDEFGHIJKLM N
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 _`abcdefghijklm n
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++Z z
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++_T U
000000E0 DF E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE ?NnnRrU 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 #1
12 3179
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 ?@ABCDEFGHIJKLM N
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 _`abcdefghijklm n
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******@fakei sp.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.new s.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.go oglegroups.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.supernew s.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.CSU A.Berkeley.EDU
Sep 14 '05 #9
em****@soda.csu a.berkeley.edu (E. Mark Ping) wrote in
news:dg******** **@agate.berkel ey.edu:
In article <11************ *@news.supernew s.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

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

Similar topics

2
1586
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 of code to show the type of thing I'm doing:: ################################# file_dict = {} a_list = b_list =
7
3538
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. How do i make the last pointer in the indirect sector that has another level of indirect pointer, and be defined recursively to support infinite large files? -code-
5
2440
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 dynamicaly allocated structure and then write it to a binary file using fwrite() with appending mode. I can retrive the data I wrote while the program is running. But if I stop the program and re-execute it I can't read the things I wrote to the...
0
3940
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. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
2
1198
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 you want to download or save this file?" dialog. The pdf file I'm already creating. That's no big deal. But now, how do I make this newly-created file available for the user to download? Can I activate that dialog somehow? I'd like to do...
1
5400
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 completely/succsessfully on the client's computer before updating some metadata on the server (file downloaded date for instance) However, All examples i have tried, and all examples I have found that other people says works - doesn't work for me :-(
1
6508
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" setting to be "E_ALL", notices are still not getting reported. The perms on my file are 664, with owner root and group root. The php.ini file is located at /usr/local/lib/php/php.ini. Any ideas why the setting does not seem to be having an effect? ...
4
5490
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. i have done this one.Now what i need is to enable the users to upload files and save it into the corresponding table. Example: 3. A laser inventory form has been completed for each 3b or 4 laser and submitted to the Laser Safety Officer...
15
9439
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. http://www.mscibarra.com/webapp/indexperf/excel?priceLevel=0&scope=0&currency=15&style=C&size=36&market=1897&asOf=Jul+25%2C+2008&export=Excel_IEIPerfRegional Want to write python to download and save the file. So far I've arrived at this:
0
9398
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10156
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
9832
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...
1
7375
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
5275
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
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
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.