重新打开和 cout 创建内部错误

freopen and cout creating internal error

本文关键字:内部 错误 创建 cout 新打开      更新时间:2023-10-16

我正在尝试读取和写入一些文件,但是每次尝试向output.out文件std::cout某些内容时,都会收到"错误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 ++,没有矢量大小调整,现已更正(但是错误,在某种程度上仍然存在:代码正在这种状态下编译,但是一旦我尝试输入一个字符串,就会出现同样的问题就像在我的std::cout<< x[j] << h[j] << std::endl;中添加<<" "

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

假设您将代码放在主块中,代码在 MSVC 15.5.2 中编译良好。证据在这里。

即使编译正常,代码似乎也没有按预期工作。首先,你不能std::cin >> x[j] >> h[j];.创建一个临时变量来存储输入,然后将其推送回向量:

int input1, input2;
std::cin >> input1 >> input2;
x.push_back(input1);
h.push_back(input2);

您还应该注意,这个循环for (int j = 0; j < n; n++)永无止境。变量j应该增加,而不是n。无论你想要实现什么,这似乎都不是办法。

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];