Search code examples
c++stringfstreamstringstream

Can't read text file yet passing is_open and good checks?


I'm getting an Access Violation Writing Location error while trying to load a text file. While debugging, I noticed that my "is_open()" and "good()" checks both pass because I reach "while (std::getline(myfile, line)). How is that possible? What's even dumber is that this very function works perfectly in a project of its own but for some reason I get the access violation error here.

// Header

static bool LoadObj(std::string file, std::vector<GLfloat> &out_vertices, std::vector<GLfloat> &out_normals);

// CPP

bool Resources::LoadObj(std::string file, std::vector<GLfloat> &out_vertices, std::vector<GLfloat> &out_normals)
{
std::string line;
std::ifstream myfile(file);

if (myfile.is_open())
{
    if (myfile.good())
    {
        while (std::getline(myfile, line))
        {
            if (!strncmp(line.c_str(), "v", 1))
            {
                std::string dummy;
                std::stringstream ss(line);
                ss >> dummy;

                while (ss >> line)
                {
                    out_vertices.push_back(std::stof(line));
                    std::cout << line;
                }
            }
        }
    }
}

return false;
}

Solution

  • See the following question for the answer. Needed to include a lib and configure compiler settings. fstream included but "ifstream not found" and "identifier is undefined"