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 8477245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:24:34+00:00 2026-06-10T18:24:34+00:00

Obviously one could loop through a file using fgetl or similar function and increment

  • 0

Obviously one could loop through a file using fgetl or similar function and increment a counter, but is there a way to determine the number of lines in a file without doing such a loop?

  • 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-06-10T18:24:36+00:00Added an answer on June 10, 2026 at 6:24 pm

    I like to use the following code for exactly this task

    fid = fopen('someTextFile.txt', 'rb');
    %# Get file size.
    fseek(fid, 0, 'eof');
    fileSize = ftell(fid);
    frewind(fid);
    %# Read the whole file.
    data = fread(fid, fileSize, 'uint8');
    %# Count number of line-feeds and increase by one.
    numLines = sum(data == 10) + 1;
    fclose(fid);
    

    It is pretty fast if you have enough memory to read the whole file at once. It should work for both Windows- and Linux-style line endings.

    Edit: I measured the performance of the answers provided so far. Here is the result for determining the number of lines of a text file containing 1 million double values (one value per line). Average of 10 tries.

     Author           Mean time +- standard deviation (s)
    ------------------------------------------------------
     Rody Oldenhuis      0.3189 +- 0.0314
     Edric (2)           0.3282 +- 0.0248
     Mehrwolf            0.4075 +- 0.0178
     Jonas               1.0813 +- 0.0665
     Edric (1)          26.8825 +- 0.6790
    

    So fastest are the approaches using Perl and reading all the file as binary data. I would not be surprised, if Perl internally also read large blocks of the file at once instead of looping through it line by line (just a guess, do not know anything about Perl).

    Using a simple fgetl()-loop is by a factor of 25-75 slower than the other approaches.

    Edit 2: Included Edric’s 2nd approach, which is much faster and on-par with the Perl solution, I’d say.

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

Sidebar

Related Questions

This one is quite obviously a function pointer : typedef int (* foobar) (int
Obviously MVC promotes separation of concerns. One thing we are struggling with is proper
Obviously, the typical WebForms approach won't work. How does one track a user in
I have several controls in one of my applications, which all obviously extend the
One obvious way to cause a stack overflow and get Segmentation fault would be
I might be missing something obvious here, but I'm implementing NSCopying on one of
I have two models, associated with a HABTM (actually using has_many :through on both
I'm sure this is not a very pythonic situation. But I'm not actually using
I am looping through an array of file paths retrieved by a core data
My application worked well, without any errors, so far. But lately I've began using

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.