tellg() function

i was writing a part of a program to scan a file and store a copy of its contents in a dynamic char array and its size in an int variable and be able to access this information and the problem i encountered in the .cpp file was that i couldn't use tellg() as it simply stated that "variable tellg() is undefined" i think the problem is that the location of the file i give isn't a constant but a string that have yet to be passed (empty before the program is run) and would like to know if there is any way to fix it (the file i read from is going to be a .txt file)

.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef File_Scanner
#define File_Scanner
#include<iostream>

class Scanner {
private:
	char* File_Copy;
	int File_Size;
public:
	~Scanner();
	void Scan_File(std::string& File_Location);
	int Get_File_Size();
	char& Get_File_Copy_Address();
};

#endif 


.cpp
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
#include "File_Scanner.h"
#include<iostream>
#include<fstream>
#include<exception>

Scanner::~Scanner() {
	delete[] File_Copy;
}

void Scanner::Scan_File(std::string& File_Location) {
	std::ifstream Scanned_File;
	Scanned_File.open(File_Location, std::ios::in | std::ios::ate);
	if (Scanned_File.is_open()) {
		File_Size = tellg();
		Scanned_File.close();
		try {
			File_Copy = new char[File_Size];
			Scanned_File.open(File_Location, std::ios::in);
			if (Scanned_File.is_open()) {
				for (int Element; Element < File_Size; ++Element) {
					Scanned_File >> File_Copy[Element];
				}
			}
			else std::cout << "Unable to open the file" << std::endl;
		}
		catch (std::bad_alloc& Alloc_Error) { std::cout << Alloc_Error.what() << std::endl; }
	}
	else std::cout << "Unable to open the file" << std::endl;
	Scanned_File.close();
}

int Scanner::Get_File_Size() {
	return File_Size;
}

char& Scanner::Get_File_Copy_Address() {
	return *File_Copy;
}


int main is empty for now.
NOTE:Error is at line 14
Last edited on
I didn't notice thanks for pointing it out!
Topic archived. No new replies allowed.