I'm trying some basic C++ file stuff. I've created a small class with some functions to help read sections from a file. The important function in the class is the one that allows me to, starting from the end of the file, read until a null character. ("read_from_end_until_null")
class FileData
{
private:
int tail = 0;
int head = 0;
ifstream* file;
public:
FileData(std::ifstream *)
{
this->file = file;
}
string read_from_end_until_null()
{
string text;
this->file->seekg(EOF, ios_base::end);
int i = this->file->tellg();
for (i; (i - this->tail) > 0; i--)
{
if (this->file->peek() == 0)
{
this->file->get();
break;
}
this->file->seekg(i, ios_base::beg);
}
getline(this->file, text);
this->tail += text.length();
return text;
}
char* read_f(int size)
{
char* text;
this->file->seekg(this->head, ios_base::beg);
this->file->read(text, size);
this->head += size;
return text;
}
void move_f(int amount)
{
this->head += amount;
return;
}
int get_read_f()
{
returnthis->head;
}
};
In my main function, I open the file and pass the file pointer to the class:
1 2 3 4
ifstream self;
self.open(file);
FileData* data = new FileData(&self);
However, when I try and build the code, I get this error:
mismatched types 'std::basic_istream<_CharT, _Traits>' and 'std::ifstream*' {aka 'std::basic_ifstream<char>*'}
getline(this->file, text);
I have a feeling it's maybe to do with the fact that I pass a pointer to the file, instead of the actual file handler but if try without the "&", I get other errors.
I find with the members getline method, unless I'm using it wrong, you have to specify a character/delimiter to read up to. Whereas the "global" getline will just read the whole line into a string, with an instead optional character/delimiter.