Home c++ fstream C++ doesn't work

fstream C++ doesn’t work

Author

Date

Category

I study working with files in C++
Did absolutely everything as in the example. The message Good !, is displayed in the console, but the file is not created, and if you manually create the file, then nothing is written there. What could be the problem?

# include "pch.h"
#include & lt; iostream & gt;
#include & lt; fstream & gt;
#include & lt; string & gt;
using namespace std;
  int main ()
  {
    string path = "piin.txt";
    ofstream fout;
    fout.open (path);
    if (! fout.is_open ())
    {
      cout & lt; & lt; "Bad!";
    }
    else
    {
      cout & lt; & lt; "Good!";
      fout & lt; & lt; "My data";
    }
    fout.close ();
  }

Answer 1, authority 100%

Most likely you are looking for the created file in the wrong directory 🙂
I don’t remember exactly what the VS environment makes the current directory when running – so either look carefully in the project subdirectories, or specify the exact full name of the file – like "D: \\ Programs \\ Project \\ pii.txt "(don’t forget the double backslashes).


Answer 2

I work in Visual Studio, the file was created and edited in the root folder of the project! In case of a similar situation, check there

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions