ofstream creating file but not writing to it in C++
Solution 1
commenting out the redundant "open" solves it.
#include <iostream>
#include <fstream>
int main()
{
// in this function, we want to export the items to a text file
std::ofstream myfile("TodayTime.txt");
// myfile.open("TodayTime.txt");
if (myfile.is_open())
{
myfile << "The average call time is ";
myfile.flush();
myfile.close();
}
else
{
std::cerr << "didn't write" << std::endl;
}
}
I strongly suspect that you're invoking undefined behaviour by opening and already-open stream.
Solution 2
Here is the explanation:
- The call to
myfile.open("TodayTime.txt");
will fail because the stream is already associated with the file, setting the failbit. - The call to
is_open()
will succeed, as the file is open. - Then the call to streaming operator
<<
will fail (because of the failbit).
Samuel Ohrenberg
Updated on August 09, 2022Comments
-
Samuel Ohrenberg over 1 year
I am writing an MFC program that has a dialog with an "Export" button that will take all of the data that has been entered into the file and export it to a .txt (at some point I want to change this to a .msg file...but that's a question for another day).
However, when I click the button, it creates the file but doesn't write anything inside the file. For testing, I removed everything except just a simple literal string and even that isn't printing. Here is the current code for that event: The myfile.flush() statement is leftover from when I had a loop that I was trying to print to the file.
void CEHDAHTTimerDlg::OnBnClickedExport() { // in this function, we want to export the items to a text file std::ofstream myfile("TodayTime.txt"); myfile.open("TodayTime.txt"); if (myfile.is_open()) { myfile << "The average call time is "; myfile.flush(); myfile.close(); } else { SetDlgItemText(IDC_EXPORT, L"Export Unsuccessful! -- No File"); } }
Is there anything you all can think of that could be causing this? I've been at it for a few hours trying different things, like utilizing a myfile.write() function instead. I've searched a lot around here, reddit, and google in general trying to find out why this isn't writing.
I appreciate your help.
EDIT:
Okay, calling the myfile constructor the way that I did, by including the file name, went ahead and did what open file would have done
Thanks for your help!
-
Richard Hodges almost 8 years@SamuelOhrenberg :) it usually is then you know what to look for. The standard library is extremely minimalist and expects us to fulfil all preconditions. It doesn't perform redundant checks as a rule. You'll get used to it, and learn to like the performance and elegance that is achieved as a result.
-
Richard Hodges almost 8 years@SamuelOhrenberg re your edit. it's not that you don't have to open the file. It's that you opened the file when you used the constructor that takes a filename. If you had used the default constructor, you would have had to subsequently open the file.
-
Samuel Ohrenberg almost 8 yearsOooooh, I get it. So the default constructor just creates an instance of "myfile", but it has an option to include the location of the file and open it automatically
-
Richard Hodges almost 8 years@SamuelOhrenberg the ofstream is (as it's name suggests) a stream. A layer over the actual file handle. The constructor that takes a filename implicitly calls open() as part of the constructor. The destructor will automatically flush the stream and close the underlying file, if it's open. cppreference.com is a good place to read about these things. en.cppreference.com/w/cpp/io/basic_ofstream
-
Vlad Feinstein almost 8 yearsThere is no
undefined behaviour
here: the call toopen()
will fail because the stream is already associated with the file, setting the failbit. The call tois_open()
will succeed, as the file is open. Then the call to streaming operator<<
will fail (because of the failbit) - but who is checking? :) -
Richard Hodges almost 8 years@VladFeinstein if you make this an answer I'll upvote it.
-
quanta about 3 yearsThanks. Then, what is the solution?
-
Vlad Feinstein about 3 years@quanta don't open the file twice :)