468,457 Members | 1,654 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

Copying entire file, line by line into another file

Hi, I've written the following code to copy one files contents line by
line into another file. However I'm a bit dubious about using the
infinite loop to do this.

ifstream infile("File1.txt");
ofstream outfile("File2.txt");
string line = "";

while (true)
{
getline(infile,line);
//Process and edit line
outfile << line;
if (!infile.good())
break;
outfile << '\n';
}

The reason I'm not just doing

while (!infile.eof())
{
getline(infile,line);
outfile << line << '\n';
}

or

while (getline(infile,line))
{
outfile << line << '\n';
}

Is because it results in having an extra '\n' at the end of my outout
file. Perhaps you guys could let me know the correct way of achieving
what I'm trying to do.

Aug 5 '06 #1
8 9531
Jared Wiltshire wrote:
Hi, I've written the following code to copy one files contents line by
line into another file. However I'm a bit dubious about using the
infinite loop to do this.

ifstream infile("File1.txt");
ofstream outfile("File2.txt");
string line = "";

while (true)
{
getline(infile,line);
//Process and edit line
outfile << line;
if (!infile.good())
break;
outfile << '\n';
}

The reason I'm not just doing

while (!infile.eof())
{
getline(infile,line);
outfile << line << '\n';
}

or

while (getline(infile,line))
{
outfile << line << '\n';
}

Is because it results in having an extra '\n' at the end of my outout
file. Perhaps you guys could let me know the correct way of achieving
what I'm trying to do.
I think to copy a file I would try something along these lines:

#include <fstream>

void copy_file(const std::string & in, const std::string & out)
{
std::ifstream in_file(in.c_str(), std::ios::binary) ;
std::ofstream out_file(out.c_str(), std::ios::binary) ;

out_file << in_file.rdbuf() ;
}

--
Alan Johnson
Aug 5 '06 #2
Alan Johnson wrote:
Jared Wiltshire wrote:
Hi, I've written the following code to copy one files contents line by
line into another file. However I'm a bit dubious about using the
infinite loop to do this.

ifstream infile("File1.txt");
ofstream outfile("File2.txt");
string line = "";

while (true)
{
getline(infile,line);
//Process and edit line
outfile << line;
if (!infile.good())
break;
outfile << '\n';
}

The reason I'm not just doing

while (!infile.eof())
{
getline(infile,line);
outfile << line << '\n';
}

or

while (getline(infile,line))
{
outfile << line << '\n';
}

Is because it results in having an extra '\n' at the end of my outout
file. Perhaps you guys could let me know the correct way of achieving
what I'm trying to do.

I think to copy a file I would try something along these lines:

#include <fstream>

void copy_file(const std::string & in, const std::string & out)
{
std::ifstream in_file(in.c_str(), std::ios::binary) ;
std::ofstream out_file(out.c_str(), std::ios::binary) ;

out_file << in_file.rdbuf() ;
}

--
Alan Johnson
Thanks, but I'm trying to copy the file line by line. The reason for
this is because I need to edit certain lines before I write them to the
new file.

Aug 5 '06 #3
Jared Wiltshire wrote:
Hi, I've written the following code to copy one files contents line by
line into another file. However I'm a bit dubious about using the
infinite loop to do this.

ifstream infile("File1.txt");
ofstream outfile("File2.txt");
string line = "";
The reason I'm not just doing

while (!infile.eof())
{
getline(infile,line);
outfile << line << '\n';
}
This will output an extra line.
or

while (getline(infile,line))
{
outfile << line << '\n';
}
This will not.

--
Ian Collins.
Aug 5 '06 #4
hi,
you can use this code:

while (!infile.eof())
{
getline(infile,line);
outfile << line;
if(!infile.eof())
outfile << '\n';
}

Or

while (!infile.eof())
{
getline(infile,line);
outfile << line << ( infile.eof() ? '' : '\n' );
}

Ian Collins wrote:
Jared Wiltshire wrote:
Hi, I've written the following code to copy one files contents line by
line into another file. However I'm a bit dubious about using the
infinite loop to do this.

ifstream infile("File1.txt");
ofstream outfile("File2.txt");
string line = "";
The reason I'm not just doing

while (!infile.eof())
{
getline(infile,line);
outfile << line << '\n';
}
This will output an extra line.
or

while (getline(infile,line))
{
outfile << line << '\n';
}
This will not.

--
Ian Collins.
Aug 5 '06 #5
keyvan wrote:
hi,
you can use this code:

while (!infile.eof())
{
getline(infile,line);
outfile << line;
if(!infile.eof())
outfile << '\n';
}

Or

while (!infile.eof())
{
getline(infile,line);
outfile << line << ( infile.eof() ? '' : '\n' );
}
1. Don't top-post.

2. You code is incorrect. See FAQ 15.5
(http://www.parashift.com/c++-faq-lit....html#faq-15.5) for
details.
Aug 5 '06 #6
Ian Collins wrote:
Jared Wiltshire wrote:
Hi, I've written the following code to copy one files contents line by
line into another file. However I'm a bit dubious about using the
infinite loop to do this.

ifstream infile("File1.txt");
ofstream outfile("File2.txt");
string line = "";
The reason I'm not just doing

while (!infile.eof())
{
getline(infile,line);
outfile << line << '\n';
}
This will output an extra line.
or

while (getline(infile,line))
{
outfile << line << '\n';
}
This will not.

--
Ian Collins.
Try it when the input has a character return/line feed at the end of
the last line.

Aug 7 '06 #7
keyvan wrote:
hi,
you can use this code:

while (!infile.eof())
{
getline(infile,line);
outfile << line;
if(!infile.eof())
outfile << '\n';
}

Or

while (!infile.eof())
{
getline(infile,line);
outfile << line << ( infile.eof() ? '' : '\n' );
}

Ian Collins wrote:
Jared Wiltshire wrote:
Hi, I've written the following code to copy one files contents line by
line into another file. However I'm a bit dubious about using the
infinite loop to do this.
>
ifstream infile("File1.txt");
ofstream outfile("File2.txt");
string line = "";
>
>
The reason I'm not just doing
>
while (!infile.eof())
{
getline(infile,line);
outfile << line << '\n';
}
>
This will output an extra line.
or
>
while (getline(infile,line))
{
outfile << line << '\n';
}
>
This will not.

--
Ian Collins.
I was trying to avoid multiple conditional statements. I'll probally
just end up using the form that Ian Collins referred to. The file
probally should end in a character return/line feed anyway.

By the way if its rude to "double post" let me know, I dont have much
idea about newsgroups (I'm using Google Groups).

Aug 7 '06 #8
Jared Wiltshire wrote:
Ian Collins wrote:
>>Jared Wiltshire wrote:
>>>
while (getline(infile,line))
{
outfile << line << '\n';
}

This will not.

Try it when the input has a character return/line feed at the end of
the last line.
Please trim signatures in your responses.

getline strips the line terminator.

--
Ian Collins.
Aug 7 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Robert Tarantino | last post: by
3 posts views Thread by Son KwonNam | last post: by
17 posts views Thread by Michael | last post: by
15 posts views Thread by Shuch | last post: by
18 posts views Thread by mike3 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.