size of block device by ftell()

Gil Hamilton gil_hamilton at hotmail.com
Tue Nov 20 10:26:32 EST 2007


Seongsu Lee <senux at senux.com> wrote in
news:4e573411-2b2a-444c-994e-04aee61309b5 at d27g2000prf.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



More information about the Python-list mailing list