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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:14:17+00:00 2026-05-25T12:14:17+00:00

As a school assignment I’m tasked with writing a program that opens any text

  • 0

As a school assignment I’m tasked with writing a program that opens any text file and performs a number of operations on the text. The text must be loaded using a linked list, meaning an array of structs containing the char pointer and the pointer to the next struct. One line per struct.

But I’m having problems actually loading the file. It seems the memory required to load the text into memory must be allocated before I actually read the text. Hence I have to open the file several times. Once to count the number of lines, then twice per line; once to count the characters in the line then once to read them. It seems absurd to open a file hundreds of times just to read it into memory.

Obviously there are better ways of doing this, I just don’t know them 🙂

Examples

  • Can the point from which fgetc fetches a character be moved without re-opening the file?
  • Can the number of lines or characters in a file be checked before it is “opened”?
  • Can I somehow read a line or string from a file and save it to memory without allocating a fixed amount of bytes?
  • etc
  • 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-25T12:14:18+00:00Added an answer on May 25, 2026 at 12:14 pm

    There is no need to open the file more than once, nor to pass through it more than once.

    Look at the POSIX getline() function. It reads lines into allocated space. You can use it to read the lines, and then copy the results for your linked list.

    There is no need with a linked list to know how many lines there are ahead of time; that’s an advantage of lists.

    So, the code can be done with a single pass. Even if you can’t use getline(), you can use fgets() and monitor whether it reads to end of line each time, and if it doesn’t you can allocate (and reallocate) space to hold the line as needed (malloc(), realloc() and eventually free() from <stdlib.h>).

    Your specific questions are largely immaterial if you adopt anything of the approach I suggest, but:

    • Using fseek() (and in extremis rewind()) will move the read pointer (for fgetc() and all other functions), unless the ‘file’ does not support seeking (eg, a pipe provided as standard input).

    • Characters can be determined with stat() or fstat() or variants. Lines cannot be determined except by reading the file.

    • Since the file could be from zero bytes to gigabytes in size, there isn’t a sensible way of doing fixed size allocations. You are pretty much forced into dynamic memory allocation with malloc() et al. (Behind the scenes, getline() uses malloc() and realloc().)

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

Sidebar

Related Questions

I'm writing a simple booking program for a car rental (a school assignment). Me
I currently have a school assignment that involves both PHP and asp.net. Now the
I am working on a school assignment that requires me to be able to
I'm writing a little genetic algorithm in Java, as a school assignment. Up until
I am working on a school assignment that requires operator overloading. I am having
In a school assignment I'm supposed to complete a method that should return an
I've been working on a school assignment that makes pretty heavy use of vectors,
i have an erd for a school assignment that contains a many to many
I am creating a file server application for school assignment. What I currently have
For an assignment at school, we have to use structs to make matrices that

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.