我正在尝试从 C++ 中的两个单独的文本文件中读取输入和输出。
代码(test.cpp):
#include<bits/stdc++.h>
using namespace std;
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int x = 6 ;
cin >> x;
cout << x;
}
输入.txt:
1
output.txt 为空
在 VS 代码中使用终端并编写命令时:
g++ 测试.cpp
.\a.exe
读取输入文本文件中的数据并将数据写入输出文本文件。
但是如果我使用调试,输入和输出文件不会被程序识别。没有输入和输出被读/写。没有显示错误,程序刚刚结束。
启动.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": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin",
"environment": [],
"console": "externalTerminal",
"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"
}
]
}
我希望文本文件的输入/输出也能在调试时发生。