试图打印向量的内容

病房9250

我一直在尝试使C ++变得越来越舒适,而我开始尝试编写一些文件操作手册。我正在研究可以解析f​​asta文件的过程,但是遇到了一些困难:

#include<fstream>
#include<iostream>
#include<string>
#include<vector>

using namespace std;

//A function for reading in DNA files in FASTA format.
void fastaRead(string file)
{
    ifstream inputFile;
    inputFile.open(file);
    if (inputFile.is_open()) {
        vector<string> seqNames;
        vector<string> sequences;
        string currentSeq;
        string line;
        while (getline(inputFile, line))
        {
            if (line[0] == '>') {
                seqNames.push_back(line);
            }
        }
    }
    for( int i = 0; i < seqNames.size(); i++){
        cout << seqNames[i] << endl;
    }
    inputFile.close();
}

int main()
{
    string fileName;
    cout << "Enter the filename and path of the fasta file" << endl;
    getline(cin, fileName);
    cout << "The file name specified was: " << fileName << endl;
    fastaRead(fileName);
    return 0;
}

该函数应通过一个文本文件,如下所示:

Hello World!
>foo
bleep bleep
>nope

并标识以“>”开头的内容,并将其推入向量seqNames中,然后将内容报告回命令行。-因此,我正在尝试编写检测快速格式化磁头的功能。但是,当我编译时,我被告知:

n95753:Desktop wardb$ g++ testfasta.cpp
testfasta.cpp:25:25: error: use of undeclared identifier 'seqNames'
    for( int i = 0; i < seqNames.size(); i++){
                        ^
testfasta.cpp:26:17: error: use of undeclared identifier 'seqNames'
        cout << seqNames[i] << endl;

但是我很确定我在行中声明了向量: vector<string> seqNames;

谢谢,本

谢尔盖·卡里尼琴科(Sergey Kalinichenko)

这是因为您在的内部范围中声明了向量if您需要将声明移出,以便while循环也可以看到它们:

vector<string> seqNames;
vector<string> sequences;
if (inputFile.is_open()) {
    string currentSeq;
    string line;
    while (getline(inputFile, line))
    {
        if (line[0] == '>') {
            seqNames.push_back(line);
        }
    }
}
for( int i = 0; i < seqNames.size(); i++){
    cout << seqNames[i] << endl;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章