Reading numbers from three text files in C++ This code is supposed to read some text files. What should I do in order to optimize my code. I was confused about writing a user defined function so that I can pass any text to it and it stores it in vectors. I have different types of vectors as you see below. 

#include "stdafx.h"
#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;fstream&gt;
using namespace std;

int main()
{
    unsigned short int num1;
    float num2, num3;
    vector&lt;unsigned short int&gt;index;
    vector&lt;float&gt;lenght;
    vector&lt;float&gt;coordinate;
    ifstream InFile1;
    ifstream InFile2;
    ifstream InFile3;
    InFile1.open("E:\\Programming Languages\\HelloC++\\Geodetic\\Index.txt"); 
    InFile2.open("E:\\Programming Languages\\HelloC++\\Geodetic\\Lenght.txt");
    InFile3.open("E:\\Programming Languages\\HelloC++\\Geodetic\\Coordinate.txt");
    if (!InFile1 || !InFile2 || !InFile3){
        cerr &lt;&lt; "Sorry Couldn't Open The File\n.";
    }
    while (InFile1 &gt;&gt; num1) {
        index.push_back(num1);
    }
    while (InFile2 &gt;&gt; num2) {
        lenght.push_back(num2);
    }
    while (InFile3 &gt;&gt; num3){
        coordinate.push_back(num3);
    }
    for (size_t i = 0; i &lt; coordinate.size(); i++) {
        cout &lt;&lt; coordinate[i]&lt;&lt;endl;
    }
    return 0;
}
I'm going to be cheeky here and make a recommendation that's currently beyond what I suspect your current C++ level is, because I feel like this is a great problem to introduce templates:

Consider splitting the file reading in its own function, so that you don't have to reimplement the file loading logic three times. In this case, this will necessarily be a function template, since you can load different type of values.

Here's what it would look like:

template&lt;typename T&gt;
std::vector&lt;T&gt; read_num_file(const char* file_name) {
  // open ifstream and error check here...

  std::vector&lt;T&gt; result;
  T val;
  while(InFile &gt;&gt; val) {
    result.push_back(val);
  }
  return result;
}

int main() {
  std::vector&lt;unsigned short&gt; index = read_num_file&lt;unsigned short&gt;("Index.txt");

  ...
}