Thread: Open file?

  1. #1
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40

    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>:pen' : 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

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    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.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    100
    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.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    100
    Well, technically, it'll probably give you a slightly different error if it's just the types that are wrong.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Location
    Florida, Dade County, Homestrad, 33031
    Posts
    40
    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!

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    1
    NeonBlack, that helped me too
    Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't open file
    By LyTning94 in forum C++ Programming
    Replies: 8
    Last Post: 12-28-2011, 06:05 PM
  2. cant open file - says no file or directory
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 10:57 PM
  3. Replies: 3
    Last Post: 03-08-2010, 02:43 PM
  4. Open File/Save File crashes on cancel?
    By Blackroot in forum Windows Programming
    Replies: 0
    Last Post: 08-02-2008, 02:16 AM
  5. Replies: 12
    Last Post: 03-10-2005, 07:48 PM