-1

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;
}
lordvidex
  • 3,581
  • 3
  • 17
  • 25
  • 1
    Is there any reason why you're putting yourself through this pain in the first place? What you are doing makes it annoying to insert debug outputs and other helpful diagnostics. You can open input.txt and output.txt like plain-old regular file streams and operate on those streams. – user4581301 Mar 11 '20 at 22:18
  • Unrelated: using images of code [is usually a bad idea](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question) for many reasons. [So is `#include `](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). – user4581301 Mar 11 '20 at 22:21
  • 2
    Out of interest though, what sort of hackery did you need to do to get Visual Studio to accept `#include `? Roll your own? Lift the file from GCC? – user4581301 Mar 11 '20 at 22:23
  • MSVC gives warnings for valid code to try and lure you into using their proprietary stuff – M.M Mar 11 '20 at 23:41
  • 4
    All questions here must have all relevant information ***in the question itself as plain text***. Links can stop working at any time making questions meaningless. Code, data, or errors, shown as images cannot be copy/pasted; or edited or compiled for further research and investigation. This question must be [edit]ed, and all links and images removed and replaced with all relevant information, as plain text. All code must meet all requirements of a [mre]. You can find many other questions here that include everything as plain text, and I see no reason why this question one can't, either. – Sam Varshavchik Mar 11 '20 at 23:48
  • I plan to participate in a coding contest and for the first time, input and output file submission is required as none of my practice has ever required that – lordvidex Mar 12 '20 at 12:42
  • I have added the code in plain text @SamVarshavchik – lordvidex Mar 12 '20 at 12:42

2 Answers2

3
  • First of all add this to your code
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<stdlib.h>
  • then add input.in and output.out to your project
  • then right click in solution explorer and select
    • Properties
      • Configurations
        • C/C++
          • Preprocessor then edit Preprocessor Definitions and change it to_CRT_SECURE_NO_DEPRECATE;%(PreprocessorDefinitions)

Steps

MahmouD Skafi
  • 370
  • 3
  • 15
2

Just use freopen_s or go to Project->Properties->Configuration Properties->C/C++->Preprocessor->Preprocessor Definitions and add _CRT_SECURE_NO_WARNINGS

Example:

FILE *input;

errno_t e = freopen_s(&input, "input.txt", "w", stdin);
if(e)
    /* Handle that error(cannot reopen) */;

...

fclose(input);
mylibh
  • 153
  • 9
  • 2
    A note: *This function or variable may be unsafe* warnings are Visual Studio's compiler warning you that the function can fail hilariously if used in correctly and to recommend that you use a (formerly Microsoft-specific) alternative that cleans up some of the failure points. GCC-based compilers assume you know what you're doing and used the function correctly and do not warn you. – user4581301 Mar 11 '20 at 22:42