Output file is empty!!

Hi,

I am writing to a file and then close it but it remains empty!
The relevant code is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
	ofstream out{ outputFileName };

	if (!out)
	{
		ostringstream os;
		os << "Cannot open file '" << outputFileName << "'";
		throw ios_base::failure{ os.str() };
	}


	for ( auto& r = m_rows.begin(); r != m_rows.end(); ++r)
	{
		for (auto& c = r->begin(); c != r->end(); ++c)
		{
			out << *c;
			if (next(c) != r->end())
			{
				out << ",";
			}
		}
		out << endl;
	}
	out.flush();
	out.close();
}


What could be wrong??

Thanks!
Juan
You're not checking to see that the actual writes don't fail. You should do that every time you write, or else call the exceptions member function on it. You'll be able to tell better by seeing what the error state is.
Last edited on
What is m_rows? Are you sure it actually contains some data?
Also you may want to consider a couple of ranged based loops for your output section:

1
2
3
4
for(auto& rows : m_rows)
   for(auto& cols : rows)
      out << cols << ", " ;
   out << endl;


By the way you don't need either the flush() or the close() calls. The destructor will automatically call these functions when the instance goes out of scope.

Actually, I had a definition of operator<< that was "stealing" the call to the ofstream.operator<< and that's what was avoiding writing to the file!

Thanks for the help and the very sensible suggestions!

Juan
Topic archived. No new replies allowed.