473,394 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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
Jul 22 '05 #1
6 4352
Just a guess but are you sure the input file does not have a blank line
at the end?
Jul 22 '05 #2
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
Jul 22 '05 #3
mary 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?


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


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #4
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-

Jul 22 '05 #5

"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)
Jul 22 '05 #6
<mary> wrote in message news:mp********************************@4ax.com...
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.
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: tornado | last post by:
Hi all, I am pretty new to PHP. I was reading PHP manual and trying out the example from 2nd chapter (A simple Tutorial). When i try to print the variable as given in the example it returns...
18
by: Piotr Wolski | last post by:
i read the filr into an array using file(). some of the lines are empty. i would like to output the file but ignoring the emnpty lines. any ideas?
18
by: Tjerk Wolterink | last post by:
i have the following rule, <xsl:template match="br"> <br/> </xsl:template> This should convert all <br/> to <br/> but, my transformer transforms it all to
17
by: Pushkar Pradhan | last post by:
I want to time my matrix multiply code (in MFLOPS). I want to run the code 100,000 times or some other big number. This can be done 2 ways (for and while loops): timer1 = time(NULL); for(n = 0;...
2
by: ricky | last post by:
Can anybody help with the function to get rid of extra characters in the file. I want to remove the string from the file.So i read from input file and pass the string say "john" if found dnt write...
4
by: dt | last post by:
Greetings, I'm having some trouble understanding the plethora of options available for GNU indent for my C code. Except for the function's opening {, I want all code to line up under the...
7
by: walterbyrd | last post by:
Python's lack of an EOF character is giving me a hard time. I've tried: ----- s = f.readline() while s: .. .. s = f.readline()
2
by: GS | last post by:
How can one avoid capturing leading empty or blank lines? the data I deal with look like this "will be paid on the dates you specified. xyz supplier amount: $100.52 when: September 07,...
0
by: jebbyleezer | last post by:
Hello, I have source code that builds correctly, however, after the program terminates the output file produced is empty. Here is my source code: import java.io.*; import java.util.Scanner;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.