C++ File Help?
using namespace std;
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.
while (count < ARRAY_SIZE && inputFile >> numbers[count])
// Close the file.
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!
- husoskiLv 78 months 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.
- ChrisLv 78 months 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.
- Anonymous8 months ago