Connecting Tech Pros Worldwide Help | Site Map

sizeof

kate
Guest
 
Posts: n/a
#1: Jul 23 '05
salve.
per favore rispondete alla mia domanda:
Come faccio a ottenere le dimensioni di un file?(con c/c++)
risp presto
grazie






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

re: sizeof


kate wrote:[color=blue]
> salve.
> per favore rispondete alla mia domanda:
> Come faccio a ottenere le dimensioni di un file?(con c/c++)[/color]

// get length of file:
ifstream file;
file.open( "file.nome", ios::binary );
file.seekg( 0, ios::end );
int length = file.tellg();

?
Jack Klein
Guest
 
Posts: n/a
#3: Jul 23 '05

re: sizeof


On Sat, 05 Feb 2005 14:22:52 -0800, Gianni Mariani
<gi2nospam@mariani.ws> wrote in comp.lang.c++:
[color=blue]
> kate wrote:[color=green]
> > salve.
> > per favore rispondete alla mia domanda:
> > Come faccio a ottenere le dimensioni di un file?(con c/c++)[/color]
>
> // get length of file:
> ifstream file;
> file.open( "file.nome", ios::binary );
> file.seekg( 0, ios::end );
> int length = file.tellg();[/color]

Not guaranteed to be correct, although it works on most platforms.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Alex Vinokur
Guest
 
Posts: n/a
#4: Jul 23 '05

re: sizeof



"Jack Klein" <jackklein@spamcop.net> wrote in message news:0gpb01hntrr85iefk0cqe6b945ecpsjflq@4ax.com...[color=blue]
> On Sat, 05 Feb 2005 14:22:52 -0800, Gianni Mariani
> <gi2nospam@mariani.ws> wrote in comp.lang.c++:
>[color=green]
> > kate wrote:[color=darkred]
> > > salve.
> > > per favore rispondete alla mia domanda:
> > > Come faccio a ottenere le dimensioni di un file?(con c/c++)[/color]
> >
> > // get length of file:
> > ifstream file;
> > file.open( "file.nome", ios::binary );
> > file.seekg( 0, ios::end );
> > int length = file.tellg();[/color]
>
> Not guaranteed to be correct, although it works on most platforms.
>[/color]

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



Rolf Magnus
Guest
 
Posts: n/a
#5: Jul 23 '05

re: sizeof


Alex Vinokur wrote:
[color=blue]
>
> "Jack Klein" <jackklein@spamcop.net> wrote in message
> news:0gpb01hntrr85iefk0cqe6b945ecpsjflq@4ax.com...[color=green]
>> On Sat, 05 Feb 2005 14:22:52 -0800, Gianni Mariani
>> <gi2nospam@mariani.ws> wrote in comp.lang.c++:
>>[color=darkred]
>> > 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();[/color]
>>
>> Not guaranteed to be correct, although it works on most platforms.
>>[/color]
>
> What exactly can be incorrect?[/color]

The result of trying to seek to the end of a binary file is undefined.

Alex Vinokur
Guest
 
Posts: n/a
#6: Jul 23 '05

re: sizeof



"Rolf Magnus" <ramagnus@t-online.de> wrote in message news:cu4srt$dpk$00$2@news.t-online.com...[color=blue]
> Alex Vinokur wrote:
>[color=green]
> >
> > "Jack Klein" <jackklein@spamcop.net> wrote in message
> > news:0gpb01hntrr85iefk0cqe6b945ecpsjflq@4ax.com...[color=darkred]
> >> On Sat, 05 Feb 2005 14:22:52 -0800, Gianni Mariani
> >> <gi2nospam@mariani.ws> 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.
> >>[/color]
> >
> > What exactly can be incorrect?[/color]
>
> The result of trying to seek to the end of a binary file is undefined.
>[/color]

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



Andrea Laforgia
Guest
 
Posts: n/a
#7: Jul 23 '05

re: sizeof


kate ha scritto:
[color=blue]
> per favore rispondete alla mia domanda:[/color]

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 abuse@newsland.it


Cingar
Guest
 
Posts: n/a
#8: Jul 23 '05

re: sizeof


Gianni Mariani ha scritto:[color=blue]
> // get length of file:
> ifstream file;
> file.open( "file.nome", ios::binary );
> file.seekg( 0, ios::end );
> int length = file.tellg();[/color]

&, 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 abuse@newsland.it


Default User
Guest
 
Posts: n/a
#9: Jul 23 '05

re: sizeof


Cingar wrote:[color=blue]
> Gianni Mariani ha scritto:[color=green]
> > // get length of file:
> > ifstream file;
> > file.open( "file.nome", ios::binary );
> > file.seekg( 0, ios::end );
> > int length = file.tellg();[/color]
>
> &, in C:
>
> #include <stdio.h>
> FILE * file;
> file = fopen("file.name", "rb");
> fseek(file, 0, SEEK_END);
> long length = ftell(file);[/color]


