Thread: Selecting any option other than exit results in "This file does not exist"

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    1

    Selecting any option other than exit results in "This file does not exist"

    Hi,

    Selecting any option (1-3) results in 'This file does not exist'. I've fiddled around, to no avail. I'm guessing it's something to do with the folder structure the files are actually in, but obviously, I'm not too sure.

    The files themselves are in the Visual Studio project folder.

    Can anyone help?

    Cheers.

    Code:
    #include <iostream>
    #include <fstream>
    
    
    using namespace std;
    
    int main()
    
    
    {
        cout << "Willkommen zu meinem Programm!\n\n";
        char choice;
        do {
            //User Prompt
            cout << "Please Choose a Selection from the Menu.\n\n";
            //Display Menu
            cout << "1) Program Languages and their uses\n2) Different Programming Paradigms\n3) Different C++ Features and their uses \n4) Exit\n\n";
            cin >> choice;
            cout << endl;
            if (choice == '1')
            {
                //Read the file
                ifstream infile;
                ofstream outfile;
                //Open the File
                infile.open("ProgrammingLanguages.txt", ios::in);
                //Ensure File exists
                if (!infile)
                {
                    cout << "File does not exist!\n\n";
                }
                //Close the file
                infile.close();
            }
            if (choice == '2')
            {
                //Read the file
                ifstream infile;
                ofstream outfile;
                //Open the File
                infile.open("ProgrammingParadigms.txt", ios::in);
                //Ensure File exists
                if (!infile)
                {
                    cout << "File does not exist!\n\n";
                }
                //Close the file
                infile.close();
            }
            if (choice == '3')
            {
                //Read the file
                ifstream infile;
                ofstream outfile;
                //Open the File
                infile.open("C++Features.txt", ios::in);
                //Ensure File exists
                if (!infile)
                {
                    cout << "File does not exist!\n\n";
                }
                //Close the file
                infile.close();
            }
            else if (choice == '4')
            {
                //Tell user to have nice day
                cout << "A thousand blessings upon you and your family.\n";
                cin.get();
                cin.ignore();
        }
    
    
        } while (choice != '4');
    
    
        return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    9
    Hi,

    Obviously, the files you wanted to read are no in the same path with your current C++ program. You might have to check that.
    There are some improvement you can also make on your code.

    Instead of using repeated ifs like you are doing, why not use if/else if? Then why repeat the ifstream and ofstream in every time?
    You can place so that all if statement have have access to just one.

    Lastly, you are not reading any of the file, so anything is display even if your code works.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The easiest way to find out where your program expects "local" files to be is to try to create an output file with some unique file name. Then use your operating system "find" functionality to locate where the program wrote the file, that will be the location where you need to put your input files.

    Also you should get used to using the stream constructor to open your file and let the destructor close the file automatically, and note an ifstream is always opened for input.

    Code:
    ifstream fin("YourInputFile.txt");
    Also you should go to your command line change into the directory and insure the files actually exist where you think they are and that the extensions are correct. Many times Windows helps you by providing the file extension so you may have a file named "YourInputFile.txt.txt".

    Obviously, the files you wanted to read are no in the same path with your current C++ program.
    Actually it's not obvious at all. That is a possibility but that is not the only possible problem. It is possible, for example, that the file is in the proper place with the proper name but you don't have the correct access rights.


    Then why repeat the ifstream and ofstream in every time?
    Look closer at the code, the file names are different in every if statement. So keeping the streams local to the block is a good choice.


    Jim
    Last edited by jimblumberg; 04-12-2016 at 04:15 PM.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also posted here.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    9
    Look closer at the code, the file names are different in every if statement. So keeping the streams local to the block is a good choice.
    I get the angle you are pointing at, but even with a different file names, keeping the streams at the top of the if/else if, works good. it using a single
    file handler to open different files, as long as the handler is closed lexically; like so:


    Code:
            ....
            if (...) {...}
             else if (choice == '3')  {
    
                //Open the File
                infile.open("myFile.txt", ios::in);
    
                //Ensure File exists
                if (!infile)
                {
                    cout << "File does not exist!\n\n";
                }
                 while(getline(infile, line))
                cout << line << endl;
    
                //Close the file
                infile.close();  // note
            }
           ...

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    But why worry about having to close the stream? If you keep the stream local the destructor automatically closes the stream when the instance goes out of scope. It is usually better to try to practice the RAII technique whenever possible.

    And don't forget that just closing a file and reopening the file doesn't always reset the error flags for the stream.

    Jim

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    9
    Quote Originally Posted by jimblumberg View Post
    But why worry about having to close the stream? If you keep the stream local the destructor automatically closes the stream when the instance goes out of scope. It is usually better to try to practice the RAII technique whenever possible.

    And don't forget that just closing a file and reopening the file doesn't always reset the error flags for the stream.

    Jim
    You have a point there.

    Maybe the OP could then use a function to do that, instead of repetitions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Option to "Continue or Quit" will not work.
    By steveryan in forum C Programming
    Replies: 12
    Last Post: 11-09-2012, 04:54 PM
  2. Question about "Back" option in menu.
    By Xanderbeard in forum C++ Programming
    Replies: 6
    Last Post: 05-11-2010, 04:56 PM
  3. Replies: 2
    Last Post: 01-13-2010, 04:58 PM
  4. IE7 and Firefox2 "open file" option
    By joelsmit in forum Tech Board
    Replies: 1
    Last Post: 01-11-2007, 03:18 AM
  5. "Selecting" Instances of Classes
    By SnS CEO in forum C++ Programming
    Replies: 12
    Last Post: 08-04-2005, 07:50 PM