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

  • Home
  • SEARCH
  • 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 9133015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:24:45+00:00 2026-06-17T08:24:45+00:00

I want to read a file and write each line to array of char.

  • 0

I want to read a file and write each line to array of char. As I don’t know the amount of lines, therefore I thought the most efficient way is to use 2D array of char pointer. However I get segmentation fault.

My question might be duplicate of this one :
2D array of char pointers –> Segmentation fault?

But I couldn’t figure the correct syntax for C so I couldn’t try.

Here’s my code:

   FILE *file = fopen ( filename, "r" );
   if ( file != NULL )
   {
        char line [ 128 ]; /* or other suitable maximum line size */
        char **new_line;
        int i = 0;
    while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
    {
        strcpy(new_line[i], line);
        i++;
    }
  • 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-17T08:24:46+00:00Added an answer on June 17, 2026 at 8:24 am

    Memory is not allocated for new_line which causes the segmentation fault.

    If you know the no of lines, then you can declare that as local array itself. In that case your accessing method will works fine.

    #define MAX_LINES 20
    #define MAX_CHARS 128
    ...
    char new_line[MAX_LINES][MAX_CHARS] = {0};
    ...
    

    Your problem here is you dont know the maximum number of lines. So you have selected double pointer. In that case you need to first malloc with some n number of lines and then you need to keep on using realloc to increase the buffer size.

    #define MAX_CHARS 128
    #define N_NO_OF_LINES 10
    ...
    char line[MAX_CHARS] = {0};
    char **new_line = NULL;
    int noOfLines = 0;
    int lineCount = 0;
    
    new_line = malloc(sizeof(char*) * N_NO_OF_LINES);
    noOfLines = N_NO_OF_LINES;
    
    while (fgets (line, sizeof line, file) != NULL) /* read a line */
    {
        if (lineCount >= noOfLines)
        {
            new_line = realloc(new_line, (sizeof(char*)*(noOfLines+N_NO_OF_LINES)));
            noOfLines += N_NO_OF_LINES;
        }
    
        new_line[lineCount] = strdup(line);
        lineCount++;
    }
    

    Note : Take care of null check for malloc and realloc

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

Sidebar

Related Questions

I want to read a file (hello.in) and to write it to another file
i want simultaneously read and write data into file. Can i use StreamReader and
Basically I want to read and write to a file then save all the
I want to write a batch file to read an input text file, extract
I want to write a file where an external application can read it, but
I want to read a file from a java web application. I don't want
How can I read a file and convert each line into a date? Ok
i want to read text file containing the data of 4X4 matrix each element
I have a txt file with 16 lines of integers. Each line contains 5
lets say i have 5 lines within a txt file called users.txt each line

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.