【问题标题】:freopen and cout creating internal errorfreopen 和 cout 创建内部错误
【发布时间】:2018-01-16 10:38:35
【问题描述】:

我正在尝试读取和写入一些文件,但每次我尝试将 std::cout 某些内容添加到我的 output.out 文件时,我都会收到“错误 C1001,编译器中发生内部错误”。

为什么?

(我在预处理器定义中使用了_CRT_SECURE_NO_WARNINGS,以便能够使用freopen())

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <deque>
#include <array>
#include <vector>

freopen("C:\\somepath\\input.in", "r", stdin);
freopen("C:\\somepath\\output.out", "w", stdout);

int t;
std::cin >> t;
std::cout << t << std::endl;
for (int i = 0; i < t; i++)
{
    int n;
    std::cin >> n;
    std::cout << t << std::endl;
    std::vector <int> x, h;
    x.resize(n);
    h.resize(n);
    for(int j=0;j<n;j++)
    {
        std::cin >> x[j] >> h[j];
        std::cout<< x[j] << h[j] << std::endl;
    }
}

编辑:正如 Nighteen 所说,我的代码中有一些错误(n++,没有调整矢量大小,现已更正) 但是该错误仍然以某种方式存在: 代码正在此状态下编译,但是一旦我尝试输入 cout 字符串,就会出现相同的问题 比如在我的std::cout&lt;&lt; x[j] &lt;&lt; h[j] &lt;&lt; std::endl;中添加&lt;&lt;" "

in the std::cout<< x[j] <<" "<< h[j] << std::endl;

【问题讨论】:

  • 将你的代码放入main函数中。
  • 我有一个主函数,我只是没有粘贴它
  • 你能给我们一个你想要的例子 input.in 和对应的 output.out 吗?
  • codechef.com/problems/DEFOREST 该页面中有输入/输出示例^^

标签: c++ compiler-errors cout freopen c1001


【解决方案1】:

假设您将代码放在主块中,该代码在 MSVC 15.5.2 中编译得很好。证明here。

即使编译没问题,代码似乎也没有按应有的方式运行。首先,你不能std::cin &gt;&gt; x[j] &gt;&gt; h[j];。创建一个临时变量来存储输入,然后将其推回向量中:

int input1, input2;
std::cin >> input1 >> input2;

x.push_back(input1);
h.push_back(input2);

您还应该注意这个循环for (int j = 0; j &lt; n; n++) 永远不会结束。变量j 应该增加,而不是n。无论你想达到什么目标,这似乎都不是办法。

【讨论】:

  • 谢谢,但是当我尝试 cout 任何字符串时,错误仍然存​​在! (看编辑)
【解决方案2】:

std::cout 是用于输出到控制台的 std::ostream。对于输出到文件,使用了 std::ifstream 和 std::ofstream 类。使用以下语法:

std::ifstream [ifstream_name](constructor parameters);
[ifstream_name] >> [container to store file contents];

std::ofstream [ofstream name](constructor parameters);
[ofstream_name] << [contents to write to file];

【讨论】:

  • 我正在尝试在 codechef 上发帖,if 和 ofstream 不可用,我只有 freopen。
猜你喜欢
  • 1970-01-01
  • 2016-12-05
  • 2013-06-11
  • 1970-01-01
  • 1970-01-01
  • 2014-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多