-
Open file?
I am trying to open a file using the following code,
Code:
ifstream a_file ( "D:\\ LotteryProgram / MegaMill" );
//Opens MegaMill for reading the file
if ( !a_file.is_open() )
{
cout << " The file could not be opened" << endl;
(a_file.open());
}
else {
cout << "Safely use the file stream" << endl;
cin.get();
}
but I am getting an error:
"Error 1 error C2661: 'std::basic_ifstream<_Elem,_Traits>::open' : no overloaded function takes 0 arguments"
and I don't understand it. Why do I get an error when I tell the program to open a_file if it is not open? Can someone translate this error into English so I can make sense of it? ....therry
-
You cannot call open() with no parameters. Read the documentation and see that you need to supply a file name and optionally, the file mode.
The parenthesis around the function call do nothing.
Code:
a_file.open(filename);
Will do just fine, although not much is likely to change in the nanoseconds since you tried to open the file with
Code:
ifstream a_file ( "D:\\ LotteryProgram / MegaMill" );
You should make sure you supply the correct path. I'm pretty sure that file names are not allowed to begin with spaces, so what you've got here is almost certainly wrong.
-
Why do you have that open statement inside the if ?
If opening failed once, it is very much possible that it'll fail again.
Put it in a while loop... going on for a fixed no. of times or indefinitely until it succeeds.
-
It looks like you're still new to programming, so here's a tip about error messages that talk about overloads with x number of arguments: Essentially you can have multiple functions defined in your program, even within the same class or whatever, if they use different argument types or argument amounts. For instance:
Code:
void func() {}
void func(int x) {}
void func(string x) {}
void func(int x, string x) {}
When you try to call one of those functions in your program, the compiler can figure out which one just by looking at the arguments you passed to it. These are usually thought of as multiple definitions of the same function - an "overloaded" function. So when you see error messages that talk about overloads taking x amount of arguments, that means that you're calling a function that exists, but with the type(s) and/or number of your arguments being incompatible with any of its definitions.
-
Well, technically, it'll probably give you a slightly different error if it's just the types that are wrong.
-
Yes, I am so new to C++ that I cannot even be called a newbie. I am teaching myself... with the help of all you guys. Could you give me more documented examples about those *void finc() ()*?
Please excuse my ignorance... A lot of time, when I Google, what I get is so technical that it leaves me just as confused. I'm still trying to understand something that is strange to me. . . . but I'm learning!
-
NeonBlack, that helped me too
Thank you