【发布时间】:2022-12-09 00:16:28
【问题描述】:
在我的main.cpp:
#include <cstdio>
#include "hashtable.h"
int main() {
printf("1hello");
freopen("2.txt", "w", stdout);
printf("2hello");
freopen("1.txt", "r", stdin);
printf("3hello");
int type;
char buffer[1000];int data;
hashtable table(10000, new naive_hashing(), new linear_probe());
while (true) {
scanf("%d", &type);
if (type == 0) {
scanf("%s", buffer);scanf("%d", &data);
table.insert(hash_entry(buffer, data));
}
else if (type == 1) {
scanf("%s", buffer);
printf("%d\n", table.query(buffer));
}
else break;
}
return 0;
}
1.txt:
0 dhu_try 3039
0 shirin 3024
0 SiamakDeCode 2647
0 huanghansheng 233
1 dhu
1 dhu_try
1 shirin
1 siamakdecode0
1 huanghansheng
2
output:
1hello
如您所见,程序在进入第一个 freopen 函数后暂停了。我已经检查了文档,但仍然找不到它停止运行的原因。谁能帮帮我吗? :恳求脸:
【问题讨论】:
-
知道
freopen()的人可能会出现,但为什么不使用<fstream>呢?或者<iostream>就此而言?我根本不相信这是一个 C++ 问题/程序。 -
执行程序后,您在
2.txt中看到了什么?我希望freopen("2.txt", "w", stdout);之后的所有printf输出都在该文件中。 -
我很好奇:作为学习 C++ 的一部分,您究竟是如何知道
freopen和scanf的?上次类似的事情是这里的主题,它被确定为坏知识的来源是一个不称职的 C++ 讲师。你在同一条船上吗?
标签: c++ debugging freopen cstdio