473,320 Members | 2,041 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,320 software developers and data experts.

How to get correct size of a text file in standard c ?

How does one get the correct length of a text file in standard c ?
I know ftell() does not work due to the carriage return-linefeed
translation.

Thanks for any help !

Jan 7 '07 #1
4 2235
pninja005 said:
How does one get the correct length of a text file in standard c ?
I know ftell() does not work due to the carriage return-linefeed
translation.
There is no portable method, either for text files or for binary files,
other than reading every byte and counting as you go.

Your OS probably has an API call for this. If a non-ISO solution is
acceptable, check with a platform-specific newsgroup.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 7 '07 #2
pninja005 wrote:
How does one get the correct length of a text file in standard c ?
I know ftell() does not work due to the carriage return-linefeed
translation.

Thanks for any help !
It's not really possible in standard C. The OS is free to store any
amount of extra bytes along with a file for it's internal purposes. And
as you note some characters may get translated by your environment.
Nearly all modern systems provide system calls to accomplish this.
Using them may be the best strategy. If you still want to do it in
standard C, as Richard notes, you need to read in the whole file and
keep count. This count is not likely to correspond exactly with the on
disk size of the file.

Jan 7 '07 #3
pninja005 wrote:
>
How does one get the correct length of a text file in standard c ?
I know ftell() does not work due to the carriage return-linefeed
translation.
First you define "correct length". I can think of systems that
store text in 128 byte chunks, in reversed order by line, for good
reasons. No \n or \r in the lot.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Jan 7 '07 #4
On Sun, 7 Jan 2007 09:48:21 -0600, Richard Heathfield wrote
(in article <Gv*********************@bt.com>):
pninja005 said:
>How does one get the correct length of a text file in standard c ?
I know ftell() does not work due to the carriage return-linefeed
translation.

There is no portable method, either for text files or for binary files,
other than reading every byte and counting as you go.
Even that isn't reliable, as files with so-called "resource forks" as
in Mac OS file systems can be larger than reported by such a method.
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Jan 10 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

49
by: Sam | last post by:
Hi all, Is there a function in the standard library that can get the size of a file? Thank you very much. Sam.
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
14
by: googler | last post by:
Is there any C library function that returns the size of a given file? Otherwise, is there a way in which file size can be determined in a C program? I need to get this for both Linux and Windows...
0
by: jimmyfingers | last post by:
I've just tried the following code for decompressing a .zip file, but get the following error message: "The magic number in GZip header is not correct. Make sure you are passing in a GZip stream."...
24
by: temper3243 | last post by:
Hi, Many people have used this code in my project. It works because b is using the extra memory for other 4 variables.I access obj max. well here are a few questions 1) Where does it fail. I...
4
by: alisbub | last post by:
1. Write an interactive program that asks the user to input the length and width of a rectangular lawn. The dimensions should be in yards. Your program should compute the area of the lawn in square...
20
by: Peter Olcott | last post by:
Is there any standard C++ way to determine the size of a file before it is read?
8
by: Benniit | last post by:
Am using Vb.NET 2008 and SQL 2008. I have a problem, when the execution reaches daAdapter.Update(dtTable) then I receive this error ""String or binary data would be truncated. The statement has been...
70
mideastgirl
by: mideastgirl | last post by:
I have recently been working on a website for an honors association, and have a lot of difficulty but have found help from those on this site. I would like to see if I can get some more help on a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.