Question:
C++ File Help?
?
2019-11-14 05:02:08 UTC
#include

#include

#include

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!
Three answers:
husoski
2019-11-14 12:25:45 UTC
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 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
2019-11-14 09:17:26 UTC
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.
anonymous
2019-11-14 05:02:42 UTC
Okay..........n.......


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...