1

Basically, I'm following a simple tutorial about files handling in C++. I've been trying to create and write into a txt file at the same time, but any of the methods I've tried won't actually create a txt file in my executable location. I should also say that, I print myfile.is_open() just to know if the file truly created and opened, but I get 0 everytime with every method. What am I doing wrong ? I mainly tried to create and write to a txt file like this:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    fstream myfile;
    myfile.open("example.txt", ios::out);
    cout << myfile.is_open() << endl;
    myfile << "Writing this to a file.\n";
    myfile.close();
}
  • 4
    Tactical note: rather than `cout << myfile.is_open() << endl;` and continuing anyway if the file isn't open, us an `if` statement to report the failure and exit the program. – user4581301 Jun 03 '22 at 18:02
  • 3
    The file wouldn't necessarily get created in the same location as the executable, but rather in whatever your current working directory is when you execute the program. Perhaps this is your issue? – Chris Jun 03 '22 at 18:03
  • 2
    Always test the stream state after a IO transaction to make sure the transaction succeeded. Errors that you don't catch and handle turn into bugs, and debugging is already time consuming enough to not need more bugs. – user4581301 Jun 03 '22 at 18:04
  • The program has no control over where the working directory starts, that's controlled by whatever starts the program. This makes relative paths like `"example.txt"` not very useful. If you need to ensure the file is created in the same directory as the executable, start reading here: [Get path of executable](https://stackoverflow.com/questions/1528298/get-path-of-executable) – user4581301 Jun 03 '22 at 18:07
  • I recommend changing the filename to something more unique, then using the OS to find the file. It's a common technique I use when I don't know where the program created the file. – Thomas Matthews Jun 03 '22 at 18:08
  • If you are using C++17 or later, you can use [`std::filesystem::current_path()`](https://en.cppreference.com/w/cpp/filesystem/current_path) to determine where the current working directory is pointing (otherwise, use platform APIs, like `GetCurrentDirectory()` on Windows, etc). Also, just a nitpick - if you are using an `std::fstream` for output-only I/O, consider using `std::ofstream` instead (and `std::ifstream` for input-only I/O). And you can pass a filename and flags to the stream's constructor instead of using `open()`. – Remy Lebeau Jun 03 '22 at 18:40

1 Answers1

0

First, I bet you're using an IDE like Visual Studio. Most IDEs set your working directory somewhere other than your project directory. I don't use Visual Studio, but many of them put them in ../.

So your file is being produced, but not where you think you should find it.

If you compile and run this program without an IDE, you'll get your file where you expect it.

You may also be able to tell your IDE that the working directory should be your project directory.


Now, to keep you from making a few bad habits, I'm going to tell you two more things.

It's considered a mistake to do using namespace std. Instead, I do using statements only on those things I am going to use frequently. In your short code, I wouldn't have done any.

Next, if you're going to write out a file, it's better to use std::ofstream. It's otherwise the same code. But it's a bit clearer that you're only using the file for output.

So my version of your code:

#include <iostream>
#include <fstream>

int main()
{
    std::ofstream myfile;
    myfile.open("example.txt");
    std::cout << myfile.is_open() << std::endl;
    myfile << "Writing this to a file.\n";
    myfile.close();
}

Yeah, those std:: everywhere can be annoying, so you could do this:

#include <iostream>
#include <fstream>

using std::ofstream;
using std::cout;
using std::endl;

int main()
{
    ofstream myfile;
    myfile.open("example.txt");
    cout << myfile.is_open() << endl;
    myfile << "Writing this to a file.\n";
    myfile.close();
}

I actually have an include of CommonUsing.h that I put a few things I do almost everywhere.

#pragma once

#include <chrono>
#include <iostream>

#include <date/date.h>

//======================================================================
// The most common using statements I do in most of my code.
//======================================================================

using std::cout;
using std::cerr;
using std::endl;
using std::string;

using namespace std::chrono_literals;

using date::operator<<;
Joseph Larson
  • 8,530
  • 1
  • 19
  • 36