473,387 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 3361
["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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.