472,992 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,992 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 3334
["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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Alex | last post by:
Hi all, can someone please show me how to get the size of a file with ANSI C? both in text mode and binary mode. thanks in advance.
17
by: Arnold | last post by:
Is using fseek and ftell a reliable method of getting the file size on a binary file? I thought I remember reading somewhere it wasn't... If not what would be the "right" and portable method to...
6
by: Andrew Clark | last post by:
*** post for FREE via your newsreader at post.newsfeed.com *** Hello all, I recall several threads over the years about how reading file size cannot be done consistantly or portably, but I...
4
by: Imran | last post by:
Hello All Am trying to read a huge txt file and write to a file back. pls see the code int main() { char* pData;
15
by: uremae | last post by:
I tried to open some large files in my computer. (ram 512MB) 1. is there limitation of file size FOPEN? 2. if I have a file that is larger than the maximum size, how can I open the file?
35
by: munish.nr | last post by:
Hi All, I want to know the size of file (txt,img or any other file). i knoe only file name. how i can acheive this. does anybody is having idea about that. plz help. rgrds, Munish Nayyar
8
by: =?windows-1256?B?5eTPx+bs?= | last post by:
What can i do to open , write and seek in file with size 2 GB or more I know that you r limited with int type size which is 32-bit but how programs like gzip , zip read and write from files with...
20
by: Ashit Vora | last post by:
Hi, I 'm new to C programming and 'm stuck somewhere. I want to find the size of a file. I couldn't find a proper way of doing it. What I was planning to do is... Open the requested file,...
18
by: MisterE | last post by:
I hear that this isn't always valid: FILE *in; long size; in = fopen("foo.bar","rb"); fseek(in,0,SEEK_END); size = ftell(in); fseek(in,0,SEEK_SET); then fread size many bytes into memory.
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.