0

I am trying to open a file using ifstream constructor and I can't find a way to do it using a relative path.
All the files are in the same folder called: example

On Linux I made it work using: ifstream file("/home/username/New Volume/VSCode Projects/example/p1.txt");

On Windows I made it work with: ifstream fisier("D:\\Projects\\VSCode Projects\\example\\p1.txt");

I've tried some combinations but nothing worked:

// Linux
ifstream file("p1.txt");
ifstream file("./p1.txt");
ifstream file("../p1.txt");

// Windows
ifstream file("p1.txt");
ifstream file("\p1.txt");
ifstream file("\\p1.txt");
ifstream file(".\p1.txt");
ifstream file(".\\p1.txt");

Inside .vscode folder there is:

// launch.json:
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe build active file"
        }
    ]
}

// settings.json
{
    "files.associations": {
        "iosfwd": "cpp"
    }
}

// tasks.json
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

What is the correct relative path for ifstream ?

bleah1
  • 471
  • 3
  • 18
  • How do you run the program? – user253751 Mar 03 '21 at 14:51
  • Are you sure that you're running your program from that same directory? (or in linux, that you're cd'd into that directory when you run the program?) – scohe001 Mar 03 '21 at 14:51
  • I am running the program by pressing F5. I have added the json settings to the original question. They seem ok to me. I don't know how to check the current working directory. Who tells F5 from where to run ? – bleah1 Mar 03 '21 at 15:17

1 Answers1

0
ifstream file("p1.txt");

This works. Based on seeing "VSCode Projects" in your complete file path I would guess that the issue is that you haven't configured your current working directory correctly in Visual Studio Code.

orlp
  • 112,504
  • 36
  • 218
  • 315
  • I am running the program by pressing F5. I have added the json settings to the original question. They seem ok to me. I don't know how to check the current working directory. Who tells F5 from where to run ? – bleah1 Mar 03 '21 at 15:18
  • `I don't know how to check the current working directory` https://en.cppreference.com/w/cpp/filesystem/current_path – KamilCuk Mar 03 '21 at 15:26
  • @KamilCuk I've been trying to use `filesystem` for the past hour and I can't make it work. I have this: https://stackoverflow.com/questions/65169630/visual-studio-code-using-filesystem-library exact problem and I've tried every fix I could find online and it still doesn't work. I have g++ 8.1.0 and I have added `"--std=c++17 -lstdc++fs"` to the list or `args` inside `tasks.json` – bleah1 Mar 03 '21 at 15:41
  • You added `"--std=c++17", "-lstdc++fs",` right? it's separate arguments. – KamilCuk Mar 03 '21 at 15:52
  • Without ``, you can just use [`GetCurrentDirectory()`](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getcurrentdirectory) on Windows, and [`getcwd()`](https://man7.org/linux/man-pages/man3/getcwd.3.html) on Linux. – Remy Lebeau Mar 03 '21 at 17:55
  • @KamilCuk I did separate the arguments. It doesn't work. I've tried using `GetCurrentDirectory()` from here: https://stackoverflow.com/questions/875249/how-to-get-current-directory but that prompts even more errors. – bleah1 Mar 04 '21 at 12:41