L
lovecreatesbea...
The condition at line 31 is added to check if the program finished to
read the whole file. Is it needed and correct? Thank you.
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int read(string filename, int last_pos)
{
ifstream file;
int size = 0;
//read continue after the last read position
static int offset = last_pos + 1;
file.open(filename.c_str(), ios::in);
if(!file)
{
cerr << "failed to open file: " << filename << endl;
return -1;
}
file.seekg(0, ios::end);
size = file.tellg();
file.seekg(offset, ios::beg);
string line;
while (getline(file, line))
{
//...
offset = file.tellg();
}
//check if it finished to read the whole file
if (offset != size) // line 31
{
file.clear();
read(filename, offset);
}
return 0;
}
read the whole file. Is it needed and correct? Thank you.
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int read(string filename, int last_pos)
{
ifstream file;
int size = 0;
//read continue after the last read position
static int offset = last_pos + 1;
file.open(filename.c_str(), ios::in);
if(!file)
{
cerr << "failed to open file: " << filename << endl;
return -1;
}
file.seekg(0, ios::end);
size = file.tellg();
file.seekg(offset, ios::beg);
string line;
while (getline(file, line))
{
//...
offset = file.tellg();
}
//check if it finished to read the whole file
if (offset != size) // line 31
{
file.clear();
read(filename, offset);
}
return 0;
}