VSCode C/C++编程实现重定向+指定exe生成位置

本文详细介绍了如何在VSCode中使用C/C++进行输入输出重定向,通过修改settings.json和task.json配置,实现本地重定向及避免在线评测系统(OJ)提交错误。同时,讲解了如何指定.exe文件的生成位置,保持工作目录整洁。

VSCode C/C++编程实现重定向+指定exe生成位置

输入输出重定向

示例路径: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了(^_^)。

指定exe生成位置

这个牵扯到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/即可

评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值