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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:18:39+00:00 2026-06-02T20:18:39+00:00

I have a text file with 25 lines, each with 34 characters on. In

  • 0

I have a text file with 25 lines, each with 34 characters on.

In C how is it possible to load these characters and store them into a 2D array?

If the first three lines of the file are such:

bamaaaaaaaacxxxxxxxxxxbaaaaaaaamac
jzjzzzzzzzzdaaaaaaaaaaezzzzzzzzjzj
jzjzbaaczgzzzzzzzzzzzzzzgzbaaczjzj

...and so on

I require the array to be stored as if it was defined like this:

char* data[] = {
            "baaaaaaaaaaaaaacxxbaaaaaaaaaaaaaac",
            "jzzzzzzzzzzzzzzjxxjzzzzzzzzzzzzzzj",
            "jzbaaaaaaaaaaaaexxdaaaaaaaaaaaaczj",
...and so on

Hopefully this makes some sense! It is important that the type of data is char data[][] as it is used in that format in the rest of my project and cannot be changed.

I have done the basic begining of the File IO

FILE  *infp;

printf("Opening file\n");

if((infp = fopen("file.txt", "r"))== NULL) {
    printf("\nERROR : Unable to open input file\n");
    SetExitWithCode( 999 );
    }else{
            //code here
            }

Can anyone help?

  • 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-02T20:18:40+00:00Added an answer on June 2, 2026 at 8:18 pm

    So, you want your array to look like this:

    char data[25][35] //There is an extra 1 character per line to hold the null terminator character
    

    Then simply read the whole file into that array

    fread(data, 1, sizeof(data), infp);
    

    And finally, replace the new line character in on each line with a terminator

    for (int i = 0; i < 25; ++i) {
        data[i][34] = 0;
    }
    

    This is the easiest solution to the problem, but it is also a bad way of doing it. It does not validate that the data is in the correct format, and everything is hard coded.

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

Sidebar

Related Questions

I have two 3GB text files, each file has around 80 million lines. And
I have a text file with few first few lines not required and then
I have a big text file (about 100MB) and each lines are separated by
I have a text file with 50k+- lines and each line contains data which
I have a text file of about 20 million lines. Each line is 25
If I have a text file with a separate command on each line how
I have a huge text file with lines like: -568.563626 159 33 -1109.660591 -1231.295129
I have a text file with a few 1000 lines of text in it.
I have a text file made of 3 lines: Line 1 Line 3 (Line
I have a text file containing two or more types of lines. I would

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.