Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > size of block device by ftell()

Reply
Thread Tools

size of block device by ftell()

 
 
Seongsu Lee
Guest
Posts: n/a
 
      11-20-2007
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
 
Reply With Quote
 
 
 
 
Bill Marcum
Guest
Posts: n/a
 
      11-20-2007
["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

 
Reply With Quote
 
 
 
 
Gil Hamilton
Guest
Posts: n/a
 
      11-20-2007
Seongsu Lee <> wrote in
news:4e573411-2b2a-444c-994e-:

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Preferred Size, Minimum Size, Size Jason Cavett Java 5 05-25-2008 08:32 AM
mega pixels, file size, image size, and print size - Adobe Evangelists Frank ess Digital Photography 0 11-14-2006 05:08 PM
Fo:Block can you check to see if a block contains any text by using the block id? morrell XML 1 10-10-2006 07:18 PM
802.11g router / 1 x 802.11b device / 1 x 802.11g device Oli Wireless Networking 3 09-27-2004 11:56 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57