我无法使用带有fstream的vscode创建或读取文件。从.exe运行的程序工作正常(创建一个空的.txt文件(。main.cpp:
#include <fstream>
int main()
{
std::ofstream file("empty.txt");
}
上面的程序在使用vscode运行时不会创建文件。
- 编辑器:Visual Studio代码
- 编译器:MinGW-w64(MSYS2(
- 系统:Windows 10
launch.json:
{
"configurations": [
{
"name": "C/C++: g++.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "C:\msys64\mingw64\bin",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\msys64\mingw64\bin\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
],
"version": "2.0.0"
}
tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\msys64\mingw64\bin\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${fileDirname}\**.cpp",
//"${fileDirname}\**.h",
//"${file}",
"-o",
"${fileDirname}\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\msys64\mingw64\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
将launch.json
和tasks.json
中的cwd
更改为"cwd": "${workspaceFolder}",
。
另请参阅官方如何。