Ver Mensaje Individual
  #5 (permalink)  
Antiguo 04/07/2013, 11:08
ecfisa
 
Fecha de Ingreso: julio-2012
Mensajes: 133
Antigüedad: 12 años, 2 meses
Puntos: 22
Respuesta: Leer desde fichero

Hola JOHARFLO.

Código C++:
Ver original
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. void splitstr(const string&, const char, vector<string>&);
  8.  
  9. int main()
  10. {
  11.   ifstream ifs("tu_archivo.txt", ifstream::in);
  12.   vector <string> vparse;
  13.   string s;
  14.   if (ifs.is_open()) {
  15.     while (!ifs.eof()) {
  16.       getline(ifs, s);
  17.       splitstr(s, ';', vparse);
  18.       /* Operaciones con texto parseado. Los valores obtenidos estan
  19.          en vparse desde: vparse[0] hasta vparse[vparse.size()-1]. */
  20.       vparse.clear();
  21.     }
  22.     ifs.close();
  23.   }
  24.   return 0;
  25. }
  26.  
  27. void splitstr(const string& str, const char delim, vector<string>& vec)
  28. {
  29.   size_t p1 = 0;
  30.   size_t p2 = str.find(delim);
  31.  
  32.   while (p2 != string::npos) {
  33.     vec.push_back(str.substr(p1, p2-p1));
  34.     p1 = ++p2;
  35.     p2 = str.find(delim, p2);
  36.     if (p2 == string::npos)
  37.       vec.push_back(str.substr(p1, str.length( )));
  38.   }
  39. }

Saludos :)