您只需要循环到< counter
然后,您是在复制还是连接?你只需要做一个或另一个。
我建议只在循环中使用 strcat,但初始化 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
}
此外,您需要考虑两个数组的大小。我相信(希望)这dna
是一个数组的数组char
。如果是的话,我想它66283
在它的第一个维度上很长。因此,即使每行的长度为 1 个字符,它也不适合DNA
( long)。6628
以下是关于如何准确分配正确内存量的想法:
#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);