【问题标题】:Concatenation in C with 2D char arrayC与二维字符数组的连接
【发布时间】:2012-12-09 01:24:59
【问题描述】:

我正在将文本文件逐行读取到二维数组中。我想连接 char 数组,所以我有一个长 char 数组。我遇到了麻烦,我可以让它与两个 char 数组一起工作,但是当我尝试做很多时,我出错了。

目前 char 数组如下所示:

AGCTTTTCATTC

我想得到这样的东西:

AGCTTTTCATTCAGCTTTTCATTC

我已经包含了我的一些代码。

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 抱歉,我必须平衡这个问题的分数。在我看来,它缺乏研究工作。我认为如果您阅读strcpy 和strcat,您可以自己走得更远。附:很高兴看到dna 的定义,因为那也可能使用第一个strcpy 错误。

标签: c char concatenation dna-sequence


【解决方案1】:

您只需要循环到&lt; 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 long。所以它不适合DNA(6628 long),即使每行的长度是 1 个字符。

这里有一个关于如何正确分配内存的想法:

#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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2018-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-30
    相关资源
    最近更新 更多