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

Calculating File Size in C

seeminsuleri
hello there,
Can anyone plz guide me how to calculate the sise of a string present in an opened file.
we already have the file descriptor, inputFile. but when i try to do

begin=inputFile.tellg();
inputFile.seekg(0,ios::end);
end=inputFile.tellg();
size=end-begin;

it doesnt work in C. It does work in C++ though.
Can u plz give me some C alternative for the above.
Nov 3 '06 #1
5 30940
macklin01
145 100+
Well, a primative but sure-fire way to get the job done is to read bytes (or some other data size) until you reach the end, and keep track. All the traditional FILE operations still go in C, right? e.g.,

int count = 0;
FILE* fp;
fp = fopen( "file.txt", "r" );

while( !feof( fp ) && fp )
{
unsigned char c;
fread( &c , 1,1, fp );
count++;
}
if( fp )
{ fclose( fp ); }


Again, not elegant, but robust. :) -- Paul
Nov 3 '06 #2
thanks for your help Paul,
I was looking at a code

fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);

I didnt know these functions exist in C.
I think they would do the job too.

Well, a primative but sure-fire way to get the job done is to read bytes (or some other data size) until you reach the end, and keep track. All the traditional FILE operations still go in C, right? e.g.,

int count = 0;
FILE* fp;
fp = fopen( "file.txt", "r" );

while( !feof( fp ) && fp )
{
unsigned char c;
fread( &c , 1,1, fp );
count++;
}
if( fp )
{ fclose( fp ); }


Again, not elegant, but robust. :) -- Paul
Nov 4 '06 #3
macklin01
145 100+
thanks for your help Paul,
I was looking at a code

fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);

I didnt know these functions exist in C.
I think they would do the job too.
Ah, that's much more elegant, and likely more efficient, too. Nice work. :) -- Paul
Nov 4 '06 #4
there is an another function
filelength(int fd); which returns the length of the file associated with the handle fd.
U need to include io.h b4 using this function.
Nov 4 '06 #5
About the fseek - ftell method, I should add that I saw this on http://www.cplusplus.com/reference/clibrary/cstdio/ftell.html

"For text streams, the value is not guaranteed to be the exact number of bytes from the beginning of the file, but the value returned can still be used to restore the position indicator to this position using fseek."

Thus, the value is not guaranteed. So you might want to consider a different method for calculating length of string files.
May 22 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

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...
5
by: sugaray | last post by:
Hi, my problem with calculating the size of an array is when I pass an array as a parameter to a function which perform the calculation, the result never comes right, like below: int...
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...
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.
5
by: Claudio Grondi | last post by:
I have just started to play around with the bsddb3 module interfacing the Berkeley Database. Beside the intended database file databaseFile.bdb I see in same directory also the __db.001...
11
by: Parrot | last post by:
Is there any routine I can call to reduce the size of an image file after uploading a file from a client. I am looking to reduce file sizes programmatically using C# in my web page after uploading....
5
by: Jefferis NoSpamme | last post by:
Hi all, I'm trying to limit the file size of an image submission and I keep running into various problems. I've got most of it working, but I'm stumped and I have a basic question as to WHY this...
4
by: Doug | last post by:
Hi, It looks like the only way to get a size of a file within csharp is to use FileInfo and the Length property. However that only returns the number of bytes in the file which is translating...
1
by: chrisj | last post by:
I'm using freeASPupload and got some assistance integrating to a Member script. It works successfully. In this modified version there are two groups that use this upload script. Members of one...
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
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.