sublime text 不要在输出文件中写入任何内容

问题描述 投票:0回答:1

如果我的代码很简单,例如:

#include <bits/stdc++.h>
using namespace std;
int main(){
    cout<<"hi";
    return 0;
}

然后它可以在我的.out文件中写入hi。但如果我的代码变得稍微复杂一点,它就会显示

【2.9秒完成】

无需在我的文件中写入任何输出。

这是我的 sublime-build 文件:

{
    "cmd": ["g++.exe", "-std=c++17", "${file}",
            "-o", "${file_base_name}.exe",
            "&&", "${file_base_name}.exe<input.inp>output.out"],
    "shell":true,
    "working_dir":"$file_path",
    "selector":"source.cpp"
}

我尝试重置,但我不知道这件事。

c++ output sublimetext3
1个回答
0
投票

根据提供的信息,问题似乎可能与 sublime-build 文件中如何处理输入和输出重定向有关。当您使用输入和输出重定向运行 C++ 程序时,它可能会等待 input.inp 文件出现,然后再执行程序并将输出写入 output.out。

我不知道这是否可以解决这个问题,但你可以扔掉重定向部分:

{
    "cmd": ["g++.exe", "-std=c++17", "${file}", "-o", "${file_base_name}.exe", "&&", "${file_base_name}.exe"],
    "shell": true,
    "working_dir": "$file_path",
    "selector": "source.cpp"
}

cpp 文件需要这些:

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    // Redirect input from input.inp
    ifstream cin("input.inp");
    if (!cin) {
        cerr << "Error opening input file!" << endl;
        return 1;
    }

    // Redirect output to output.out
    ofstream cout("output.out");
    if (!cout) {
        cerr << "Error opening output file!" << endl;
        return 1;
    }

    // Your main code here
    cout << "hi" << endl;

    return 0;
}
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.