Question about File Reader Class

I am making a file reader class to read files. It must be able to ,given a string skip ahead to the line after the given string. I am trying to do this in the constructor. Here is the code I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "../inc/SnakeFileReader.hpp"

SnakeFileReader::SnakeFileReader(std::string& filePath)
{
    fileToRead_.open(filePath.c_str(), std::ios::in);
}
SnakeFileReader::SnakeFileReader(std::string& filePath, std::string& startString)
{
    fileToRead_.open(filePath.c_str(), std::ifstream::in);

    std::string toFind;

    while (toFind != startString  && !fileToRead_.eof())
    {
        fileToRead_ >> toFind;
    }
}
std::string& SnakeFileReader::ReadLine()
{
    std::string fileLine = "";

    if (!fileToRead_.eof())
        fileToRead_ >> fileLine;

    return fileLine;
}
SnakeFileReader::~SnakeFileReader()
{
    fileToRead_.close();
}


The first constructor works fine. However the second caused a run time error when it tries to open the file (the same one given to the other constructor). I will put a check to make sure it can load a file but I still need to fix this.

Thanks.
Just realised its not when it tries to open the file it causes the error (Segmentation Fault) its as soon as it enters the constructor.
Topic archived. No new replies allowed.