当我执行 cout seqA 时,例如如果 dummyLine=ACACACTA seqA 有问题。我将 temp 用于动态数组,因为在此代码之后我编写 seqA[lenA] 编译器说必须确定数组的维度。
char *seqA=NULL;
char *temp=NULL;
int lenA = 0;
fileA.open("d:\\str1.fa");
if(fileA == NULL) {
perror ("Error opening 'str1.fa'\n");
exit(EXIT_FAILURE);
}
string dummyLine;
getline(fileA, dummyLine);
while(getline(fileA, dummyLine)) {
lenA=lenA+(dummyLine.length());
temp=(char*)realloc(seqA,lenA*sizeof(char));
if (temp!=NULL) {
seqA=temp;
for (int i=0; i<(dummyLine.length()); i++)
{
seqA[lenA-dummyLine.length()+i]=dummyLine[i];
}
}
else {
free (seqA);
puts ("Error (re)allocating memory");
exit (1);
}
}
cout<<"Length seqA is: "<<lenA<<endl;
cout<<seqA<<endl;
fileA.close();
输出图片: