Mismatched file types for std::getline

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")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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() 
	{
		return this->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.

Any help would be appreciated!
For getline(...) you need to dereference the file pointer:

getline(*file, text); // Note: * before file
Last edited on
getline(this->file, text);
does this work instead:
this->file.getline(...)?

I don't know why but I am in the habit of using the member getline instead of the general purpose one, probably just to avoid passing the streams...
https://www.cplusplus.com/reference/istream/basic_istream/getline/
Last edited on
Yep, that fixed it - thanks!
@jonnin

The member variable does not use std::string. Instead it uses a char buffer which raises the problem that you need to predict the read size.
Last edited on
True. I usually have the file size at that point... seems like this is overdue for an upgrade. maybe in c++ 25 ...
@jonnin

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.
The default delim for .getline() is '\n' - same as for the standalone std::getline().

http://www.cplusplus.com/reference/istream/istream/getline/
http://www.cplusplus.com/reference/string/string/getline/
Topic archived. No new replies allowed.