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

Discussion in 'C++' started by mary, Jan 19, 2005.

  1. mary

    mary Guest

    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
    mary, Jan 19, 2005
    #1
    1. Advertising

  2. mary

    Phil Staite Guest

    Re: How Do We Avoid the Extra Empty Line at the End of the OutputFile?

    Just a guess but are you sure the input file does not have a blank line
    at the end?
    Phil Staite, Jan 19, 2005
    #2
    1. Advertising

  3. 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
    Ulrich Achleitner, Jan 19, 2005
    #3
  4. Re: How Do We Avoid the Extra Empty Line at the End of the OutputFile?

    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
    Ioannis Vranos, Jan 19, 2005
    #4
  5. mary

    shez Guest

    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-
    shez, Jan 19, 2005
    #5
  6. "Ulrich Achleitner" <> wrote in message
    news:eek:pskua7dpgdeken5@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)
    Computer Whizz, Jan 19, 2005
    #6
  7. <mary> wrote in message news:...

    > 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.
    Andrew Koenig, Jan 19, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lukas
    Replies:
    3
    Views:
    790
    spiff
    Nov 10, 2005
  2. simon.alexandre
    Replies:
    3
    Views:
    664
    simon.alexandre
    Jan 11, 2005
  3. dt
    Replies:
    4
    Views:
    471
    CBFalconer
    Dec 31, 2006
  4. walterbyrd
    Replies:
    7
    Views:
    302
    Asun Friere
    May 17, 2007
  5. mathieu
    Replies:
    3
    Views:
    585
    Bo Persson
    Sep 4, 2009
Loading...

Share This Page