When trying to read data from a file, I don't know why but it reads me from the second line. I need to read the first data separated by a space and it actually reads as I want. But it skips the first line and writes the last line twice. Below is what is in the file and the code.
What is in txt file:
H Budapeszt 8 10 11
I Neapol 8 12 5
F Marsylia 5 6 6
GB Leeds 8 7 4
D Frankfurt 10 7 8
I Genua 4 6 8
D Dortmund 11 12 7
NL Rotterdam 8 12 9
D Dusseldorf 9 9 12
DK Kopenhaga 4 7 10
Code:
#include <fstream>
#include <iostream>
using namespace std;
int main() {
string a;
string linia;
fstream plik;
plik.open("galerie_przyklad.txt",ios::in);
if(plik.good()==false){
cout << "Nie udalo sie otworzyc pliku!" << endl;
exit(0);
}
else{
while(getline(plik, linia)){
plik >> a;
cout << a << endl;
}
plik.close();
}
return 0;
Result in terminal:
I
F
GB
D
I
D
NL
D
DK
DK