473,394 Members | 1,810 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,394 software developers and data experts.

size of a file

Is there a way to determine the size of a file before/without reading
all chars?

for example:

ifstream test("test.txt");
//i need the file size here

while(test.read(buffer,size));
....
//i can get it here but i need to know how often the loop will be executed.
Jul 23 '05 #1
7 18221
I use:

test.seekg(0, std::ios::end);
size = test.tellg();
test.seekg(0);

but would prefer a better alternative should one exist.

Regards,
cadull
"Andreas Müller" <ma**************************@amueller.org> wrote in
message news:d5*********@newsserv.zdv.uni-tuebingen.de...
Is there a way to determine the size of a file before/without reading all
chars?

for example:

ifstream test("test.txt");
//i need the file size here

while(test.read(buffer,size));
...
//i can get it here but i need to know how often the loop will be
executed.


Jul 23 '05 #2
"Andreas Müller" <ma**************************@amueller.org> wrote in
message news:d5*********@newsserv.zdv.uni-tuebingen.de
Is there a way to determine the size of a file before/without reading
all chars?

for example:

ifstream test("test.txt");
//i need the file size here

while(test.read(buffer,size));
...
//i can get it here but i need to know how often the loop will be
executed.


Some files have headers giving this information, but of course a plain text
file doesn't.

The operating system you are using may provide an API to query file size,
e.g., there is GetFileSize on Windows.

The Boost filesystem library provides a file_size() function.

--
John Carson

Jul 23 '05 #3
cadull wrote:
I use:

test.seekg(0, std::ios::end);
size = test.tellg();
test.seekg(0);
I also tried this but shouldn't size contain the number of bytes?
I always get a confusing value, the filesize is 16,1KB but i always get
14416517 by printing out size. what's wrong?

but would prefer a better alternative should one exist.

Regards,
cadull


"Andreas Müller" <ma**************************@amueller.org> wrote in
message news:d5*********@newsserv.zdv.uni-tuebingen.de...
Is there a way to determine the size of a file before/without reading all
chars?

for example:

ifstream test("test.txt");
//i need the file size here

while(test.read(buffer,size));
...
//i can get it here but i need to know how often the loop will be
executed.






Jul 23 '05 #4
Andreas Müller wrote:
cadull wrote:
I use:

test.seekg(0, std::ios::end);
size = test.tellg();
test.seekg(0);



I also tried this but shouldn't size contain the number of bytes?
I always get a confusing value, the filesize is 16,1KB but i always get
14416517 by printing out size. what's wrong?

sorry my fault, i fixed it. thanks

but would prefer a better alternative should one exist.

Regards,
cadull
"Andreas Müller" <ma**************************@amueller.org> wrote in
message news:d5*********@newsserv.zdv.uni-tuebingen.de...
Is there a way to determine the size of a file before/without reading
all
chars?

for example:

ifstream test("test.txt");
//i need the file size here

while(test.read(buffer,size));
...
//i can get it here but i need to know how often the loop will be
executed.





Jul 23 '05 #5

"Andreas Müller" <ma**************************@amueller.org> wrote in message
news:d5*********@newsserv.zdv.uni-tuebingen.de...
| Is there a way to determine the size of a file before/without reading
| all chars?
|
| for example:
|
| ifstream test("test.txt");
| //i need the file size here
|
| while(test.read(buffer,size));
| ...
| //i can get it here but i need to know how often the loop will be executed.

Store it first, and note that it is opened in
binary mode to obtain an accurate positioning:

# include <iostream>
# include <fstream>
# include <ios>
# include <exception>

typedef std::ifstream::pos_type pos_type;
pos_type get_file_size( const std::string& filename )
{
std::ifstream InFile( filename.c_str(),
std::ios_base::binary | std::ios_base::ate );

if( !InFile )
throw std::runtime_error( "ERROR: Could not obtain file size "
"for [" + filename + "]\n" );

return InFile.tellg();
}

int main()
{
pos_type pos = get_file_size( "X.txt" );

// ...

return 0;
}

Cheers,
Chris Val
Jul 23 '05 #6
Andreas Müller wrote:
Is there a way to determine the size of a file before/without reading
all chars?

for example:

ifstream test("test.txt");
//i need the file size here

while(test.read(buffer,size));
...
//i can get it here but i need to know how often the loop will be executed.


Many systems provide the stat() and fstat() functions
which return a struct of info on the file; the struct
includes the file's size.

Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #7

"Andreas Müller" <ma**************************@amueller.org> wrote in message news:d5*********@newsserv.zdv.uni-tuebingen.de...
Is there a way to determine the size of a file before/without reading
all chars?

for example:

ifstream test("test.txt");
//i need the file size here

while(test.read(buffer,size));
...
//i can get it here but i need to know how often the loop will be executed.


Samples can be seen at
http://groups-beta.google.com/group/...464ce8b75f8417
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 23 '05 #8

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

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...
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...
8
by: Dave | last post by:
I am serialising an object to a memory mapped file (using the CreateFileMapping and MapViewOfFile p/invoke calls). These need to know the maximum size of the "file". I can put in a "good guess" ie...
8
by: Ron | last post by:
Hi all, How do I determine the size of the tables I'm using? I looked under properties and it's not there. The book I just browsed said table is limited to 1GB. How do I find out what size my...
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...
12
by: Phil Z. | last post by:
After migrating an ASP.NET 1.1 application to 2.0 we were getting "Cannot access a closed file" errors when uploading. I found a number of post on the subject and have since moved from using an...
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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
0
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...

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.