C++ File Help?

#include <iostream>

#include <string>

#include <fstream>

using namespace std;

int main()

{

    const int ARRAY_SIZE = 12; // Array size.

    int numbers[ARRAY_SIZE]; // Array with 12 elements.

    int count = 0; // Loop counter variable.

    string filename; // For the user to enter the file name.

    ifstream inputFile; // Input the file stream object.

    // Get the file name from the user.

    cout << "Enter the name of the file you wish to open : ";

    cin >> filename;

    inputFile.open(filename); // Open the file.

    if (inputFile)

    {

        while (count < ARRAY_SIZE && inputFile >> numbers[count])

        {

            count++;

        }

        // Close the file.

        inputFile.close();

    }

Okay so I have this code written, and alot more after that which all runs fine, but I keep getting an error in the area where im trying to open the file,

for the line " inputFile.open(filename)

I am not sure why I keep getting an error considering 1) I have the file needed saved in the same folder as my project and I even linked it/compiled it 

and 2) i cross referenced my code with a ton of sites on google and everyone says this is how to do it.

USEFUL INFO: The prompt I had was to take a "user inputted file" which you want to be the one that you already have saved, and calculate the average total and sum of the numbers listed in the file, what pisses me off is the "user input" part because why can't I just use the file provided? anyway thanks!

3 Answers

Relevance
  • 1 month ago

    The two most common errors opening a file for reading are that the file name is misspelled, or is not in the runtime default directory (and you didn't provide a correct full path).

    By the way, the current working directory might not be (and arguably shouldn't be) the default directory at run time.  This will depend on the IDE you're using. 

    If in doubt, you can include <cstdlib> and then use one of:

    system("CD"); // on Windows, or

    system("echo $PWD"); // almost anywhere else

    If you're on a Windows system, remember that \ path separator characters must be doubled in a C++ string constant ("C:\\Users\\Husoski" for example).  It's lightly documented, but in most contexts outside of the CMD.EXE shell, you can use / instead of \ as path separator character and Windows will do the right thing.

    If you're on one of those "almost anywhere else" systems (Mac, Linux, Unix), remember that file names are case sensitive.

  • Chris
    Lv 7
    1 month ago

    Actually telling us what the error message says would be helpful, don't you think...?

    There's a reason the computer doesn't just say "ERROR", it actually tries to tell you how to fix it.

    I can't count how many times some fool posts a question that does mention the error, and the first google result for the error tells you exactly how to fix it, but not even posting the fücking error message is some next level shît.

    • husoski
      Lv 7
      1 month agoReport

      Chris, this is C++ and not Python or even Java/C#.  You don't get error messages unless you've specifically written code to produce them...and that's clearly not the case here.

  • 1 month ago

    Okay..........n.......

Still have questions? Get your answers by asking now.