Share via


I can't get .txt files to open, even though they're in the right directory

Question

Tuesday, December 5, 2017 8:02 PM | 1 vote

Hi hello so first of all, sorry if this is the wrong place to put this question

I can't get it to open my files successfully. Here's an example of code that won't work:

ifstream inFS;

inFS.open("exam.txt");
if (!inFS.is_open()) {
cout << "Could not open exam.txt" << endl;
return 0;
}

It runs through the code and tells me it can't open it. I have the file in \ConsoleApplication2\ConsoleApplication2\ where i KNOW it's meant to direct to. I set the working directory to it. It still won't open them.

Pls advise (,,,: thank you

All replies (2)

Tuesday, December 5, 2017 10:07 PM | 1 vote

This is a C++ language question. The ifstream class is a C++ standard class. You will get faster and better results if you use the C++ forum for C++ questions.

For diagnostic purposes, try adding a GetCurrentDirectory to ensure you are in the directory. The following works for me.

TCHAR Buffer[MAX_PATH];
DWORD dwRet;
dwRet = GetCurrentDirectory(MAX_PATH, Buffer);
std::wcout << Buffer << std::endl;

std::ifstream inFS;
inFS.open("exam.txt");
if (!inFS.is_open()) {
    std::cout << "Could not open exam.txt" << std::endl;
    return 0;
}
std::cout << "Opened\n";

Sam Hobbs
SimpleSamples.Info


Wednesday, December 6, 2017 2:50 PM | 1 vote

The problem is, since you haven't said how you are running your executable then we don't know if it is meant to direct you to that directory. Since you also said only a relative path, then do you mean C:\ConsoleApplication2\ConsoleApplication2\ Z:\mysources\ConsoleApplication2\ConsoleApplication2\ or even somewhere in between O:\My Pictures\WeirdPictures\ConsoleApplication2\ConsoleApplication2\ Relative paths don't hold any meaning on their own.

The working directory of an executable can be annoying, because it can change depending on how you run your application. If you run it in Visual Studio, even if you don't have the debugger attached, then it will be set to what ever is in the debugger properties. This is by default the project directory (the directory that the .vcxproj file is in). If you actually look at the settings itself, it expands to an absolute path.

If you try to set this to a relative path, then what Visual Studio does is interesting. It tacks the relative path onto the end of the project directory.

You will find this a common theme, if you use a relative path as a working directory, then whatever is used to run it will do something unexpected.

For the remaining descriptions, I am going to be using a sample that is similar to yours and includes the code to get the current directory that Sam also posted. The code is:

#include <fstream>
#include <iostream>
#include <Windows.h>

void gcd()
{
    wchar_t Buffer[MAX_PATH];
    DWORD dwRet;
    dwRet = GetCurrentDirectoryW(MAX_PATH, Buffer);
    std::wcout << Buffer << std::endl;

}

void pause_exit()
{
    char mv;
    std::cout << "Press Any Key To Continue...\n";
    fread(&mv, 1, 1, stdin);
}

int wmain()
{
    std::ifstream in;

    gcd();

    in.open("test.txt");
    if (!in.is_open())
    {
        std::cout << "failed\n";

        pause_exit();

        return -1;
    }

    std::cout << "file opened\n";

    pause_exit();

    return 0;
}

The executable file's path is C:\Users\Darran\source\testme\Debug\testme.exe

For other ways to run an application, you have the shortcut, the working path is set inside the shortcut. If you double click on the file directly then the working directory will be set to the same directory as the executable. Output:

C:\Users\Darran\source\repos\testme\Debug
failed
Press Any Key To Continue...

If you run it from the console, the path is the current directory in the console:

C:\Users\Darran\source\repos\testme\Debug>testme
C:\Users\Darran\source\repos\testme\Debug
failed
Press Any Key To Continue...

C:\Users\Darran\source\repos\testme\testme>..\debug\testme
C:\Users\Darran\source\repos\testme\testme
file opened
Press Any Key To Continue...

So as was already said by Sam, you should check if it really is checking where you think it should check.

But this is also why relative paths are discouraged. Just changing the current directory can cause problems. It is usually better to keep the files that you want to access in the same directory tree as the main executable and use that as a root.

This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.