M
Mike Copeland
I am trying to convert some applications to be "more C++" and use
safer I/o. The code below works, but it poses an annoyance: the last
record "read" from the input file has no content. Therefore, I must
include the "if(line.length())" code before writing any data to the
output file, because failing to do that adds a zero-length record to the
end of the output file.
What am I missing in this logic that would avoid adding the code?
TIA
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
string line;
fstream f1, f2;
f1.open("pat12.n00", fstream::in);
if(f1.is_open())
{
f2.open("test.txt", fstream:ut);
do
{
getline(f1, line);
if(line.length()) f2 << line << endl;
} while(!f1.eof());
f1.close();
f2.close();
}
else cout << "Unable to open file";
return 0;
}
safer I/o. The code below works, but it poses an annoyance: the last
record "read" from the input file has no content. Therefore, I must
include the "if(line.length())" code before writing any data to the
output file, because failing to do that adds a zero-length record to the
end of the output file.
What am I missing in this logic that would avoid adding the code?
TIA
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
string line;
fstream f1, f2;
f1.open("pat12.n00", fstream::in);
if(f1.is_open())
{
f2.open("test.txt", fstream:ut);
do
{
getline(f1, line);
if(line.length()) f2 << line << endl;
} while(!f1.eof());
f1.close();
f2.close();
}
else cout << "Unable to open file";
return 0;
}