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