Thread: seekg()
View Single Post
Old 01-19-2007, 07:15 AM   #1 (permalink)
Deliverance
C++ Beginner
 
Join Date: Jul 2005
Location: Ottawa
Posts: 73
Deliverance is on a distinguished road
seekg()

Hi guys, I am writing a function that takes in an ifstream parameter, which has already been used to read information to store it in a dynamic array (see my previous post). Aside from closing and re-opening the file, is there any way to set the position of the file pointer to the start of the file?

Here is the code snippet below that I have tried, but the seekg() function is not returning my pointer to the start of the index. the file contents would resemble something as follows:
Code:
name lastname 5 99
etc.
etc.
where the last two numbers refer to an id and a mark, and each record is separated by a newline. Any suggestions?
Code:
void ListFileHDD(ifstream& fin)
{
	cout<<"LIST FILES FROM HDD\n";
	char firstName[255], lastName[255];
	int id;
	float mark;
	fin.seekg(0,ios::beg);
	while(!fin.eof())
	{
		if(!(fin >> firstName >> lastName >> id >> mark))
			break;
		cout
			<<"First Name: " << firstName <<"\tLast Name: " << lastName
			<<"\tStudent ID: " << id <<"\tStudent Mark: " << mark << endl;
	}
	return;
}
Deliverance is offline   Reply With Quote