Thread: file size

  1. #1
    Registered User
    Join Date
    Jul 2007
    Location
    Hyderabad
    Posts
    35

    file size

    hi,

    how to calculate the size of a file after opening and write somthing in to file.

    fp=fopen(PATH,"a+");
    fprintf(fp,"%s\n","text");

    after writing into file how we know memory occupied by file.

    thnx in advance.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Huh?

    Well the most portable way to measure it's size would be to open it (preferably in read-only), seek to the end and find out the pos of the pointer, look into fseek() and ftell()

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Calling ftell() before and after the fprintf() might tell you something.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    "text" == 5 bytes.

    But the actual size on disk may be more, at least the block size.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What robwhit is saying is that there are "two different file-sizes". The first, and most obvious one is the number of bytes "actually in use" in the file. The second one is the number of blocks or clusters of blocks the file occupies, which may be more than the "in use" size.

    By the way robwith: text is a variable, so it's not easy to determine it's length from just looking at the code.

    As suggested, if you want to know how long the file is "in actual use", just use ftell (possibly combined with fseek).

    Note also that you can figure out "how much fprintf() printed" by taking it's return-value - all printf type functions return a count of the number of bytes they wrote to the destination. So if you know the size (in use) of the file originally, you can calculate the new size by using the returned count from printf.

    --
    Mats

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    fprintf returns number of characters (not bytes) printed... in text mode \n character can be represented by 2 bytes (on windows for example) so the file size may increase more than the number returned by printf...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    vart, you are indeed correct - a newline is counted as one char, even if the machine/OS definition of "newline" is CR+LF.

    And I also suspect that "wide chars" printf returns a count of how many wide-chars, rather than the number of bytes printed.

    --
    Mats

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by matsp View Post
    By the way robwith: text is a variable, so it's not easy to determine it's length from just looking at the code.
    "text" is not a variable, it's a string constant. What scenario would it not be 5 bytes?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by robwhit View Post
    "text" is not a variable, it's a string constant. What scenario would it not be 5 bytes?
    You are right, I read it wrong (why do people insist on using forms like "%s\n", "some constant text" - I've seen it several times --- unless there's some formatting involved, why not put it straight into the format string).

    --
    Mats

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    What scenario would it not be 5 bytes?
    When newlines are CR+LF? But you're right, with that code snippet it's easy to tell. Anyway, I'm sure that code was just a sample, and the OP's real code is more complicated.

    [edit] matsp beat me. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by matsp View Post
    You are right, I read it wrong (why do people insist on using forms like "%s\n", "some constant text" - I've seen it several times --- unless there's some formatting involved, why not put it straight into the format string).
    Code:
    fgets(buf, sizeof(buf), stdin);
    printf(buf); /* uh oh */
    a user could enter some formatting characters (%) and mess with the stack. So use this:
    Code:
    fgets(buf, sizeof(buf), stdin);
    printf("%s", buf);
    I guess it's just habit.
    Code:
    printf("%s\n", "some constant text");
    does this same thing as
    Code:
    puts("some constant text");
    though. Preference as to which one to use, I guess.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by robwhit View Post
    Code:
    fgets(buf, sizeof(buf), stdin);
    printf(buf); /* uh oh */
    a user could enter some formatting characters (%) and mess with the stack. So use this:
    Code:
    fgets(buf, sizeof(buf), stdin);
    printf("%s", buf);
    I guess it's just habit.
    Code:
    printf("%s\n", "some constant text");
    does this same thing as
    Code:
    puts("some constant text");
    though. Preference as to which one to use, I guess.
    Good point with the percent entered by user - but if it's a string literal, you'd know yourself if it's got percents or not, right?

    And GCC replaces printf with no extra args with a puts as an optimization when it can ;-)

    --
    Mats

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by matsp View Post
    Good point with the percent entered by user - but if it's a string literal, you'd know yourself if it's got percents or not, right?
    but what if you change your code later to use variables?
    Quote Originally Posted by matsp View Post
    And GCC replaces printf with no extra args with a puts as an optimization when it can ;-)
    you want your code to be portable, right?

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by robwhit View Post
    you want your code to be portable, right?
    Yes, of course - at least unless you know for sure this isn't going to move anywhere (it's no point in writing portable code when you are doing OTHER unportablt stuff).

    My point about printf and puts was that:
    Code:
       printf("Something to print\n");
    gets converted by gcc to
    Code:
       puts("something to print");
    Anyways, this isn't adding anything to the original post, so lets cut it now...

    --
    Mats

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by matsp View Post
    My point about printf and puts was that:
    Code:
       printf("Something to print\n");
    gets converted by gcc to
    Code:
       puts("something to print");
    oh ok, I was thinking about this:
    Code:
    const char *b = "Something to print\n";
    printf(b);
    Quote Originally Posted by matsp View Post
    Anyways, this isn't adding anything to the original post, so lets cut it now...
    agreed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM