我正在为学校学习C++。我正在使用Visual Studio编写C++,我正在尝试编译以下代码:
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
freopen("cd.inp", "r", stdin);
freopen("cd.out", "w", stdout);
int a, b;
while (true){
cin >> a >> b;
cout << a << ' ' << b;
break;
}
fclose(stdin);
fclose(stdout);
}
我有这些错误:Severity Code Description Project File Line Suppression State Error C4996 'freopen': This function or variable may be unsafe. Consider using freopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. clock_degrees F:\CODING\C++\clock_degrees\clock_degrees\clock_degrees.cpp 11
Severity Code Description Project File Line Suppression State Warning C6031 Return value ignored: 'freopen'. clock_degrees F:\CODING\C++\clock_degrees\clock_degrees\clock_degrees.cpp 11
我现在该怎么办?
我也遇到了有关 scanf 和 printf 的问题,而不是 freopen 的问题。我以为问题来自
最佳答案
您应该改为使用ifstream
和ofstream
:
#include <fstream>
// don't do using namespace std;
using std::ifstream;
using std::ofstream;
using std::ios_base;
int main() {
ifstream in;
ofstream out;
in.exceptions(ios_base::failbit | ios_base::badbit);
out.exceptions(ios_base::failbit | ios_base::badbit);
try {
in.open("cd.inp");
int a, b;
// The while loop is pointless
in >> a >> b;
out.open("cd.out");
out << a << ' ' << b;
} catch (ios_base::failure const& e) {
// Error handling
return -1;
}
}
关于c++ - 我对c++有关<stdio.h>库有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63871819/