Thread: ls -l and st.size in stat struct

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    8

    Question ls -l and st.size in stat struct

    Hi Everyone:

    I hope you can help me with this but its driving me nuts and Im not seeing the solution, I'm trying to code my own version of ls -l:

    And I'm stuck Im trying to output the total size of the files in a directory, converted the byte size value of st_size by dividing it by 1024 e.g. astatbuff->st_size/1024, now all I need to do is to total these values but for the life of me a cannot get it to work.

    Any help would be much appreciated.
    Christian

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    What EXACTLY is the issue? I fixed "doesn't work" last week, so it must be something else.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Shouldn't you do:
    Code:
    astatbuff->st_size/1024 + 1
    otherwise the file size will say 0 if it's < 1024 bytes.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Or, use a float, or, add 1023 then divide by 1024 just as you are to round up.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    8

    Exclamation ls -l and st.size in stat struct

    Hi:

    Thanks to everyone who replied and thanks for the handy info but what I really wanted to know is once the file byte sizes have been rounded up how can I calculate the total file sizes e.g file 1 = 15kb and file 2 = 15k, 15kb + 15kb = 30kb total

    Thanks once again.
    Christian

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cljones81 View Post
    Hi:

    Thanks to everyone who replied and thanks for the handy info but what I really wanted to know is once the file byte sizes have been rounded up how can I calculate the total file sizes e.g file 1 = 15kb and file 2 = 15k, 15kb + 15kb = 30kb total

    Thanks once again.
    Christian
    Add them up.

    No really: whenever you print a number (or if you're not printing them, when you compute a number), add it to the total.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by me
    Shouldn't you do:

    Code:
    astatbuff->st_size/1024 + 1
    astatbuff->st_size/1024 + 1otherwise the file size will say 0 if it's < 1024 bytes.
    Quote Originally Posted by Todd Burch View Post
    Or, use a float, or, add 1023 then divide by 1024 just as you are to round up.
    I think you're right. My example would be off by 1 if the file was exactly divisible by 1024.
    Code:
    (astatbuff->st_size + 1023) / 1024

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by cpjust View Post
    I think you're right.
    My four favorite words.
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed