 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
kate Guest
|
Posted: Sat Feb 05, 2005 8:18 pm Post subject: sizeof |
|
|
salve.
per favore rispondete alla mia domanda:
Come faccio a ottenere le dimensioni di un file?(con c/c++)
risp presto
grazie
|
|
| Back to top |
|
 |
Gianni Mariani Guest
|
Posted: Sat Feb 05, 2005 10:22 pm Post subject: Re: sizeof |
|
|
kate wrote:
| Quote: | salve.
per favore rispondete alla mia domanda:
Come faccio a ottenere le dimensioni di un file?(con c/c++)
|
// get length of file:
ifstream file;
file.open( "file.nome", ios::binary );
file.seekg( 0, ios::end );
int length = file.tellg();
?
|
|
| Back to top |
|
 |
Jack Klein Guest
|
|
| Back to top |
|
 |
Alex Vinokur Guest
|
Posted: Sun Feb 06, 2005 9:54 am Post subject: Re: sizeof |
|
|
"Jack Klein" <jackklein (AT) spamcop (DOT) net> wrote
| Quote: | On Sat, 05 Feb 2005 14:22:52 -0800, Gianni Mariani
[email]gi2nospam (AT) mariani (DOT) ws[/email]> wrote in comp.lang.c++:
kate wrote:
salve.
per favore rispondete alla mia domanda:
Come faccio a ottenere le dimensioni di un file?(con c/c++)
// get length of file:
ifstream file;
file.open( "file.nome", ios::binary );
file.seekg( 0, ios::end );
int length = file.tellg();
Not guaranteed to be correct, although it works on most platforms.
|
What exactly can be incorrect?
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Sun Feb 06, 2005 10:59 am Post subject: Re: sizeof |
|
|
Alex Vinokur wrote:
| Quote: |
"Jack Klein" <jackklein (AT) spamcop (DOT) net> wrote in message
news:0gpb01hntrr85iefk0cqe6b945ecpsjflq (AT) 4ax (DOT) com...
On Sat, 05 Feb 2005 14:22:52 -0800, Gianni Mariani
[email]gi2nospam (AT) mariani (DOT) ws[/email]> wrote in comp.lang.c++:
kate wrote:
salve.
per favore rispondete alla mia domanda:
Come faccio a ottenere le dimensioni di un file?(con c/c++)
// get length of file:
ifstream file;
file.open( "file.nome", ios::binary );
file.seekg( 0, ios::end );
int length = file.tellg();
Not guaranteed to be correct, although it works on most platforms.
What exactly can be incorrect?
|
The result of trying to seek to the end of a binary file is undefined.
|
|
| Back to top |
|
 |
Alex Vinokur Guest
|
Posted: Sun Feb 06, 2005 11:14 am Post subject: Re: sizeof |
|
|
"Rolf Magnus" <ramagnus (AT) t-online (DOT) de> wrote
| Quote: | Alex Vinokur wrote:
"Jack Klein" <jackklein (AT) spamcop (DOT) net> wrote in message
news:0gpb01hntrr85iefk0cqe6b945ecpsjflq (AT) 4ax (DOT) com...
On Sat, 05 Feb 2005 14:22:52 -0800, Gianni Mariani
[email]gi2nospam (AT) mariani (DOT) ws[/email]> wrote in comp.lang.c++:
kate wrote:
salve.
per favore rispondete alla mia domanda:
Come faccio a ottenere le dimensioni di un file?(con c/c++)
// get length of file:
ifstream file;
file.open( "file.nome", ios::binary );
file.seekg( 0, ios::end );
int length = file.tellg();
Not guaranteed to be correct, although it works on most platforms.
What exactly can be incorrect?
The result of trying to seek to the end of a binary file is undefined.
|
1. What is a reason for that?
2. Can we have any indication of that?
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
|
|
| Back to top |
|
 |
