Why file containing 256 bytes is 257 bytes long?

Discussion in 'C++' started by Yandos, Sep 14, 2005.

  1. Yandos

    Yandos Guest

    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.
    Yandos, Sep 14, 2005
    #1
    1. Advertising

  2. Yandos

    mlimber Guest

    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
    mlimber, Sep 14, 2005
    #2
    1. Advertising

  3. Yandos

    Nan Li Guest

    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.
    Nan Li, Sep 14, 2005
    #3
  4. Yandos

    Kyle Guest

    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.
    Kyle, Sep 14, 2005
    #4
  5. On 14 Sep 2005 21:22:20 +0200, Yandos <> 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. ]
    Roberto Waltman, Sep 14, 2005
    #5
  6. Yandos

    Yandos Guest

    Kyle <> wrote in news:dg9u9s$2o1$:

    >> 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, Sep 14, 2005
    #6
  7. Yandos

    Yandos Guest

    "Nan Li" <> 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.
    Yandos, Sep 14, 2005
    #7
  8. Yandos

    Mike Smith Guest

    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
    Mike Smith, Sep 14, 2005
    #8
  9. Yandos

    E. Mark Ping Guest

    In article <>,
    Mike Smith <> 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
    E. Mark Ping, Sep 14, 2005
    #9
  10. Yandos

    Yandos Guest

    (E. Mark Ping) wrote in
    news:dga6d7$12b$:

    > In article <>,
    > Mike Smith <> 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.
    Yandos, Sep 15, 2005
    #10
  11. Yandos

    Pete Becker Guest

    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)
    Pete Becker, Sep 15, 2005
    #11
  12. Yandos

    Yandos Guest

    Roberto Waltman <> wrote in
    news::

    > On 14 Sep 2005 21:22:20 +0200, Yandos <> 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.
    Yandos, Sep 15, 2005
    #12
  13. Yandos

    Pete Becker Guest

    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)
    Pete Becker, Sep 15, 2005
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. M-One
    Replies:
    43
    Views:
    1,441
    Mark McIntyre
    Jul 16, 2003
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,761
    Smokey Grindel
    Dec 2, 2006
  3. NevilleDNZ
    Replies:
    1
    Views:
    592
    NevilleDNZ
    Jan 1, 2007
  4. Marcos Prieto
    Replies:
    1
    Views:
    450
    Antoine Pitrou
    Aug 5, 2010
  5. Phil C.
    Replies:
    3
    Views:
    223
    Joe Kaplan \(MVP - ADSI\)
    Feb 25, 2005
Loading...

Share This Page