Information about system("pause");
As for system("Pause")
and it's use: I wouldn't recommend using it in any code base that is designed to be ported or distributed and here is why: Why is it wrong!.
Now if you are using it for your own quick dirty hack to keep windows console open just for testing small functions or classes that is fine, however here is an alternative that will do the same thing that meets the standard as well as being portable.
#include <iostream>
#include <string>
int main() {
[code...]
std::cout << "\nPress any key to quit.\n";
std::cin.get(); // better than declaring a char.
return 0;
}
Information about freopen()
Another thing to be aware of is that you are calling freopen()
on a text file but you never have any call to close the file after you are done with it; I don't know if freopen()
does this for you automatically, but if it doesn't then you should close the file handle before exiting the program and after you have extracted all the needed information from it.
Here is a related Q/A stack: freopen()
.
For more information on freopen()
here is an excellent resource webpage: C: <cstdio> - freopen()
& C++: <cstdio> - std::freopen()
My Attempt of running your code.
Now that I was able to test your program. If you are using Visual Studio and running the application from your debugger in debug mode it will automatically close out the application when it is done. You can either run it without the debugger( ctrl + F5
) or run it from a console or terminal outside of the IDE from the path of the generated executable and the program will run in the manner you are expecting it to.