Hello,

This is for an assignment I have to do, I have completed most of it just can't get one part to work correctly.

The assignment is:
The TextFieldLoader class will have only one constructor and MUST accept the following parameters only:
1. filename (const char *) that will be used to read (and store) the entire contents of the file into memory.
2. row (int), and
3. column (int) representing where on the screen the TextFieldLoader is located.
4. width (int), and
5. height (int) representing the TextFieldLoader's dimensions.

Once instantiated, the TextFieldLoader must use the functions seekg( ) and tellg( ) to dynamically determine how large the file is before loading the contents of the file into memory using only one call to the read( ) function.

Now I have completed most of whats required except that I am not sure how to proceed with dynamically allocating memory for the file input and the using the read function.

This is what I have done:

//declaration
class TextFieldLoader: public ifstream, public TextField{
public:
	TextFieldLoader(/*const*/ char*, int, int, int, int);
	~TextFieldLoader();
};

//definition
TextFieldLoader::TextFieldLoader(/*const */char *fn, int row, int col, int width, int height)
			: ifstream(fn), TextField(fn, row, col, width, height){
	
	int length;
	//determining the size of the file
	//move to end of file
	seekg(0, ios::end);
	length = tellg();
	seekg(0, ios::beg);

	//read into memory
	read(fn, length);
}

I think I have got the memory allocation correct but can't get the read to work properly.

Thanks.

Recommended Answers

All 3 Replies

A little update, I think its wrong as the data sent to

TextFieldLoader::TextFieldLoader(/*const */char *fn, int row, int col, int width, int height)
			: ifstream(fn), TextField(fn, row, col, width, height){

is done first so I have no way of passing the array to TextField as that is run first.

TextFieldLoader::TextFieldLoader(/*const */char *fn, int row, int col, int width, int height)
			: fstream(fn), TextField(fn, row, col, width, height){
	
	int length;
	//determining the size of the file
	//move to end of file
	seekg(ios::beg, ios::end);
	//get length
	length = tellg();
	//move to start of file
	seekg(0, ios::beg);

	char* data;
	data = new char[length];

	//read into memory
	read(data, length);
	
	cout << data << endl;
	getchar();
}

This seems to work as the data is copied.

TextFieldLoader::TextFieldLoader(/*const */char *fn, int row, int col, int width, int height)
			: fstream(fn), TextField(fn, row, col, width, height){
	
	int length;
	//determining the size of the file
	//move to end of file
	seekg(ios::beg, ios::end);
	//get length
	length = tellg();
	//move to start of file
	seekg(/*0,*/ ios::beg);
	
	//read into memory
	read(*page_, length);
}

Why do you try to pass an fstream object?
Just declare an fstream object in the private section of your class :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.