Program won't open file?

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  // This program reads data from a file.
#include <iostream>
#include <fstream>
using namespace 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.
Hello Bradenm95,

Coderguy101 has your answer.

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.

Hope that helps,

Andy
Last edited on
Registered users can post here. Sign in or register to post.