我正在练习 C++,并创建了一个类,用于存储从快速格式读取的序列及其名称。代码如下:
#include<fstream>
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Sequence {
vector<string> fullSequence, sequenceNames;
public:
void fastaRead(string fileName);
string getSequence(int index);
};
string Sequence::getSequence(int index)
{
return fullSequence[index];
}
void Sequence::fastaRead(string fileName)
{
vector<string> fullSequence, sequenceNames;
ifstream inputFile;
inputFile.open(fileName);
if (inputFile.is_open()) {
string currentSeq;
string line;
bool newseq = false;
while (getline(inputFile, line))
{
if (line[0] == '>') {
sequenceNames.push_back(line.substr(1,line.size()));
newseq = true;
} else {
if (newseq == true) {
fullSequence.push_back(currentSeq);
currentSeq = line;
newseq = false;
} else {
currentSeq.append(line);
}
}
}
}
inputFile.close();
}
int main()
{
Sequence inseq;
cout << "Fasta Sequence Filepath" << endl;
string input;
getline(cin, input);
inseq.fastaRead(input);
inseq.getSequence(0);
return 0;
}
但是,当我使用以下虚拟输入文件运行程序时:
>FirstSeq
AAAAAAAAAAAAAA
BBBBBBBBBBBBBB
>SecondSeq
TTTTTTTTTTTTTT
>ThirdSequence
CCCCCCCCCCCCCC
>FourthSequence
GGGGGGGGGGGGGG
当线路出现分段错误时inset.getSequence(0)
调用我做了什么导致段错误,我如何确保它不会发生?我知道这可能与指针中的错误有关,但我认为我没有使用过指针,如果我没记错的话需要 * 字符。
谢谢,本。