Copying entire file, line by line into another file

  • Thread starter Jared Wiltshire
  • Start date
J

Jared Wiltshire

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

Alan Johnson

Jared said:
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::eek:fstream out_file(out.c_str(), std::ios::binary) ;

out_file << in_file.rdbuf() ;
}
 
J

Jared Wiltshire

Alan said:
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::eek:fstream out_file(out.c_str(), std::ios::binary) ;

out_file << in_file.rdbuf() ;
}
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.
 
I

Ian Collins

Jared said:
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.
 
K

keyvan

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' );
}
 
J

Jared Wiltshire

keyvan said:
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' );
}

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

Ian Collins

Jared said:
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.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top