fstream搜索:位置保持在-1

fstream seeking: position stays at -1

本文关键字:位置 搜索 fstream      更新时间:2023-10-16

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

我有下面的构造函数和定义(可能很可怕——这是我第一次用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();
}

很抱歉它是一个相当大的块,不管怎样,构造函数都会逐个字符地读取每一行的起点。然后,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,并且在使用查找时不会改变。这会导致在我的AlignedPosition构造函数中引发无效字符和异常。这是否与已经在我的构造函数中导航到文件末尾有关?为什么我在输入流中的位置一直保持在-1?

谢谢,本。

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