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
?