c++ - 我对c++有关<stdio.h>库有问题吗?

标签 c++ visual-studio compiler-errors stdio

我正在为学校学习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 的问题。我以为问题来自库。

最佳答案

您应该改为使用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/

    相关文章:

    c - 错误 : Array type has incomplete element type and binary operand error

    compiler-errors - CUDA 在编译期间无法识别 nvcuda 命名空间

    c++ - 迭代 Boost 属性树中的项目

    c# - 如何在 .Net Core 2.2 中正确使用 ConfigurationManager.GetSection()

    c - mainCRTStartup 的签名是什么

    visual-studio - 如何在 Visual Studio 中复制 SublimeText ctrl+D

    c++ - 如何在我的项目包中使用 c++ 库 gtsam?

    c++ - C++ Windows 中的 RAW 套接字

    c++ - 如何在不调用对象构造函数的情况下声明对象数组

    c++ - 有没有非utf8字符之类的东西