Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3218026
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:28:47+00:00 2026-05-17T15:28:47+00:00

I am trying to learn C and the book I am using (Apress’ ‘Learn

  • 0

I am trying to learn C and the book I am using (Apress’ ‘Learn C’) has a chapter that is terribly confusing on Random Access functions. The following code is confusing me:

 int    GetNumberOfDinos( void ) {
    FILE    *fp;
    long    fileLength;

    if ( (fp = fopen( kDinoFileName, "r" )) == NULL )
        DoError( "Couldn't open file...Goodbye!" );

    if ( fseek( fp, 0L, SEEK_END ) != 0 )
        DoError( "Couldn't seek to end of file...Goodbye!" );

    if ( (fileLength = ftell( fp )) == -1L )
        DoError( "ftell() failed...Goodbye!" );

    fclose( fp );

    return( (int)(fileLength / kDinoRecordSize) );
}

I understand the purpose of the code, but not how that purpose is being achieved. The fopen line is easy to understand. The fseek and ftell is where my troubles begin. The parameters for fseek are the file, the offset, and then one of the 3 SEEKs. Why is the condition of it not being zero given there? If the file truly does exist (kDinoFileName), and they want to point to the end of that file, why would the location be zero? The file exists and there is information! And then I completely don’t understand how the ftell function would ever end up with -1L?? Is this code more difficult than it needs to be?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:28:47+00:00Added an answer on May 17, 2026 at 3:28 pm

    The code is simply checking for errors. fseek() returns 0 on success and non-zero on failure. ftell() returns -1 on failure.

    As far as “why would the location be zero?” – the fseek() call is requesting to seek from the end of the file (SEEK_END). Zero bytes from the end of the file is.. the end of the file. So

    fseek( fp, 0L, SEEK_END )
    

    is a request to move the file pointer to the end of the file.

    The code is performing the following steps:

    1. open the file
    2. seek to the end
    3. get the position of the file pointer (which, since we’re at the end tells you how many bytes are in the file – note, strictly speaking the file needs to be opened with binary access, for example using “rb”, for this to be true)

    The code is complicated somewhat by the error handling. Many books and articles leave out the error handling for exactly that reason. However, that has it’s own drawback of teaching people to ignore error conditions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn Scheme from the book The Little Schemer using DrScheme on
Trying to learn a bit of CSS and I want a horizontal navbar and
In trying to learn how to create objects in ActionScript, I have had no
I'm trying to learn C. As a C# developer, my IDE is Visual Studio.
I am trying to learn the keyboard shortcuts in Visual Studio in order to
I'm trying to learn RegEx in Ruby, based on what I'm reading in The
I'm trying to learn C++ so forgive me if this question demonstrates a lack
I am trying to learn some of the basic and advanced features of visual
I'm trying to learn to use SDL for a little game I'm writing ,
I am trying to learn CodeIgniter to use for a shopping site, but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.