I put file to cin stream via command argument like this:
./program < input.txt
And I have this simple code:
std::vector<std::string> getLines(std::istream& input)
{
std::vector<std::string> data;
std::string line;
int i = 0;
char c;
while (input.get(c))
{
if (c == '\n')
{
data.push_back(line);
i = 0;
}
else
line += c;
}
return data;
}
int main(void)
{
double elapsed;
std::vector<std::string> out;
Timer timer_1;
// the data is now in cin stream so I pass it to function above
out = getLines(std::cin);
elapsed = timer_1.elapsed();
for (unsigned int i = 0; i < out.size(); i++)
std::cout << out[i] << '\n';
std::cout << elapsed << '\n';
// now try to stop program from closing:
std::getchar(); // doesn't work
system("pause"); // doesn't work either
// this doesn't work too
while (getchar() != '\n');
getchar();
// program exits before I can see the output
return 0;
}
Of course I could just breakpoint the last statement but this cannot be done in release. If you are interested in Timer class I took it from this answer.