471,355 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,355 software developers and data experts.

size of block device by ftell()

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
Nov 20 '07 #1
2 3225
["Followup-To:" header set to comp.os.linux.development.system.]
On 2007-11-20, Seongsu Lee <se***@senux.comwrote:
>

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

Nov 20 '07 #2
Seongsu Lee <se***@senux.comwrote in
news:4e**********************************@d27g2000 prf.googlegroups.com:
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
Nov 20 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Alex | last post: by
17 posts views Thread by Arnold | last post: by
6 posts views Thread by Andrew Clark | last post: by
4 posts views Thread by Imran | last post: by
15 posts views Thread by uremae | last post: by
35 posts views Thread by munish.nr | last post: by
8 posts views Thread by =?windows-1256?B?5eTPx+bs?= | last post: by
20 posts views Thread by Ashit Vora | last post: by
18 posts views Thread by MisterE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.