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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:19:23+00:00 2026-06-14T18:19:23+00:00

How does one read and split/chunk a file by the number of lines? I

  • 0

How does one read and split/chunk a file by the number of lines?

I would like to partition a file into separate buffers, while ensuring that a line is not split up between two or more buffers. I plan on passing these buffers into their own pthreads so they can perform some type of simultaneous/asynchronous processing.

I’ve read the answer below reading and writing in chunks on linux using c but I don’t think it exactly answers the question about making sure that a line is not split up into two or more buffers.

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

    I would choose a chunk size in bytes. Then I would seek to the appropriate location in the file and read some smallish number of bytes at a time until I got a newline.

    The first chunk’s last character is the newline. The second chunk’s first character is the character after the newline.

    Always seek to a pagesize() boundary and read in pagesize() bytes at a time to search for your newline. This will tend to ensure that you only pull the minimum necessary from disk to find your boundaries. You could try reading like 128 bytes at a time or something. But you then risk making more system calls.

    I wrote an example program that does this for letter frequency counting. This, of course, is largely pointless to split into threads as it’s almost certainly IO bound. And it also doesn’t matter where the newlines are because it isn’t line oriented. But, it’s just an example. Also, it’s heavily reliant on you having a reasonably complete C++11 implementation.

    • threaded_file_split.cpp on lisp.paste.org

    They key function is this:

    // Find the offset of the next newline given a particular desired offset.
    off_t next_linestart(int fd, off_t start)
    {
       using ::std::size_t;
       using ::ssize_t;
       using ::pread;
    
       const size_t bufsize = 4096;
       char buf[bufsize];
    
       for (bool found = false; !found;) {
          const ssize_t result = pread(fd, buf, bufsize, start);
          if (result < 0) {
             throw ::std::system_error(errno, ::std::system_category(),
                                       "Read failure trying to find newline.");
          } else if (result == 0) {
             // End of file
             found = true;
          } else {
             const char * const nl_loc = ::std::find(buf, buf + result, '\n');
             if (nl_loc != (buf + result)) {
                start += ((nl_loc - buf) + 1);
                found = true;
             } else {
                start += result;
             }
          }
       }
       return start;
    }
    

    Also notice that I use pread. This is absolutely essential when you have multiple threads reading from different parts of the file.

    The file descriptor is a shared resource between your threads. When one thread reads from the file using ordinary functions it alters a detail about this shared resource, the file pointer. The file pointer is the position in the file at which the next read will occur.

    Simply using lseek before you read each time does not help this because it introduces a race condition between the lseek and the read.

    The pread function allows you to read a bunch of bytes from a specific location within the file. It also doesn’t alter the file pointer at all. Apart from the fact that it doesn’t alter the file pointer, it’s otherwise like combining an lseek and a read in the same call.

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

Sidebar

Related Questions

How does one read a data file in an iPhone project? For example, lets
I am working with WCF .NET 3.5 SP1 and have read that one does
How does one use rm to delete a file named '--help'? When I try,
How does one write a (Intel) F90 function that converts a string into lowercase
How does one properly read values of custom control properties set via design time?
My objective is to read an XML text file and split each word and
How does one access data imported from a CSV file by using dynamic note
I am having trouble determining how to split separate elements in a text file
does one perform better over the other in terms of indexing/quering etc ? e.g.
How does one wait until all of the Javascript is loaded before curling a

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.