gpt4 book ai didi

c++ - 我对c++有关库有问题吗?

转载 作者:行者123 更新时间:2023-12-02 10:56:08 25 4
gpt4 key购买 nike

我正在为学校学习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 StateError 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 StateWarning C6031 Return value ignored: 'freopen'. clock_degrees F:\CODING\C++\clock_degrees\clock_degrees\clock_degrees.cpp 11


我现在该怎么办?
我也遇到了有关 scanf printf 的问题,而不是 freopen 的问题。我以为问题来自 库。

最佳答案

您应该改为使用ifstreamofstream:

#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;
}
}
  • Why is “using namespace std;” considered bad practice?
  • 关于c++ - 我对c++有关<stdio.h>库有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63871819/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com