fstream寻求:位置保持在-1

时间:2013-11-29 16:38:52

标签: c++ constructor character seekg

我构建了一个用于处理某个文件正式的类,它的构造函数遍历文件并搜索我需要的关键信息 - 这个想法是字符写在多行上,我想读取每个字符的第一个字符line,每行的第二个字符,依此类推。

我有下面的构造函数和定义(可能很糟糕 - 这是我第一次用C ++编写任何严肃的东西),

class AlignmentStream{
private:
    const char* FileName;
    std::ifstream FileStream;
    std::vector<int> NamesStart;
    std::vector<int> SequencesStart;
    std::vector<int> SequenceLengths;
    int CurrentPosition;
    int SequenceNum;


public:
    AlignmentStream(const char* Filename);
    std::vector<int> tellSeqBegins();
    std::vector<int> tellNamesStart();
    std::vector<int> tellSequenceLengths();
    int getSequenceNum();
    AlignedPosition get();
};


AlignmentStream::AlignmentStream(const char* Filename)
{
    FileName = Filename;
    FileStream.open(FileName);
    std::cout << "Filestream is open: " << FileStream.is_open() << std::endl;
    std::cout << "Profiling the alignment file..." << std::endl;
    if (FileStream.is_open() == false)
        throw StreamClosed(); // Make sure the stream is indeed open else throw an exception.
    if (FileStream.eof())
        throw FileEnd();
    char currentchar;
    // Let's check that the file starts out in the correct fasta format.
    currentchar = FileStream.get();
    if (FileStream.eof())
        throw FileEnd();
    if (currentchar != '>')
        throw FormatError();
    NamesStart.push_back(FileStream.tellg());
    bool inName = true;
    bool inSeq = false;
    int currentLength = 0;
    while(!FileStream.eof()){
        while (!FileStream.eof() && inName == true) {
            if (currentchar == '\n') {
                inName = false;
                inSeq = true;
                SequencesStart.push_back(FileStream.tellg());
            } else {
                currentchar = FileStream.get();
            }
        }
        while (!FileStream.eof() && inSeq == true) {
            if (currentchar == '>') {
                inName = true;
                inSeq = false;
                NamesStart.push_back(FileStream.tellg());
            } else {
                if (currentchar != '\n') {
                    currentLength++;
                }
                currentchar = FileStream.get();
            }
        }
        SequenceLengths.push_back(currentLength); // Sequence lengths is built up here - (answer to comment)
        currentLength = 0;
    }
    SequenceNum = (int)SequencesStart.size();
    // Now let's make sure all the sequence lengths are the same.
    std::sort(SequenceLengths.begin(), SequenceLengths.end());
    //Establish an iterator.
    std::vector<int>::iterator it;
    //Use unique algorithm to get the unique values.
    it = std::unique(SequenceLengths.begin(), SequenceLengths.end());
    SequenceLengths.resize(std::distance(SequenceLengths.begin(),it));
    if (SequenceLengths.size() > 1) {
        throw FormatError();
    }
    std::cout << "All sequences are of the same length - good!" << std::endl;
    CurrentPosition = 1;
    FileStream.close();
}

道歉它是相当大块的,无论如何构造函数通过char遍历char并获取要读取的每一行的起点。 get函数(未显示)然后通过并寻找每一行的开头+多少到达正确的字符 - 由成员变量CurrentPos给出。然后它构造我的另一个自定义对象AlignedPosition并返回它。

AlignedPosition AlignmentStream::get()
{
    std::vector<char> bases;
    for (std::vector<int>::iterator i = SequencesStart.begin(); i != SequencesStart.end(); i++) {
        // cout messages are for debugging purposes.
        std::cout << "The current filestream position is " << FileStream.tellg() << std::endl;
        std::cout << "The start of the sequence is " << *i << std::endl;
        std::cout << "The position is " << CurrentPosition << std::endl;
        FileStream.seekg((int)(*i) + (CurrentPosition - 1) );
        std::cout << "The Filestream has been moved to " << FileStream.tellg() << std::endl;
        bases.push_back(FileStream.get());
    }
    CurrentPosition++;
    //this for loop is just to print the chars read in for debugging purposes.
    for (std::vector<char>::iterator i = bases.begin(); i != bases.end(); i++) {
        std::cout << *i << std::endl;
    }
    return AlignedPosition(CurrentPosition, bases);
}

正如您所看到的,第一个循环遍历每一行的起始位置+ CurrentPosition,然后获取char并将其推回到一个向量上,此向量将传递给我的AlignedPosition构造函数,其他所有内容都是用于调试的消息。但是在执行时我看到了:

eduroam-180-37:libHybRIDS wardb$ ./a.out
Filestream is open: 1
Profiling the alignment file...
All sequences are of the same length - good!
SeqNum: 3
Let's try getting an aligned position
The current filestream position is -1
The start of the sequence is 6
The position is 1
The Filestream has been moved to -1
The current filestream position is -1
The start of the sequence is 398521
The position is 1
The Filestream has been moved to -1
The current filestream position is -1
The start of the sequence is 797036
The position is 1
The Filestream has been moved to -1
?
?
?
Error, an invalid character was present
Couldn't get the base, caught a format error! 

简而言之,我看到的是文件流的位置是-1,并且在使用seek时不会改变。这会导致无效的字符和在我的AlignedPosition构造函数中抛出的异常。这个问题是否已经通过文件导航直到我的构造函数结束?为什么我在输入流中的位置始终保持为-1?

谢谢, 本。

1 个答案:

答案 0 :(得分:1)

如果您在流上收到文件结尾,seekg可能无法清除它。您需要先在流上调用clear()。由于您阅读直至EOF,您可能需要致电clear()。 (参考:en.wikipedia.org/wiki/Seekg)