I'm writing a simple program to open a file and display the list of numbers on that file. Except when I run it, nothing happens. The numbers will not display.
// This program reads data from a file.
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
ifstream inputFile;
int number;
// Open the file.
inputFile.open("steps.txt");
// Read the numbers from the file and
// display them.
while (inputFile >> number)
{
cout << number << endl;
}
// Close the file.
inputFile.close();
return 0;
}
Your code works perfectly. One thing you wanna check is the directory of the file your trying to read and name. Make sure the "steps.txt" file is in the same directory as the .cpp file.
Since this type of problem is usually a path problem what you can do if reverse the open statement and make it for output. Opening a file for output will create the file if it does not exist. Give the file name an unusual name that is easy to search for. Do a search for that name an you will see which sub directory it is in and that is where you will need to put the input file.
The other option is to put all your input files in one place an include the full path to the file name.