Example #1
0
void ReadDataFile(){
	cout << "Opening data file 'data_" << FileNumber << ".txt'..." << endl;
	string file = "data_" + FileNumber + ".txt";
	DataFile.open(file.c_str());
	if (!DataFile){
		TerminateError("Unable to open data file.");
	}
	ListNode<ListNodeData> *node = NULL;
	//Find the last node to append the list, if the list is already with items. 
	TheList.Rewind();
	while (TheList.Current() != NULL){
		node = TheList.Current();
		TheList.Next();
	}
	string text, dummy;
	int number;
	//Read data file into list
	while (!DataFile.eof()){
		GetLine(DataFile, text);		//GetLine gets rid of newline character.
		if (DataFile.eof()){			//EOF was reached before a number is read!? Means file is malformed. Ignoring final node.
			cout << "Unexpected end of file for data file. Finishing read." << endl;
			break;
		}
		DataFile >> number;
		getline(DataFile, dummy);	//Use GetLine to clear the newline character.
		//The default behaviour of inserting new nodes to the list is by inserting it at the first node.
		//So we need a "workaround" to build the list as it was defined in the data file.
		cout << "Appending Node " << text << " " << number << endl;
		if (node == NULL){
			node = TheList.InsertNewNode(ListNodeData(number, text));		//This is the first node.
		}
		else{
			node = TheList.AppendNode(node, ListNodeData(number, text));	//Append additional node behind the first node
		}
		
	}
	DataFile.close();
}