allora guarda io per i parser uso sempre una classe Token, che mi restituisce solo "un pezzo" (un token appunto).
Ti do l'input e se ti piace puoi provare ad implementare il tuo sistema in questo modo.codice:/************************************************************************* ** Each piece of information we want to read is on its own line ** ** which means we need to write code that can parse lines of text ** ** from a file and break down the lines into smaller pieces of text. ** ** These smaller pieces of text are called tokens. ** ** This class can return to use tokens from within a text file. ** **************************************************************************/
Io imparai a fare così da un libro che ho studiato sulle DirectX.
Non trovo purtroppo miei pezzi di codice semplici da poterti incollare qui , però ho trovato una interessante discussione a riguardo su stackoverflow
codice:#include <iostream> #include <fstream> class Token { private: friend std::ostream& operator<<(std::ostream&,Token const&); friend std::istream& operator>>(std::istream&,Token&); std::string value; }; std::istream& operator>>(std::istream& str,Token& data) { // Check to make sure the stream is OK. if (!str) { return str; } char x; // Drop leading space do { x = str.get(); } while(str && isspace(x) && (x != '\n')); // If the stream is done. exit now. if (!str) { return str; } // We have skipped all white space up to the // start of the first token. We can now modify data. data.value =""; // If the token is a '\n' We are finished. if (x == '\n') { data.value = "\n"; return str; } // Otherwise read the next token in. str.unget(); str >> data.value; return str; } std::ostream& operator<<(std::ostream& str,Token const& data) { return str << data.value; } int main() { std::ifstream f("PLOP"); Token x; while(f >> x) { std::cout << "Token(" << x << ")\n"; } }

Rispondi quotando