sizeof

K

kate

salve.
per favore rispondete alla mia domanda:
Come faccio a ottenere le dimensioni di un file?(con c/c++)
risp presto
grazie
 
G

Gianni Mariani

kate said:
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();

?
 
J

Jack Klein

// 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.
 
A

Alex Vinokur

Rolf Magnus said:
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?
 
A

Andrea Laforgia

kate ha scritto:
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.
 
C

Cingar

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 <stdio.h>
FILE * file;
file = fopen("file.name", "rb");
fseek(file, 0, SEEK_END);
long length = ftell(file);
 
C

Cingar

Default User ha scritto:
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.
 
K

Kronos

Su it.comp.lang.c Default User said:

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
 
J

Jonathan Turkanis

[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
 
R

Rolf Magnus

Jonathan said:
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.
 
F

Fabiano

Alex Vinokur ha scritto:
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?
 
A

Angelo Paolitto

1. What is a reason for that?
2. Can we have any indication of that?

I don't know if it's the same for C++, for C language Standard
ISO/IEC 9899:1999 states

"A binary stream need not meaningfully support fseek calls with a
whence value of SEEK_END."

C:\>iao Angelo
 
A

Angelo Paolitto

What happens if we have no READ privilege on this particular files? Is
this the issue?

With this technique, you need to open the file. If you don't have READ
privilege you can't open file.

HTH

C:\>iao Angelo
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top