S
Seongsu Lee
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")
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")