How Do We Avoid the Extra Empty Line at the End of the Output File?

M

mary

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?

Thanks!

mary
 
P

Phil Staite

Just a guess but are you sure the input file does not have a blank line
at the end?
 
U

Ulrich Achleitner

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.
 
I

Ioannis Vranos

mary said:
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?



Perhaps in the style:


if (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();
 
S

shez

You can also try this:

out << in.rdbuf();

That's all you need to copy a file (I assume that's what you're trying
to do).

-shez-
 
C

Computer Whizz

Ulrich Achleitner said:
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.

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.
 
A

Andrew Koenig

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".

Really? I don't see why. It looks to me that the output will be identical
to the input unless the input file flouts convention by not ending with a
newline character.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top