Also not guaranteed to work.

http://www.eskimo.com/~scs/C-faq/q19.12.html



Brian

Cingar
Guest
 
Posts: n/a
#10: Jul 23 '05

re: sizeof


Default User ha scritto:[color=blue]
> Cingar wrote:[color=green]
> > Gianni Mariani ha scritto:[color=darkred]
> > > // get length of file:
> > > ifstream file;
> > > file.open( "file.nome", ios::binary );
> > > file.seekg( 0, ios::end );
> > > int length = file.tellg();[/color]
> >
> > &, in C:
> >
> > #include <stdio.h>
> > FILE * file;
> > file = fopen("file.name", "rb");
> > fseek(file, 0, SEEK_END);
> > long length = ftell(file);[/color]
>
> Also not guaranteed to work.[/color]

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 abuse@newsland.it


Kronos
Guest
 
Posts: n/a
#11: Jul 23 '05

re: sizeof


Su it.comp.lang.c Default User <defaultuserbr@yahoo.com> ha scritto:[color=blue][color=green]
>> #include <stdio.h>
>> FILE * file;
>> file = fopen("file.name", "rb");
>> fseek(file, 0, SEEK_END);
>> long length = ftell(file);[/color]
>
>
> Also not guaranteed to work.
>
> http://www.eskimo.com/~scs/C-faq/q19.12.html[/color]

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

re: sizeof



"Default User" <defaultuserbr@yahoo.com> wrote in message news:1107797957.227237.23420@o13g2000cwo.googlegro ups.com...[color=blue]
> Cingar wrote:[color=green]
> > Gianni Mariani ha scritto:[color=darkred]
> > > // get length of file:
> > > ifstream file;
> > > file.open( "file.nome", ios::binary );
> > > file.seekg( 0, ios::end );
> > > int length = file.tellg();[/color]
> >
> > &, in C:
> >
> > #include <stdio.h>
> > FILE * file;
> > file = fopen("file.name", "rb");
> > fseek(file, 0, SEEK_END);
> > long length = ftell(file);[/color]
>
>
> Also not guaranteed to work.
>
> http://www.eskimo.com/~scs/C-faq/q19.12.html
>[/color]
[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





Jonathan Turkanis
Guest
 
Posts: n/a
#13: Jul 23 '05

re: sizeof


[color=blue][color=green]
>> Also not guaranteed to work.
>>
>> http://www.eskimo.com/~scs/C-faq/q19.12.html
>>[/color]
> [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?[/color]

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


Rolf Magnus
Guest
 
Posts: n/a
#14: Jul 23 '05

re: sizeof


Jonathan Turkanis wrote:
[color=blue]
> Seeking to the end of a file and querying the offset does not result in
> undefined behavior, however.[/color]

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.

Fabiano
Guest
 
Posts: n/a
#15: Oct 24 '05

re: sizeof


Alex Vinokur ha scritto:[color=blue]
> "Rolf Magnus" <ramagnus@t-online.de> wrote in message news:cu4srt$dpk$00$2@news.t-online.com...
>[color=green]
>>Alex Vinokur wrote:
>>
>>[color=darkred]
>>>"Jack Klein" <jackklein@spamcop.net> wrote in message
>>>news:0gpb01hntrr85iefk0cqe6b945ecpsjflq@4ax.com ...
>>>
>>>>On Sat, 05 Feb 2005 14:22:52 -0800, Gianni Mariani
>>>><gi2nospam@mariani.ws> 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?[/color]
>>
>>The result of trying to seek to the end of a binary file is undefined.
>>[/color]
>
>
> 1. What is a reason for that?
> 2. Can we have any indication of that?
>
>[/color]
What happens if we have no READ privilege on this particular files? Is
this the issue?
Angelo Paolitto
Guest
 
Posts: n/a
#16: Oct 24 '05

re: sizeof


"Alex Vinokur" <alexvn@big-foot.com> wrote in message
news:36mch4F5368c0U1@individual.net
[color=blue][color=green]
> >
> > The result of trying to seek to the end of a binary file is undefined.
> >[/color]
>
> 1. What is a reason for that?
> 2. Can we have any indication of that?[/color]

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



--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
Angelo Paolitto
Guest
 
Posts: n/a
#17: Oct 24 '05

re: sizeof


"Fabiano" <fab@fake.net> wrote in message
news:la6ijd.9d6.ln@news.tana.net
[color=blue][color=green]
> >
> >[/color]
> What happens if we have no READ privilege on this particular files? Is
> this the issue?[/color]

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


--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
Closed Thread