1

Code does not create .txt files to output

Updated: this code is working on friend's PC, but on on my PC. We both installed same mingw file. Both have Windows 11, both tried on VS code and Far Manager

Hi. Here is following code:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
using namespace std;
int main() {
 freopen("input.txt","r",stdin);
 int a,b;
 cin >> a >> b;
 cout << a + b << endl;
 freopen("output.txt","w",stdout);
 cout << a + b << endl;
}

Code succesfully read integers a,b from file input.txt and outputs them in console, but freopen it do nothing. I tried ofstream fout("output.txt") also and it did not work. I tried this in Visual Studio Code with Code Runner and Far Manager with than line on compilation used on both: g++ --std=c++17 !.! -DLOCAL -O2 -Wall -Wl,--stack=67108864 -o !.exe

For VS code I tried debug cwd (current working directory) and it was correct, I also checked .json files such as tasks.json and launch.json with it's cwd.

I have tried this code on Visual Studio (not VS code) and it worked! Maybe, the problem is in Windows 11? No antivirus programs on PC, no Windows Defender, no virtual machine, no windows 11 UAC.

Tried this code to get an error, but it seems like no errors occured.

 FILE* file = freopen("output.txt", "w", stdout);
 if (file == NULL) {
 perror("Error opening file\n");
 printf("errno: %d, description: %s\n", errno, strerror(errno));
 }
 else {
 printf("File reopened successfully.\n");
 }
asked Jan 12, 2025 at 15:00
14
  • 6
    Why do you think you should use freopen (and std::cin, std::cout)? Use std::ofstream learncpp.com : 28.6 — Basic file I/O Commented Jan 12, 2025 at 15:04
  • 3
    Recommendation : fully switch to "visual studio community edition" it is much more easy to use than "visual studio code" Commented Jan 12, 2025 at 15:06
  • ofstream do not work either. Visual Studio is not available on ICPC World Finals docs.icpc.global/worldfinals-programming-environment Commented Jan 12, 2025 at 15:11
  • 1
    First of all, always check for failure. In your case check what freopen returns. If it fails, use strerror to get a printable string of the error from errno. And to emphasize what others have said, use standard C++ facilities for all your file input and output. Commented Jan 12, 2025 at 15:14
  • 1
    ofstream should work, but be sure you set the "working directory" of your final directory to a place where you have rights to write files. Considering the link you sent : make sure you practice on Linux (so you are familiar with the linux file system behavior too). Commented Jan 12, 2025 at 15:31

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.