size of block device by ftell()

Discussion in 'Python' started by Seongsu Lee, Nov 20, 2007.

  1. Seongsu Lee

    Seongsu Lee Guest

    Hi all,

    I want to get the size of a block device by ftell(). I found that I
    can get
    the size of a device by seek() and tell() in Python. But not in C.

    What is difference between them? How can I get the size of a block
    device by ftell()?


    # whoami
    root

    # du -sh /etc/services
    364K /etc/services

    # df -h | grep hdb1
    /dev/hdb1 111G 2.0G 103G 2% /mnt/data1

    ---------------------------------------------------------------------------
    # cat ftell_test.c
    #include <stdio.h>

    long int ftell_test(const char *d);

    int main(void) {
    printf("%ld\n", ftell_test("/etc/services"));
    printf("%ld\n", ftell_test("/dev/hdb1"));

    return 0;
    }

    long int ftell_test(const char *d) {
    FILE *fp;
    long int l;

    fp = fopen(d, "r");
    fseek(fp, 0L, SEEK_END);
    l = ftell(fp);
    fclose(fp);

    return l;
    }

    ---------------------------------------------------------------------------
    # cat ftell_test.py
    #!/usr/bin/env python

    def ftell_test(d):
    f = open(d, "r")
    f.seek(0, 2) # SEEK_END is 2
    return f.tell()

    if __name__ == '__main__':
    print ftell_test("/etc/services")
    print ftell_test("/dev/hdb1")

    ---------------------------------------------------------------------------
    # gcc ftell_test.c -o ftell_test

    ---------------------------------------------------------------------------
    # ./ftell_test
    362031
    -1

    ---------------------------------------------------------------------------
    # ./ftell_test.py
    362031
    120034091520
     
    Seongsu Lee, Nov 20, 2007
    #1
    1. Advertising

  2. Seongsu Lee

    Bill Marcum Guest

    ["Followup-To:" header set to comp.os.linux.development.system.]
    On 2007-11-20, Seongsu Lee <> wrote:
    >
    >
    > Hi all,
    >
    > I want to get the size of a block device by ftell(). I found that I
    > can get
    > the size of a device by seek() and tell() in Python. But not in C.
    >
    > What is difference between them? How can I get the size of a block
    > device by ftell()?
    >

    ftell() returns a long, which is 32 bits on a 32-bit system.
    For files or devices larger than 2GB, use ftello() and compile with
    #define _FILE_OFFSET_BITS 64
     
    Bill Marcum, Nov 20, 2007
    #2
    1. Advertising

  3. Seongsu Lee

    Gil Hamilton Guest

    Seongsu Lee <> wrote in
    news::

    > I want to get the size of a block device by ftell(). I found that I
    > can get
    > the size of a device by seek() and tell() in Python. But not in C.
    >
    > What is difference between them? How can I get the size of a block
    > device by ftell()?


    [snip]
    > fp = fopen(d, "r");
    > fseek(fp, 0L, SEEK_END);
    > l = ftell(fp);
    > fclose(fp);
    >
    > return l;


    > ---- # ./ftell_test
    > 362031
    > -1


    You need to check the return values. ftell is returning -1, which is an
    indicator that it failed. If you were to print out errno, you could then
    look up why it's failing. (Or, you could call perror(3) or strerror(3)
    to get it translated into text for you.)

    > -----------------------------------------------------------------------

    ----
    > # ./ftell_test.py
    > 362031
    > 120034091520


    Or, if you convert the python version's output to hexadecimal and count
    the digits, you might figure out the proximate cause of your program's
    failure.

    GH
     
    Gil Hamilton, Nov 20, 2007
    #3
    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. Leslaw Bieniasz

    How to speed up ftell()/fseek()

    Leslaw Bieniasz, Jun 6, 2005, in forum: C++
    Replies:
    7
    Views:
    4,840
    Lionel B
    Jun 8, 2005
  2. GiM

    ftell()

    GiM, Dec 15, 2003, in forum: C Programming
    Replies:
    3
    Views:
    511
    those who know me have no need of my name
    Jan 20, 2004
  3. morrell
    Replies:
    1
    Views:
    967
    roy axenov
    Oct 10, 2006
  4. Martin Johansen

    CR-NL, NL and ftell

    Martin Johansen, Feb 20, 2005, in forum: C Programming
    Replies:
    18
    Views:
    527
    Eric Sosman
    Feb 22, 2005
  5. xcm

    a question about ftell function

    xcm, May 30, 2005, in forum: C Programming
    Replies:
    4
    Views:
    339
    Scott J. McCaughrin
    Sep 23, 2005
Loading...

Share This Page