"Ulrich Achleitner" <ua********@aon.at> wrote in message
news:opskua7dpgdeken5@innsbruck-neu...
On Tue, 18 Jan 2005 22:51:57 -0600, mary <unknown> wrote:
When we use
string line;
while (getline(in,line))
{
out.write(line.c_str(),line.size());
out.put('\n');
}
in.close();
out.close();
we are adding an empty line at the end of the file associated with
the stream "out". The same thing happens if we only use
out << line << endl;
to write into the file for "out".
How do we avoid that last empty line?
you need to avoid the last '\n' or endl.
imho, this is not possible without intermediate storage of the data, or
counting the non-empty lines of the input file before entering the
while-loop.
--
have a nice day
ulrich
Why not treat the first line special and then write out the subsequent lines
(as explained in Accelerated C++)...
Like so:
string line;
if (in.good) {
getline(in,line);
out.write(line.c_str(),line.size());
}
while (getline(in,line)) {
out.put('\n');
out.write(line.c_str(),line.size());
}
in.close();
out.close();
The first line is written, then all subsequent lines are added with a new
line at the beginning - therefore there's no trailing new line... You learn
this pretty early on in Accelerated C++.
I am a newbie, so I've probably got a few things "not correct", but I do
think treating the first line special, and then all remaining lines is the
way to go.
--
=========
Comp Whizz
=========
(The C++ beginner)