size of a file

  • Thread starter =?ISO-8859-1?Q?Andreas_M=FCller?=
  • Start date
?

=?ISO-8859-1?Q?Andreas_M=FCller?=

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.
 
C

cadull

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
 
J

John Carson

Andreas Müller said:
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.
 
?

=?ISO-8859-15?Q?Andreas_M=FCller?=

cadull said:
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?
 
?

=?ISO-8859-1?Q?Andreas_M=FCller?=

Andreas said:
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
 
C

Chris \( Val \)

| 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
 
L

Larry I Smith

Andreas said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top