-1

I think I may not have the file I'm trying to read in the right spot but i've put it everywhere possible in the IDE and it is still not working. Ive even put it next to the .exe for my program and it still cant find it.

enter image description here

I was expecting the file to be able to be opened so I can read its contents with c++ code.

#include <iostream>
#include <fstream>       
#include <cstdlib>  // exit prototype
#include <iomanip>

using namespace std;

int main(int argc, char* argv[])
{
    ifstream inFile;
    string filename = "infile6";
    // if a filename is provided, open file
    cout << "Enter the name of a file to read from: " << endl;
    //cin >> filename;
    cout << "Name of a file requested: " << filename << endl;

    inFile.open(filename.c_str());
    if (!inFile)
    {
        cerr << endl;
        cerr << "File cannot be opened" << " " << filename << endl;
        exit(1);
    }

    return 0;  // ifstream destructor closes file

} // end main
5
  • yeah I know I dont have an "infile1" file but the same "File cannot be opened" happends with "infile2". Commented Sep 18, 2023 at 21:47
  • 1
    Write a small "Hello World" that writes to a file. Run it. Now find the file. This is the default location, if you don't specify a path (or it's the starting location for a relative path). Commented Sep 18, 2023 at 21:51
  • 1
    You are trying to open the file using a relative path. Chances are, the program's current working directory is not referring where you think it is at runtime. Always open files using absolute paths instead, for exactly this reason. You can use std::file_system::current_path() or equivalent to see where the CWD is actually pointing. Commented Sep 18, 2023 at 21:53
  • The creating a file helped me locate where I should place the files i was trying to read. The file now opens. Thank you! Also, or my assignment we are required to use a relative path so that his software can run the program. Commented Sep 18, 2023 at 22:01
  • Since C++11 you can create inFile from std::string without converting name to C-style string: std::ifstream inFile(filename) Commented Sep 18, 2023 at 22:40

1 Answer 1

1

The default location of projects created with Visual Studio is:

C:\Users\%USERNAME%\Documents\Visual Studio 2022\Projects

replace Visual Studio 2022 with your version

Your binary file is located deep inside that directory tree

Most likely :

C:\Users\%USERNAME%\Documents\Visual Studio 2022\Projects\RA2\Debug\RA2.exe or C:\Users\%USERNAME%\Documents\Visual Studio 2022\Projects\RA2\Debug\RA2.exe

RA2/
    ├── RA2.sln
    ├── RA2.vcxproj
    ├── RA2.vcxproj.filters
    ├── Debug/
    │   └── RA2.exe
    ├── Source Files/
    │   └── main.cpp
    ├── Header Files/
    │   └── ...
    ├── Resource Files/
    │   └── ...
    └── ...

Now, your program is expecting the file infile6 to be located in the same directory as your executable(C:\Users\%USERNAME%\Documents\Visual Studio 2022\Projects\RA2\Debug\).

You can either copy/paste your file in that directory, or you can provide the absolute path to your infile6.

Here is the absolute path ssuming your username is Bob and Visual Studio version 2022

string filename = "C:\\Users\\Bob\\Documents\\Visual Studio 2022\\Projects\\RA2\\Source Files\\infile6";

P.S if you use C++17 you should take advantage of std::filesystem to deal with file paths, its cleaner, safer, and the modern way.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.