Connecting Tech Pros Worldwide Forums | Help | Site Map

size of a file

Andreas Müller
Guest
 
Posts: n/a
#1: Jul 23 '05
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.

cadull
Guest
 
Posts: n/a
#2: Jul 23 '05

re: size of a file


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" <mail_REMOVE_THIS_SPAMFILTER_@amueller.org> wrote in
message news:d5fbt6$nb$1@newsserv.zdv.uni-tuebingen.de...[color=blue]
> 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.[/color]



John Carson
Guest
 
Posts: n/a
#3: Jul 23 '05

re: size of a file


"Andreas Müller" <mail_REMOVE_THIS_SPAMFILTER_@amueller.org> wrote in
message news:d5fbt6$nb$1@newsserv.zdv.uni-tuebingen.de[color=blue]
> 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.[/color]

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

Andreas Müller
Guest
 
Posts: n/a
#4: Jul 23 '05

re: size of a file


cadull wrote:[color=blue]
> I use:
>
> test.seekg(0, std::ios::end);
> size = test.tellg();
> test.seekg(0);[/color]

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?
[color=blue]
>
> but would prefer a better alternative should one exist.
>
> Regards,
> cadull
>
>
> "Andreas Müller" <mail_REMOVE_THIS_SPAMFILTER_@amueller.org> wrote in
> message news:d5fbt6$nb$1@newsserv.zdv.uni-tuebingen.de...
> [color=green]
>>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.[/color]
>
>
>
> [/color]

Andreas Müller
Guest
 
Posts: n/a
#5: Jul 23 '05

re: size of a file


Andreas Müller wrote:[color=blue]
> cadull wrote:
> [color=green]
>> I use:
>>
>> test.seekg(0, std::ios::end);
>> size = test.tellg();
>> test.seekg(0);[/color]
>
>
> 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?
> [/color]
sorry my fault, i fixed it. thanks
[color=blue][color=green]
>>
>> but would prefer a better alternative should one exist.
>>
>> Regards,
>> cadull
>>
>>
>> "Andreas Müller" <mail_REMOVE_THIS_SPAMFILTER_@amueller.org> wrote in
>> message news:d5fbt6$nb$1@newsserv.zdv.uni-tuebingen.de...
>>[color=darkred]
>>> 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.[/color]
>>
>>
>>
>>
>>[/color]
> [/color]

Chris \( Val \)
Guest
 
Posts: n/a
#6: Jul 23 '05

re: size of a file



"Andreas Müller" <mail_REMOVE_THIS_SPAMFILTER_@amueller.org> wrote in message
news:d5fbt6$nb$1@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


Larry I Smith
Guest
 
Posts: n/a
#7: Jul 23 '05

re: size of a file


Andreas Müller wrote:[color=blue]
> 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.[/color]

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.
Alex Vinokur
Guest
 
Posts: n/a
#8: Jul 23 '05

re: size of a file



"Andreas Müller" <mail_REMOVE_THIS_SPAMFILTER_@amueller.org> wrote in message news:d5fbt6$nb$1@newsserv.zdv.uni-tuebingen.de...[color=blue]
> 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.[/color]

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



Closed Thread