Wczytywanie danych z pliku do listy

0

Witam.
Chciałbym wczytać z pliku tekstowego plik.txt do listy. Program nie wyrzuca błędów, ale gdy chcę wypisać listę na ekran wypisuje się tylko 1 osoba, a dokładnej:
Jan Kowalski 21 99
Wygląda na to, że lista ma tylko jeden element, więc obstawiam, że błąd jest gdzieś w pętli while.

#include <iostream>
#include <conio.h>
#include <fstream>

using namespace std;

struct Osoby{
	string imie;
	string nazwisko;
	int wiek;
	float skutecznosc;
	struct Osoby *next;
};


int main() {
	struct Osoby *head=NULL, *tmp=NULL;
	ifstream Plik;
	Plik.open("plik.txt");
	if(Plik.good())
		cout<<"Udalo sie otworzyc plik!"<<endl;
	else{
		cout<<"Nie udalo sie otworzyc pliku!"<<endl;
		return 0;
	}
	
	tmp=new struct Osoby;
	head=tmp;
	
	while(Plik>>tmp->imie && Plik>>tmp->nazwisko && Plik>>tmp->wiek && Plik>>tmp->skutecznosc){
		tmp->next=NULL;
		tmp=tmp->next;
		tmp=new struct Osoby;
	}
	
	for(tmp=head; tmp; tmp=tmp->next)
		cout<<tmp->imie<<" "<<tmp->nazwisko<<" "<<tmp->wiek<<" "<<tmp->skutecznosc<<endl;
		
	Plik.close();

	return 0;
} 

Zawartość pliku: plik.txt

Jan Kowalski 21 99
Adam Nowak 12 45
Anna Kowalska 19 20
1
  1. przydzielasz zawsze o jedną strukturę za dużo
  2. Nie podpinasz poprawnie kolejnych rekordów
while(Plik>>es>>tmp->imie>>tmp->nazwisko>>tmp->wiek>>tmp->skutecznosc)
  1. Nie używaj polskiego nazewnictwa: http://4programmers.net/Forum/1208091
  2. Uzyj bardziej sensownej struktury:
struct Person
  {
    string name;
    string surname;
    int age;
    float eficient;
  };
struct PersonNode
  {
   Person person;
   PersonNode *next;
  };
struct PersonList
  {
   PersonNode *head,*tail;
  };
0

Ja bym to w klasy ładnie jeszcze pozapinał:

#include<iostream>
#include<string>
#include<fstream>
#include<vector>
#include<functional>
#include<sstream>
using namespace std;

// Generyczna lista jednokierunkowa
template<class T> class list {
private:
	struct node {
		T value;
		int index;
		node *next;
		node(const T& value) : value(value), next(nullptr) {}
	};

	int _count;
	node *_root;

public:
	list() : _root(nullptr), _count(0) {}	
	~list() {
		if (this->_root) {
			node *current = this->_root;
			node *next = nullptr;
			while (current) {
				next = current->next;
				delete current;
				current = next;
			}
		}
	}

	void add(const T& value) {
		node *newNode = new node(value);
		newNode->index = this->_count;

		newNode->next = this->_root;
		this->_root = newNode;
		this->_count += 1;
	}
	
	int length() {
		return this->_count;
	}

	void showAll(const function<void(const T&)>& showAction) {
		node* temp = this->_root;
		while (temp) {
			showAction(temp->value);
			temp = temp->next;
		}
	}
};

// Klasa opisująca osobę.
struct person {
	string name;
	string surname;
	int age;
	float effectiveness;
	person(const string& name, const string& surname, int age, float eff) :
		name(name), surname(surname), age(age), effectiveness(eff) {}
};

// Klasa odczytująca osobę z podanego pliki i wyświetlająca wszystkie rekordy
class personReader {
private:
	list<person> _persons;
	vector<string> _splitter;

	void split(const string& phrase) {
		this->_splitter.clear();
		stringstream stream(phrase);
		string buffer = "";
		while (stream >> buffer) {
			this->_splitter.push_back(buffer);
			buffer = "";
		}
	}

	static void showPerson(const person& prs) {
		cout << prs.name << ", " << prs.surname << ", " << prs.age << ", " << prs.effectiveness << endl;
	}

public:
	void read(const char* path) {
		fstream file(path, ios::in);
		if (file.is_open()) {
			string line = "";
			while (getline(file, line)) {
				this->split(line);
				this->_persons.add(person(this->_splitter[0], this->_splitter[1], stoi(this->_splitter[2]), stof(this->_splitter[3])));
			}
		}
	}

	void showPersons() {
		function<void(const person&)> action = showPerson;
		this->_persons.showAll(action);
	}
};

int main() {
	personReader reader;
	reader.read("test.txt");
	reader.showPersons();
	return 0;
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1