I need to read all attributes from a tеxt file that looks like the following for one Stern (engl.: Star) object. I need to replace the string "leer" with "" but there can also be a valid string which shouldn't be replaced with "".
I.e for another Stern object there could be "leer" instead of "Sol" as well.
Problem:
The problem is it doesn't replace the "leer" with the "". And it seems like it saves "leer\\r" in the object instead of only "leer" but I tried to replace "leer\\r" as well and it still doesn`t work.
This is one Stern in the text file that should be read:
0 Sol 0.000005 0.000000 0.000000 leer 1 0 And this is my operator >> to read it:
istream& operator>>(istream& is, Stern& obj) { string dummy; is >> obj.m_ID; getline(is, dummy); getline(is, obj.m_Bez); if (obj.m_Bez == "leer") obj.m_Bez = ""; is >> obj.m_xKoord >> obj.m_yKoord >> obj.m_zKoord; getline(is,dummy); getline(is,obj.m_Sternbild); if (obj.m_Sternbild == "leer") obj.m_Sternbild = ""; is >> obj.m_Index >> obj.m_PrimID; return is; } Stern.h:
#ifndef STERN_H #define STERN_H #include <string> #include <iostream> using namespace std; class Stern { public: Stern(); // 2.a) //Stern(int m_ID, string m_Bez, float m_xKoord, float m_yKoord, float m_zKoord, string m_Sternbild, int m_Index, int m_PrimID); virtual ~Stern(); void print() const; // 1.b) friend ostream& operator<<(ostream& os, const Stern& obj); // 1.b)i. friend istream& operator>>(istream& is, Stern& obj); private: int m_ID; string m_Bez; float m_xKoord; float m_yKoord; float m_zKoord; string m_Sternbild; int m_Index; int m_PrimID; }; #endif /* STERN_H */
getline(is, obj.m_Bez);and that doesn't remove the whitespace. Try triming the string first then check for equality.