I am trying to read and store data from a file from my computer using ifstream, but after building and compiling, my command line interface does not display the output that I am expecting. My code looks like:
#include <iostream>
using namespace std;
int main ()
{
int num1;
ifstream file_In;
file_In.open("YES");
file_In >> num1;
cout << num1 << endl;
file_In.close();
return 0;
}
I have a text file named "YES" on my desktop and it simply contains the text: 10 20 5 5 5 7
and I am expecting to see the number 10 displayed on my command line interface but instead, I get a 0.
Most likely you are running into a problem of incorrect file path. I made a small change in your program and it works fine. So try giving the full path starting from drive name e.g. "C:\nitin\progs\YES".
main()
{
int num1;
ifstream file_In;
file_In.open("C:\\nitin\\progs\\YES");
file_In >> num1;
cout << num1 << endl;
file_In.close();
}