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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:37:00+00:00 2026-06-03T04:37:00+00:00

I could use a set of eyes (or more) on this code. I’m trying

  • 0

I could use a set of eyes (or more) on this code. I’m trying to read in a set amount of bytes from a filestream (f1) to an array/buffer (file is a text file, array is of char type). If I read in size “buffer – 1” I want to “realloc” the array and the continue to read, starting at where I left off. Basically I’m trying to dynamically expand the buffer for the file of unknown size. What I’m wondering:

  1. Am I implementing this wrong?
  2. How would I check failure conditions on something like “realloc”
    with the code the way it is?
  3. I’m getting a lot of warnings when I compile about “implicit declaration of built-in function realloc…” (I’m seeing that warning for my use of read, malloc, strlen, etc. as well.
  4. When “read()” get’s called a second time (and third, fourth, etc.) does it read from the beginning of the stream each time? That could be my issue is I only seem to return the first “buff_size” char’s.

Here’s the snippet:

//read_buffer is of size buff_size
n_read = read(f1, read_buffer, buff_size - 1);
read_count = n_read;
int new_size = buff_size;
while (read_count == (buff_size - 1))
{

        new_size *= 2;
        read_buffer = realloc(read_buffer, new_size);
        n_read = read(f1, read_buffer[read_count], buff_size - 1);
        read_count += n_read;
}

As I am learning how to do this type of dynamic read, I’m wondering if someone could state a few brief facts about best practices with this sort of thing. I’m assuming this comes up a TON in the professional world (reading files of unknown size)? Thanks for your time. ALSO: As you guys find good ways of doing things (ie a technique for this type of problem), do you find yourselves memorizing how you did it, or maybe saving it to reference in the future (ie is a solution fairly static)?

  • 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-03T04:37:02+00:00Added an answer on June 3, 2026 at 4:37 am

    If you’re going to expand the buffer for the entire file anyway, it’s probably easiest to seek to the end, get the current offset, then seek back to the beginning and read in swoop:

    size = lseek(f1, 0, SEEK_END); // get offset at end of file
    
    lseek(f1, 0, SEEK_SET); // seek back to beginning
    
    buffer = malloc(size+1); // allocate enough memory.
    
    read(f1, buffer, size);  // read in the file
    

    Alternatively, on any reasonably modern POSIX-like system, consider using mmap.

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

Sidebar

Related Questions

I'm working alone on this project and could use another set of eyes to
I could really use another set of eyes on this so I thought I
I could use another set of eyes - perhaps I am missing somehting obvious.
I could use another pair of eyes. This script does what I want in
Could I use migrator.net bare migration framework and just have a set of SQL
I could use a bit of help with this, as i am new to
I know I could use PHP to do this, but wanted to find out
Having some trouble with the Migrations in Sequel and could use another set of
I could use a little help. I got this program to work right then
I am looking for an algorithm I could use to solve this, not the

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.