0

I'm learning C++ for my school. I' m using Visual studio to code C++, I'm trying to compile this code:


#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);
}

I've got these errors:

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

What should I do now? I've got some problems about scanf and printf instead of freopen too. I thought the problems'd come from <stdio.h> library.

Huy Mụn
  • 1
  • 1
  • 3
    What you should do now, is actually use C++ instead of C. `freopen` is a C library function, and it is not always specified how C library's standard input/output functions have any bearing on `std::cin` and `std::cout`. Even if you address this compiler diagnostic, this may or may not work. You should look up how to use `std::ifstream` and `std::ofstream`, in order to open streams that read or write from files, and use that instead of `std::cin` and `std::cout`, with the underlying file streams hijacked via `freopen`. P.S. `scanf` and `printf` are also C. – Sam Varshavchik Sep 13 '20 at 14:22
  • 2
    You need to get examples of C++ coding from somewhere else. This is a mishmash of C and C++. It's hard enough to learn C++. – doug Sep 13 '20 at 14:41
  • 2
    Unrelated note: In C++ you should use `#include ` rather than using the deprecated "stdio.h" if you insist on using the C functions. – Jesper Juhl Sep 13 '20 at 15:32
  • cool that someone deleted my comment which by now was the only comment actually helping instead of people telling tales about how c and c++ headers don't mix... – BitTickler Sep 13 '20 at 16:15
  • Recommend you add error detection / handling at the formatted extraction. Also, 'cin' is a stream (i.e. of c++) ... freopen deals with files (c-style). – 2785528 Sep 13 '20 at 19:03

2 Answers2

0

You should use ifstream and ofstream instead:

#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;
  } 
}
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
0

I suggest that you could add #define _CRT_SECURE_NO_WARNINGS at the top. If you use freopen, the compiler will think it is not safe. So, I suggest that you could use freopen_s.

Barrnet Chou
  • 1,738
  • 1
  • 4
  • 7