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]);
}
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);