c++visual-c++codeblocksfreopen

why is freopen() not working on Microsoft Visual Studio but working on CodeBlocks?


I started C++ not so long and searched so hard for different ways to read and write from/to files with no result until i tried it out on CodeBlocks which worked.. Images are attached below to point out possible errors in code though the same code were used on both applications.

Error Code: Severity Code Description Project File Line Suppression State 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. Codeforces C:\Users\owamoyo\source\repos\Codeforces\Codeforces.cpp 6

Code Blocks

#include<bits/stdc++.h>

using namespace std;

int main() {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int n; cin >> n;
    while (n--) {
        int x; cin >> x;
        cout << x << " ";
    }
    return 0;
}

MS Visual Studio

#include<bits/stdc++.h>

using namespace std;

int main() {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int n; cin >> n;
    while (n--) {
        int x; cin >> x;
        cout << x << " ";
    }
    return 0;
}

Solution

  • #define _CRT_SECURE_NO_DEPRECATE
    #include<stdio.h>
    #include<stdlib.h>
    

    Steps