ls -l and st.size in stat struct

This is a discussion on ls -l and st.size in stat struct within the C Programming forums, part of the General Programming Boards category; Hi Everyone: I hope you can help me with this but its driving me nuts and Im not seeing the ...

  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
    Katy, Texas
    Posts
    2,309
    What EXACTLY is the issue? I fixed "doesn't work" last week, so it must be something else.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    Katy, Texas
    Posts
    2,309
    Or, use a float, or, add 1023 then divide by 1024 just as you are to round up.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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,185
    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
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by cpjust View Post
    I think you're right.
    My four favorite words.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21