示例路径:E:\Cpp
输入文件:E:\Cpp\1.in
输出文件:E:\Cpp\1.out
1.用Code Runner运行:
在settings.json的"code-runner.executorMap"中加入-D LOCAL,之后效果大概如下:
"code-runner.executorMap": {
"c": " cd $dir && gcc $fileName -D LOCAL -o ",
"cpp": " cd $dir && g++ $fileName -D LOCAL -o ",
},
2.配置mingw64运行:
在task.json的args加入-D LOCAL,之后效果大概如下:
{
"version": "2.0.0",
"tasks": [
{
"label": "Build",
"command": "g++",
"args": [
"-g",
"-std=c++17",
"-lm",
"${file}",
"-o",
"-D LOCAL"
],
"type": "shell",
"presentation": {
"reveal": "silent",
"echo": false,
"focus": false
},
"problemMatcher": {
"owner": "cpp",
"fileLocation": "absolute",
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
},
]
}
3.在程序main函数开头加入以下代码:
#ifdef LOCAL
freopen("E:\\Cpp\\1.in", "r", stdin);
freopen("E:\\Cpp\\1.out", "w", stdout);
#endif
每次输出会把1.out之前的内容覆盖。
如果不想覆盖,只需要把freopen(“E:\Cpp\1.out”, “w”, stdout)的参数w(write)改为a(add):
这样就可以实现只在本地重定向了,以后再也不怕提交到OJ时忘记注释freopen了(^_^)。
这个牵扯到VSCode的$fileNameWithoutExt宏,如果想深入了解,可参考官方文档。
示例exe生成路径:E:\Cpp
1.用Code Runner运行:
在settings.json刚才基础上中加入E:/Cpp/exe/$fileNameWithoutExt.exe && E:/Cpp/exe/$fileNameWithoutExt.exe",
"code-runner.executorMap": {
"cpp": " cd $dir && g++ $fileName -D LOCAL -o E:/Cpp/exe/$fileNameWithoutExt.exe && E:/Cpp/exe/$fileNameWithoutExt.exe",
},
2.配置mingw64运行:
在tasks.json刚才基础上中加入E:/Cpp/exe/$fileNameWithoutExt.exe && E:/Cpp/exe/$fileNameWithoutExt.exe",
{
"version": "2.0.0",
"tasks": [
{
"label": "Build",
"command": "g++",
"args": [
"-g",
"-std=c++17",
"-lm",
"${file}",
"-o",
"${workspaceFolder}/exe/${fileBasenameNoExtension}.exe",
"-D LOCAL"
],
"type": "shell",
"presentation": {
"reveal": "silent",
"echo": false,
"focus": false
},
"problemMatcher": {
"owner": "cpp",
"fileLocation": "absolute",
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
},
]
}
存代码的文件夹再也不会充满.exe了。
注:若想自定义路径,替换文中所有E:/Cpp/即可