简体   繁体   中英

Trying creating and writing into a txt file in C++

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();
}

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<<;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM