使用字符构建字符串

时间:2013-11-13 18:07:35

标签: c++ string io append

我编写了一种按顺序读取FASTA文件序列的流类:

接口:

class Sequence_stream{
    public:
        Sequence_stream(const char* Filename, std::string Format); // Constructor.
        NucleotideSequence get();    // Get a sequence from file.
    private:
        std::string FileName;
        std::ifstream FileStream;
        std::string FileFormat;
};

实现:

Sequence_stream::Sequence_stream(const char* Filename, std::string Format)
{
    FileName = Filename;
    FileStream.open(FileName);
    FileFormat = Format;
    std::cout << "Filestream is open: " << FileStream.is_open() << std::endl;
}

NucleotideSequence Sequence_stream::get()
{
    if(FileFormat=="fasta")
    {
        if (FileStream.is_open())
        {
            char currentchar;
            std::string name;
            std::vector<Nucleotide> sequence;
            currentchar = FileStream.get();
            if (currentchar == '>') {  // Check that the start of the first line is the fasta head character.
                currentchar = FileStream.get(); // Proceed to get the full name of the sequence. Get characters until the newline character.
                while(currentchar != '\n')
                {
                    name.append(currentchar);
                    currentchar = FileStream.get();
                } // done getting names, now let's get the sequence.
                currentchar = FileStream.get();
                while(currentchar != '>')
                {
                    if(currentchar != '\n'){
                        sequence.push_back(Nucleotide(currentchar));
                    }
                    currentchar = FileStream.get();
                }
                if(currentchar == '>')
                {
                    FileStream.unget();
                }
                return NucleotideSequence(name, sequence);
            } else {
                std::cout << "The first line of the file was not a fasta format description line beginning with '>'. Are you sure the file is of FASTA format?" << std::endl;
                return;
            }

        } else {
            std::cout << "The filestream is not open" << endl;
        }
        return NucleotideSequence(name, sequence);
    }
}

我遇到了get方法的问题,我试图按字符顺序读取字符并将其添加到名为name with append的字符串中,但是当我编译它时抱怨它不是一个常量字符:

/local/yrq12edu/Dropbox/libHybRIDS/HybRIDS_Sequences.cpp|75| error:从'char'到'const char *'的无效转换[-fpermissive] |

然而,变量currentchar不能是常量,因为它在循环期间在获取新字符时发生变化 - 这样做的最佳方法是什么?我已经考虑过将当前的char设为const char *而不是char,但是当我尝试使用它时,我得到了一堆只读错误,它在代码中得到了字符值:

NucleotideSequence Sequence_stream::get()
{
    if(FileFormat=="fasta")
    {
        if (FileStream.is_open())
        {
            const char* currentchar;
            std::string name;
            std::vector<Nucleotide> sequence;
            *currentchar = FileStream.get();
            if (*currentchar == '>') {  // Check that the start of the first line is the fasta head character.
                *currentchar = FileStream.get(); // Proceed to get the full name of the sequence. Get characters until the newline character.
                while(*currentchar != '\n')
                {
                    name.append(currentchar);
                    *currentchar = FileStream.get();
                } // done getting names, now let's get the sequence.
                *currentchar = FileStream.get();
                while(*currentchar != '>')
                {
                    if(*currentchar != '\n'){
                        sequence.push_back(Nucleotide(*currentchar));
                    }
                    *currentchar = FileStream.get();
                }
                if(*currentchar == '>')
                {
                    FileStream.unget();
                }
                return NucleotideSequence(name, sequence);
            } else {
                std::cout << "The first line of the file was not a fasta format description line beginning with '>'. Are you sure the file is of FASTA format?" << std::endl;
                return;
            }

        } else {
            std::cout << "The filestream is not open" << endl;
        }
        return NucleotideSequence(name, sequence);
    }
}

我应该怎么做?我想过可能会将currentchar的值赋给一个临时常量,然后将其提供给append?

谢谢, 本。

2 个答案:

答案 0 :(得分:0)

我想你想要这个:

name.append(&currentchar,1);

而不是:

name.append(currentchar);

append方法想要一个指向角色的指针。

答案 1 :(得分:0)

要将单个字符附加到字符串,选项为

name.append(1, currentchar);
name.push_back(currentchar);
name += currentchar;