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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:48:25+00:00 2026-06-15T16:48:25+00:00

I am reading in a textfile line by line into a 2D array. I

  • 0

I am reading in a textfile line by line into a 2D array. I want to concatenate the char arrays so I have one long char array. I am having trouble with this, I can get it to work with two char arrays but when I try to do a lot of them I go wrong.

Currently the char arrays look like this:

AGCTTTTCATTC

I want to get something like this:

AGCTTTTCATTCAGCTTTTCATTC

I have inlcuded some of my code.

int counter = 0; 
fid = fopen("dna.fna","r");
while(fgets(line, sizeof(line), fid) != NULL && counter!=66283 ) {
    if (strlen(line)==70) {
        strcpy(dna[counter], line);        
    counter++;
    }
}
int dnaSize = 6628; 
//Concatenating the DNA into a single char array.
int i;
char DNA[dnaSize];
for(i = 0; i<66283;i++){
   strcpy(DNA[i],dna[i]);
   strcat(DNA[i+1],dna[i+1]);
}
  • 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-15T16:48:27+00:00Added an answer on June 15, 2026 at 4:48 pm

    You need to loop only up to < counter
    Then, are you copying or concatenating? You only need to do one or the other.

    I suggest just use strcat in the loop, but initialise DNA.

    char DNA[dnaSize] = ""; //initalise so safe to pass to strcat
    for(i = 0; i<counter;i++)
    {
       strcat(DNA,dna[i]); //no need for indexer to DNA
    }
    

    Also, you need to consider the sizes of your two arrays. I believe (hope) that dna is an array of an array of char. If it is, I guess it is 66283 long in it’s first dimension alone. So it is not going to fit into DNA (6628 long), even if each line was 1 char in length.

    Here is an idea on how to allocate exactly the right amount of memory:

    #define MAXLINELENGTH (70)
    #define MAXDNALINES (66283)
    
    //don't copy this line, it will not work because of the sizes involved (over 4MB)
    //it will likely stack overflow
    //just do what you are currently doing as long as it's a 2-d array.
    char dna[MAXDNALINES][MAXLINELENGTH + 1];
    
    int counter = 0; 
    int totalSize = 0;
    fid = fopen("dna.fna","r");
    while(fgets(line, sizeof(line), fid) != NULL && counter!=MAXDNALINES ) {
        const int lineLength = strlen(line);
        if (lineLength==MAXLINELENGTH) {
            strcpy(dna[counter], line);        
            counter++;
            totalSize += lineLength;
        }
    }
    
    //Concatenating the DNA into a single char array (of exactly the right length)
    int i;
    char *DNA = malloc(totalSize+1); // the + 1 is for the final null, and putting on heap so don't SO
    DNA[0] = '\0'; //the initial null is so that the first strcat works
    for(i = 0; i<counter;i++){
       strcat(DNA,dna[i]);
    }
    
    //do work with DNA here
    
    //finally free it  
    free(DNA);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading a text file line by line, and inserting it into an array.
I am reading in from a text file using StreamReader into a string array
I have a program reading from a text file (currently 653 lines long) all
I have two cell arrays one called info{} and the other is called data{}
I'm reading a text file line by line, and storing it into a NSArray.
I have a problem reading a comma-delimited TXT file. This is what I am
I have a textfile encoded in UTF-16. Each line contains a number of columns
I am reading a text file line by line with VBA into a worksheet.
I'm reading a text file line by line and converting it into a string.
I have some code reading a text file and scanning for words between brackets:

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.