473,769 Members | 5,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(cons t 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(cons t 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 3399
["Followup-To:" header set to comp.os.linux.d evelopment.syst em.]
On 2007-11-20, Seongsu Lee <se***@senux.co mwrote:
>

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_BI TS 64

Nov 20 '07 #2
Seongsu Lee <se***@senux.co mwrote in
news:4e******** *************** ***********@d27 g2000prf.google groups.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
12462
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
16005
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 obtain it? Thanks.
6
2482
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 don't remember any good reasons (not that I haven't read them, I'm sure, but it's more of a failure to hold them in my brain). Here is an attempt that I was commissioned to write, and I'd appreciate any comments and/or criticism (specific or...
4
1878
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
15469
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
2689
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
8150
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 big size like 2-GB how can i break this limit in C or even C++
20
2497
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, default position is 1st byte. Now I use fseek() and move it till the send of the file. Than I use ftell() to get the current position in the file.
18
2833
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
9586
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10043
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.