open a file?

This is a discussion on open a file? within the C++ Programming forums, part of the General Programming Boards category; I am trying to open a file on my computer using the following code: Code: ifstream a_file ( "D:\LotteryPrograms\Mega_Mill.txt" ); ...

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

    open a file?

    I am trying to open a file on my computer using the following code:

    Code:
    ifstream a_file ( "D:\LotteryPrograms\Mega_Mill.txt" );   //Opens Mega_Mill for reading the file
      
                while ( !a_file.is_open() )
                {
                        if ( !a_file.is_open() )  // Checks to make sure Mega_Mill file is open and ready to be used.
                        {
                            cout << " The file could not be opened" << endl << endl;
                            cout << "Please wait while we try to open the file." << endl << endl;
    
                            for (int i; i <= 2; i++);
                            
                            
                            (a_file.open("D:\LotteryPrograms\Mega_Mill.txt"));  // If Mega_Mill file is not open,
                                                                                // it opens it.
    
                            cin.get();   // it waits for a key stroke so we can read the comments.
                        }
    
                        else
                        
                        {
                            cout << "It's OK, the file is open" << endl << endl;   // This tells up that its ok
                                                                                    // to use the file.
                            cin.get();  // it waits for a key stroke so we can read the comments.
                        }
                        
    
                }
    but the file does not open. Where is my error? What am I doing wrong?. . . . therry

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,165
    O_o

    Do some looking into escape sequences in C strings.

    Soma

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Code:
    while ( !a_file.is_open() )
    {
        if ( !a_file.is_open() )  // Checks to make sure Mega_Mill file is open and ready to be used.
        {
            ...
        }
        else
        {
            ...
        }
    }
    Why the double check of is_open? The whole if-else test seems unnecessary, the only way you enter the while loop body is if the file is not open so the else code will never be run and the if test is already irrelevant.





    Code:
    for (int i; i <= 2; i++);
    What is this meant to accomplish? The loop variable i is uninitialized and there is no loop body code due to the semicolon at the end.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,506
    Quote Originally Posted by phantomotap View Post
    O_o

    Do some looking into escape sequences in C strings.

    Soma
    The above hints at a problem with your code.
    To escape an "\" in C use "\\".

    Code:
    ifstream a_file ( "D:\LotteryPrograms\Mega_Mill.txt" );   //Opens Mega_Mill for reading the file
    Needs to be

    Code:
    ifstream a_file ( "D:\\LotteryPrograms\\Mega_Mill.txt" );   //Opens Mega_Mill for reading the file
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    That's what comes from not listening to the compiler when it complains about "unrecognized escape sequences".
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. file does not open!
    By aadil7 in forum C Programming
    Replies: 18
    Last Post: 03-17-2011, 08:57 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, 01: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, 06:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21