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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:39:19+00:00 2026-05-25T00:39:19+00:00

I’m used to PHP, but I’m starting to learn C. I’m trying to create

  • 0

I’m used to PHP, but I’m starting to learn C. I’m trying to create a program that reads a file line by line and stores each line to an array.

So far I have a program that reads the file line by line, and even prints each line as it goes, but now I just need to add each line to an array.

My buddy last night was telling me a bit about it. He said I’d have to use a multidimensional array in C, so basically array[x][y]. The [y] part itself is easy, because I know the maximum amount of bytes that each line will be. However, I don’t know how many lines the file will be.

I figure I can make it loop through the file and just increment an integer each time and use that, but I feel that there might be a more simple way of doing it.

What can I try next?

  • 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-25T00:39:20+00:00Added an answer on May 25, 2026 at 12:39 am

    To dynamically allocate a 2D array:

    char **p;
    int i, dim1, dim2;
    
    
    /* Allocate the first dimension, which is actually a pointer to pointer to char   */
    p = malloc (sizeof (char *) * dim1);
    
    /* Then allocate each of the pointers allocated in previous step arrays of pointer to chars
     * within each of these arrays are chars
     */
    for (i = 0; i < dim1; i++)
      {
        *(p + i) = malloc (sizeof (char) * dim2);
       /* or p[i] =  malloc (sizeof (char) * dim2); */
      }
    
     /* Do work */
    
    /* Deallocate the allocated array. Start deallocation from the lowest level.
     * that is in the reverse order of which we did the allocation
     */
    for (i = 0; i < dim1; i++)
    {
      free (p[i]);
    }
    free (p);
    

    Modify the above method. When you need another line to be added do *(p + i) = malloc (sizeof (char) * dim2); and update i. In this case you need to predict the max numbers of lines in the file which is indicated by the dim1 variable, for which we allocate the p array first time. This will only allocate the (sizeof (int *) * dim1) bytes, thus much better option than char p[dim1][dim2] (in c99).

    There is another way i think. Allocate arrays in blocks and chain them when there is an overflow.

    struct _lines {
       char **line;
       int n;
       struct _lines *next;
    } *file;
    
    file = malloc (sizeof (struct _lines));
    file->line = malloc (sizeof (char *) * LINE_MAX);
    file->n = 0;
    head = file;
    

    After this the first block is ready to use. When you need to insert a line just do:

    /* get line into buffer */
    file.line[n] = malloc (sizeof (char) * (strlen (buffer) + 1));
    n++;
    

    When n is LINE_MAX allocate another block and link it to this one.

    struct _lines *temp;
    
    temp = malloc (sizeof (struct _lines));
    temp->line = malloc (sizeof (char *) * LINE_MAX);
    temp->n = 0;
    file->next = temp;
    file = file->next;
    

    Something like this.

    When one block’s n becomes 0, deallocate it, and update the current block pointer file to the previous one. You can either traverse from beginning single linked list and traverse from the start or use double links.

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

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.