예를 들어, dummyLine = ACACACTA seqA에 문제가있는 경우와 같이 cout seqA를 수행합니다. 나는이 코드 이후에 seqA [lenA] 컴파일러에 쓸 때 배열의 차원을 결정해야하기 때문에 동적으로 배열에 임시 변수를 사용합니다. 출력의realloc 메모리가 C++에서 올바르게 작동하지 않습니다
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();
사진 :
맞습니다. 내 생각에 코드를 사용하는 것이 가장 좋습니다. –