Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

file length ?

Posted on 1999-06-29
10
Medium Priority
?
205 Views
Last Modified: 2010-04-15
is this the 'right' way to get the length of a file ?

long GetSize( FILE * pFile )
{
       long nPos;
       long nPosOld;
       
       // get old file pointer postion
       nPosOld = ftell( pFile );
       fseek( pFile, 0L, SEEK_END );
       // get end position
       nPos = ftell( pFile );
       // restore old file pointer postion
       fseek( pFile, nPosOld, SEEK_SET );
     
       return nPos;
}
 
0
Comment
Question by:nil092297
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 10

Expert Comment

by:rbr
ID: 1263052
yes this is one way to get the file length. Another way is to use stat or fstat.

struct stat stat_buf;

stat ("filename",&stat_buf);
printf ("%d",stats_buf.st_size);


0
 

Author Comment

by:nil092297
ID: 1263053
Hi rbr,
sorry for rejecting your answer, but stat and fstat are not ANSI C ...?!
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1263054
I'am sorry but
    fseek( pFile, 0L, SEEK_END );
will position the file pointer at the beginning of the file. So the answer to your Q is
  No!
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 7

Expert Comment

by:KangaRoo
ID: 1263055
Oeps. My fault, read the manual wrong. rbr is right it is correct. fseek positions relative the last argument.
0
 
LVL 10

Expert Comment

by:rbr
ID: 1263056
even this is not ansi c I said that you function will do it, so I answered your question.
to Kangaroo: SEEK_END sets to the end of the file.
0
 
LVL 10

Expert Comment

by:rbr
ID: 1263057
fstat and stat are conform to AT&T and POSIX.
0
 

Author Comment

by:nil092297
ID: 1263058
Sorry again ...
>>  I said that you function will do it,
I KNOW that my function will do it ... the function works fine!
What I want to know: Is this the 'right' , the 'proper' way to do this or is there a (faster?, less function calls?) alternative  ...
 
0
 
LVL 10

Accepted Solution

by:
rbr earned 200 total points
ID: 1263059
I see no other way in ansi standard.
0
 
LVL 10

Expert Comment

by:rbr
ID: 1263060
btw
// is not ansi too
0
 

Author Comment

by:nil092297
ID: 1263061
Ok ;-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question