By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,179 Members | 2,192 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,179 IT Pros & Developers. It's quick & easy.

Copying entire file, line by line into another file

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.