我正在做一个编程问题,并正在使用 freopen 重定向流。我面临的问题是在 stdout 重定向之后printf命令没有在输出文件上打印。我什至尝试使用 fflush 但没有得到任何结果。
这是我的代码
#include<iostream>
#include<vector>
#include<cmath>
#define fc(a) static_cast<float>(a)
using namespace std;
vector<int>* touch(const vector<int> arr[], int size)
{
vector<int>* touch_circles = (vector<int>*)malloc(sizeof(vector<int>)*size);
int i1, i2, dis;
for(i1 = 0; i1 < size; i1++)
for(i2 = i1+ 1; i2 < size; i2++)
{
dis = static_cast<int>(ceil(pow(pow(fc(arr[i1][0]) - fc(arr[i2][0]),2) + pow(fc(arr[i1][1]) - fc(arr[i2][1]),2),0.5)));
if (dis <= arr[i1][2] + arr[i2][2])
{
touch_circles[i1].push_back(i2);
touch_circles[i2].push_back(i1);
}
}
return touch_circles;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("D:\\C++\\input.txt","r",stdin);
freopen("D:\\C++\\output.txt","w",stdout);
freopen("D:\\C++\\output.txt","w",stderr);
#endif
int t, x, y, n;
int itr, i, i1, i2;
scanf("%d",&t);
while(t--)
{
scanf("%d %d %d", &x, &y, &n);
vector<int> arr[n];
for(itr = 0; itr < n; itr++)
{
scanf("%d %d %d", &i1, &i2, &i);
arr[itr].push_back(i1);
arr[itr].push_back(i2);
arr[itr].push_back(i);
}
//The 'fflush'es are just for trial, problem persists with or without any of them
fflush(stdout);
vector<int> *touch_list = touch(arr, n);
fflush(stdout);
printf("Expected");
fflush(stdout);
}
}
这是我的 input.txt
1
20 10
2
10 7 2
10 4 2
我的 output.txt 是空的。代码编译良好并且没有错误,它只是运行并完成,而不在输出文件上打印任何内容。一件奇怪的事情是,如果我从 main 中注释掉函数调用,输出会打印在 ouput.txt 上。我不明白为什么会发生这种情况,因为我认为函数内部没有任何东西可能会影响文件流。任何帮助表示赞赏,我现在完全没有想法!