I'm trying to write a function that will take in std::vector
and the file name then write every integer from the file to vector. I wrote a simple void function which I have tested with the debugger and it worked fine but when I called it from the main function it returned empty std::vector
.
#include<iostream>
#include<vector>
#include<fstream>
#include<string>
using namespace std;
void fill_vector(vector<int> v, string file_name)
{
ifstream ifs(file_name);
int n;
while (ifs >> n)
v.push_back(n);
}
int main()
{
vector<int> vec;
string s = "integers.txt";
fill_vector(vec, s);
}