【问题标题】:std::ifstream constructor only accepts absolute path?std::ifstream 构造函数只接受绝对路径?
【发布时间】:2021-03-03 14:49:58
【问题描述】:

我正在尝试使用 ifstream 构造函数打开文件,但找不到使用相对路径的方法。
所有文件都在同一个文件夹中,名为:example

在 Linux 上我使用:ifstream file("/home/username/New Volume/VSCode Projects/example/p1.txt");

在 Windows 上我让它工作:ifstream fisier("D:\\Projects\\VSCode Projects\\example\\p1.txt");

我尝试了一些组合,但没有任何效果:

// 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");

.vscode文件夹里面有:

// 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"
}

ifstream 的正确相对路径是什么?

【问题讨论】:

  • 你是如何运行程序的?
  • 你确定你是从同一个目录运行你的程序吗? (或者在 linux 中,当你运行程序时你被 cd'd 到了那个目录?)
  • 我正在按 F5 运行程序。我已将 json 设置添加到原始问题中。他们对我来说似乎没问题。我不知道如何检查当前工作目录。谁告诉 F5 从哪里跑?

标签: c++


【解决方案1】:
ifstream file("p1.txt");

这行得通。根据在您的完整文件路径中看到“VSCode 项目”,我猜问题是您没有在 Visual Studio Code 中正确配置当前工作目录。

【讨论】:

  • 我正在按 F5 运行程序。我已将 json 设置添加到原始问题中。他们对我来说似乎没问题。我不知道如何检查当前工作目录。谁告诉 F5 从哪里跑?
  • I don't know how to check the current working directory en.cppreference.com/w/cpp/filesystem/current_path
  • @KamilCuk 在过去的一个小时里我一直在尝试使用filesystem,但我无法让它工作。我有这个:stackoverflow.com/questions/65169630/… 确切的问题,我已经尝试了我可以在网上找到的所有修复程序,但它仍然无法正常工作。我有 g++ 8.1.0 并且我已将 "--std=c++17 -lstdc++fs" 添加到列表或 argstasks.json
  • 您添加了"--std=c++17", "-lstdc++fs", 对吗?这是单独的论点。
  • 没有<filesystem>,你可以在Windows上使用GetCurrentDirectory(),在Linux上使用getcwd()
猜你喜欢
  • 1970-01-01
  • 2013-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-23
  • 2016-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多