Thread: it generates weird file length

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87

    it generates weird file length

    Hi. I wrote a program that should make a file of pseudo random numbers. It should have a length of 1MB, because n=1'000'000.

    The program works, but the resulting file have actually length of 1'003'904. Why doesn't the file have a length of 1'000'000 exactly?

    Here is the source code:

    Code:
    /* this program is a pseudo random generator using function rand() */
    #include<stdio.h>
    #include<math.h> 
    main()
    {
    FILE *f1;
    long int n,i;
    int z;
    
    printf("Processing..\n\n");
    f1=fopen("output.out","w"); /*Open the file Input*/
    
    n=1000000;
    
    for(i=1;i<=n;i++){
    z=rand() % 256;
    putc(z,f1); /*write a character to input*/
    }
    
    fclose(f1); /*close the file input*/
    printf("Done.");
    getch();
    }
    Thank you for help.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Are you on windows? If so, every time you intend to write 0x0A, it will write 0x0D 0x0A instead (I might have the A and D switched), unless you write the file in binary mode.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    Yes I use Windows 7, 64 bit.

    So I guess I have to learn how to make a binary file. Thank you.
    Last edited by nerio; 07-14-2011 at 09:02 AM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by nerio View Post
    Yes I use Windows 7, 64 bit.

    So I guess I have to learn how to make a binary file. Thank you.
    It's exactly as easy as making a text file as you are doing. (You should read what fopen does.)

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by nerio View Post
    Yes I use Windows 7, 64 bit.
    So I guess I have to learn how to make a binary file. Thank you.
    Hmm.....How do I work with files?

    The layout of this site is so obfuscated.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. file length
    By Raven Arkadon in forum C++ Programming
    Replies: 13
    Last Post: 08-26-2006, 08:33 AM
  2. string generates
    By bazzano in forum C Programming
    Replies: 2
    Last Post: 10-02-2005, 08:23 AM
  3. Weird modification to string length
    By ChwanRen in forum C Programming
    Replies: 0
    Last Post: 08-17-2003, 10:45 AM
  4. file length
    By Nor in forum C++ Programming
    Replies: 8
    Last Post: 07-16-2003, 02:58 AM
  5. File Length
    By Breebo in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2002, 09:56 PM