Andrea Laforgia Guest
|
Posted: Sun Feb 06, 2005 3:58 pm Post subject: Re: sizeof |
|
|
kate ha scritto:
| Quote: | per favore rispondete alla mia domanda:
|
Per cortesia, non fare uso del cross-post.
Posta la tua domanda in it.comp.lang.c++ (in italiano), oppure in
comp.lang.c++ (in inglese), se vuoi una risposta relativa al C++, oppure
in it.comp.lang.c (in italiano) o in comp.lang.c (in inglese) se vuoi una
risposta relativa al C.
--
questo articolo e` stato inviato via web dal servizio gratuito
http://www.newsland.it/news segnala gli abusi ad [email]abuse (AT) newsland (DOT) it[/email]
|
|
| Back to top |
|
 |
Cingar Guest
|
Posted: Mon Feb 07, 2005 10:05 am Post subject: Re: sizeof |
|
|
Gianni Mariani ha scritto:
| Quote: | // get length of file:
ifstream file;
file.open( "file.nome", ios::binary );
file.seekg( 0, ios::end );
int length = file.tellg();
|
&, in C:
#include <stdio.h>
FILE * file;
file = fopen("file.name", "rb");
fseek(file, 0, SEEK_END);
long length = ftell(file);
--
Cingar
--
questo articolo e` stato inviato via web dal servizio gratuito
http://www.newsland.it/news segnala gli abusi ad [email]abuse (AT) newsland (DOT) it[/email]
|
|
| Back to top |
|
 |
Default User Guest
|
Posted: Mon Feb 07, 2005 5:39 pm Post subject: Re: sizeof |
|
|
Cingar wrote:
| Quote: | Gianni Mariani ha scritto:
// get length of file:
ifstream file;
file.open( "file.nome", ios::binary );
file.seekg( 0, ios::end );
int length = file.tellg();
&, in C:
#include
FILE * file;
file = fopen("file.name", "rb");
fseek(file, 0, SEEK_END);
long length = ftell(file);
|
Also not guaranteed to work.
http://www.eskimo.com/~scs/C-faq/q19.12.html
Brian
|
|
| Back to top |
|
 |
