【问题标题】:Not getting output using freopen in c++ :-vs code showing在 C++ 中使用 freopen 没有得到输出:-vs 代码显示
【发布时间】:2021-05-03 04:10:32
【问题描述】:
#include <iostream>
using namespace std;
int main() {
#ifdef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int n;
    cin >> n;
    cout << n + 1;
    return 0;
}

在 vs 代码中使用 freopen 输入和输出我的程序在 c++ 中的结果时,我没有在 ouput.txt 文件中得到输出。例如:我创建了一个程序来输入编号。 n 和输出 n+1。 但是在输入中输入 24 时,我在 ouput.txt 中得到 32765,但在我的终端中,我得到 25 就是这样。通过一次又一次地编译程序,32765 变为 32767 等等。

【问题讨论】:

  • 使用相同的代码在 ouput.txt 中没有得到输出
  • @162_NiteshSingh 你需要创建一个名为 input.txt 的文件,然后输入任何 int。
  • 应该#ifdef ONLINE_JUDGE#ifndef ONLINE_JUDGE
  • 您是否尝试过从freopen() 打印错误(如果有)?
  • @coderx64 input.txt 工作但 ouput.txt 不工作

标签: c++ freopen


【解决方案1】:

以这种方式读取文件时,请确保您引用的文件在终端的当前工作目录中。


在以下文件夹结构中

ParentFolder
├── Folder1
│   ├── code
│   ├── code.cpp
│   └── input.txt
└── Folder2
    └── input.txt

如果您当前的工作目录是Folder2,那么使用的输入文件将是ParentFolder/Folder2/input.txt 即使您的代码和可执行文件位于同一文件夹中的输入文件,因为相对路径是使用终端工作来决定的目录。

当然,如果文件不存在/无法打开,您将获得抓取编号。

【讨论】:

    猜你喜欢
    • 2022-01-03
    • 1970-01-01
    • 2022-01-25
    • 2020-11-10
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 2022-08-18
    相关资源
    最近更新 更多