Reading vertex data from a file I have a function that reads numbers from a file and constructs a Vector3. Can anyone think of how I can make improvements to this since I am new to C++?

The file has a list of numbers like so:

0.200153
-6.30333
-2.02161
etc...


The function look like this:

//uses 3 vectors to store x,y,z then combines them at the end to make the full PxVec3

void ModelLoader::LoadVertex(std::string fileLocation)
{
    //clears vector so it doesnt break when loading multiple models
    vertexArray.clear();

    std::ifstream file2;
    file2.open(fileLocation);

    if (!file2.is_open())
        std::cout &lt;&lt; "Failed to load vertex data" &lt;&lt; std::endl;

    //individual x,y,z values for the vertex arrays
    std::vector&lt;float&gt; x;
    std::vector&lt;float&gt; y;
    std::vector&lt;float&gt; z;

    int counter = 0;

    //grabs each line add places them into a vector to be used later
    while (!file2.eof())
    {
        if (counter == 3)
            counter = 0;

        std::string num;
        file2 &gt;&gt; num;

        switch (counter)
        {
        case 0:
            x.push_back(stof(num));  //converts string to float
            break;
        case 1:
            y.push_back(stof(num));
            break;
        case 2:
            z.push_back(stof(num));
            break;
        default:
            break;
        }
        counter++;
    }
    file2.close();

    //adds x,y,z values to the vector to complete to full PxVec3
    for (int i = 0; i &lt; x.size(); i++)
        vertexArray.push_back(PhysicsEngine::PxVec3(x[i], y[i], z[i]));
}
Interface

The name LoadVertex(…) suggests that it's going to parse the string, rather than parse the file with the given filename.  I suggest LoadVertexFile(…) for clarity.

The parameter should be a const std::string&amp;, to avoid unnecessarily copying the string.

You test whether file2.is_open(), but the code proceeds anyway if it isn't open.  The error message is printed to std::cout; I would recommend std::cerr, if you must print anything at all.  (Throwing an exception would be better.)  Furthermore, there is no way to programmatically tell whether the whole method succeeded.  What if the number of entries in the file is not a multiple of 3?

Implementation

I don't see any point in building three vectors x, y, and z, then transposing the results to populate vertexArray.  All you need is three temporary floats.  You don't need a counter.  Just read three entries at a time into those three floats.

You don't need to read a std::string and convert it using stof().  The iostream interface knows how to read floats.

You don't need to .close() the file explicitly; the ifstream's destructor takes care of that for you.

Suggested solution

bool ModelLoader::LoadVertexFile(const std::string&amp; fileLocation)
{
    std::ifstream file(fileLocation);
    if (!file) {
        std::cerr &lt;&lt; "Failed to load vertex data\n";
        return false;
    }

    float x, y, z;
    vertexArray.clear();
    while (file &gt;&gt; x &gt;&gt; y &gt;&gt; z)
    {
        vertexArray.push_back(PhysicsEngine::PxVec3(x, y, z));
    }
    return true;
}