Cingar Guest
|
Posted: Mon Feb 07, 2005 6:17 pm Post subject: Re: sizeof |
|
|
Default User ha scritto:
| Quote: | Cingar wrote:
Gianni Mariani ha scritto:
// get length of file:
ifstream file;
file.open( "file.nome", ios::binary );
file.seekg( 0, ios::end );
int length = file.tellg();
&, in C:
#include
FILE * file;
file = fopen("file.name", "rb");
fseek(file, 0, SEEK_END);
long length = ftell(file);
Also not guaranteed to work.
|
Of course. As the two code fragments are just different syntactic flavors
for exactly the same stuff, either both are guaranteed to work, or both
aren't. :-)
However, I think this is the best approximation you can have with standard
C and C++ libraries.
IMHO, a solid program should use more reliable non-portable API's, and
leave this approach as the default portable implementation. I.e., the
fseek/ftell implementation could be a pretty good #else in a cascade of
platform-specific #elif's.
--
Cingar
--
questo articolo e` stato inviato via web dal servizio gratuito
http://www.newsland.it/news segnala gli abusi ad [email]abuse (AT) newsland (DOT) it[/email]
|
|
| Back to top |
|
 |
Kronos Guest
|
Posted: Mon Feb 07, 2005 11:55 pm Post subject: Re: sizeof |
|
|
Su it.comp.lang.c Default User <defaultuserbr (AT) yahoo (DOT) com> ha scritto:
IMHO that FAQ entry is not very clear.
"ftell is not guaranteed to return a byte count except for binary files."
It seems that I _can_ fopen() the file as "b", fseek() to the end and then use
ftell().
That sentence should be something like that:
"ftell is not guaranteed to return a byte count except for binary files,
but in this case you can't fseek to the end of the file since fseek
needn't support SEEK_END on binary streams."
Luca
--
Home: http://kronoz.cjb.net
The trouble with computers is that they do what you tell them,
not what you want.
D. Cohen
|
|
| Back to top |
|
 |
Alex Vinokur Guest
|
Posted: Tue Feb 08, 2005 5:39 am Post subject: Getting file size using ftell()/tellg() (Was: sizeof) |
|
|
"Default User" <defaultuserbr (AT) yahoo (DOT) com> wrote
| Quote: | Cingar wrote:
Gianni Mariani ha scritto:
// get length of file:
ifstream file;
file.open( "file.nome", ios::binary );
file.seekg( 0, ios::end );
int length = file.tellg();
&, in C:
#include
FILE * file;
file = fopen("file.name", "rb");
fseek(file, 0, SEEK_END);
long length = ftell(file);
Also not guaranteed to work.
http://www.eskimo.com/~scs/C-faq/q19.12.html
[snip] |
1. Did anybody really come across that problem (ftell() or/and tellg() don't return what is expected)?
2. ftell() and tellg() are standard function and method. Why/how can their behavior be undefined if we are correctly using them?
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
|
|
| Back to top |
|
 |
Jonathan Turkanis Guest
|
Posted: Tue Feb 08, 2005 5:49 am Post subject: Re: Getting file size using ftell()/tellg() (Was: sizeof) |
|
|
| Quote: | Also not guaranteed to work.
http://www.eskimo.com/~scs/C-faq/q19.12.html
[snip]
1. Did anybody really come across that problem (ftell() or/and
tellg() don't return what is expected)?
2. ftell() and tellg() are standard function and method. Why/how can
their behavior be undefined if we are correctly using them?
|
P.J. Plauger has a good discussion of this in his book on the std C library.
Basically it was one of the compromised that had to be made if the standard was
to apply to non-unix systems. Implementations are allowed to add an arbitrary
number of null characters at the end of a binary file, because on some systems,
at some time, no stronger guarantee could be made.
Seeking to the end of a file and querying the offset does not result in
undefined behavior, however. It's just not guaranteed to give you the result you
want.
Jonathan
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Tue Feb 08, 2005 11:10 am Post subject: Re: Getting file size using ftell()/tellg() (Was: sizeof) |
|
|
Jonathan Turkanis wrote:
| Quote: | Seeking to the end of a file and querying the offset does not result in
undefined behavior, however.
|
Yes, it does. At least with fseek/ftell. From the C standard:
Setting the file position indicator to end-of-file, as with fseek(file, 0,
SEEK_END), has undefined behavior for a binary stream (because of possible
trailing null characters) or for any stream with state-dependent encoding
that does not assuredly end in the initial shift state.
|
|
| Back to top |
|
 |
Fabiano Guest
|
Posted: Mon Oct 24, 2005 8:38 am Post subject: Re: sizeof |
|
|
Alex Vinokur ha scritto:
| Quote: | "Rolf Magnus" <ramagnus (AT) t-online (DOT) de> wrote
Alex Vinokur wrote:
"Jack Klein" <jackklein (AT) spamcop (DOT) net> wrote in message
news:0gpb01hntrr85iefk0cqe6b945ecpsjflq (AT) 4ax (DOT) com...
On Sat, 05 Feb 2005 14:22:52 -0800, Gianni Mariani
[email]gi2nospam (AT) mariani (DOT) ws[/email]> wrote in comp.lang.c++:
kate wrote:
salve.
per favore rispondete alla mia domanda:
Come faccio a ottenere le dimensioni di un file?(con c/c++)
// get length of file:
ifstream file;
file.open( "file.nome", ios::binary );
file.seekg( 0, ios::end );
int length = file.tellg();
Not guaranteed to be correct, although it works on most platforms.
What exactly can be incorrect?
The result of trying to seek to the end of a binary file is undefined.
1. What is a reason for that?
2. Can we have any indication of that?
What happens if we have no READ privilege on this particular files? Is |
this the issue?
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|