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. | | | | 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] | | | | 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 | | | | 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] | | | | 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] | | | | 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 | | | | 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. | | | | 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 |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|