Ver Mensaje Individual
  #18 (permalink)  
Antiguo 27/12/2012, 12:57
kelvi
 
Fecha de Ingreso: diciembre-2012
Mensajes: 13
Antigüedad: 11 años, 5 meses
Puntos: 0
Respuesta: Lectura de archivo en C++

Te incluyo también el código del programa de Filtrar por si te ayuda en algo:

Código C++:
Ver original
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <list>
  8. #include <sstream>
  9.  
  10. using namespace std;
  11.  
  12. typedef struct {
  13.     char EV;
  14.     float T;
  15.     int E,R;
  16.     string P;
  17.     float G;
  18.     string FLA;
  19.     int FLU;
  20.     string O,D;
  21.     float S,I;
  22. } DADES_ARXIU;
  23.  
  24. enum ID_CAMPS{EV,T,E,R,P,G,FLA,FLU,O,D,S,I};
  25.  
  26. int main() {
  27.     DADES_ARXIU camps;
  28.     DADES_ARXIU zero = {0, 0, 0, 0, "", 0, "", 0, "", "", 0, 0};
  29.     int camp;//incremental
  30.     char *pch, *copia;
  31.     string linia;
  32.     //VARIABLES PER FILTRAR
  33.     int em,re,fl;
  34.     string ori,de;
  35.     ostringstream convert,convert2;
  36.     printf("EMISOR:");cin>>em;
  37.     convert << em;
  38.     ori=convert.str();
  39.     ori = ori + ".0";
  40.     printf("RECEPTOR:");cin>>re;
  41.     convert2 << re;
  42.     de=convert2.str();
  43.     de = de + ".0";
  44.     printf("FLUX:");cin>>fl;
  45.  
  46.     //FITXERS DE LECTURA I ESCRIPTURA  
  47.     ifstream fitxer;
  48.     ofstream fitxerFiltrat;
  49.     fitxer.open("Proces/out.tr");
  50.     fitxerFiltrat.open("Proces/outFiltrat.tr");
  51.     if (!fitxer || !fitxerFiltrat) {
  52.         cerr << "Error al llegir o Escriure fitxer";
  53.         return 1;
  54.     }
  55.     while(getline(fitxer,linia)){
  56.         copia = new char [linia.size()+1];
  57.         strcpy (copia,linia.c_str());  
  58.         camp = EV;
  59.         pch = strtok(copia, " ");
  60.         while(pch != NULL){
  61.             switch(camp) {
  62.                 case EV: {                 
  63.                                 camps.EV = pch[0];
  64.                         }break;
  65.                 case E: {
  66.                     camps.E = atoi(pch);                    
  67.                         }break;
  68.                 case R: {
  69.                     camps.R = atoi(pch);                    
  70.                 }break;
  71.                 case FLU: {
  72.                     camps.FLU = atoi(pch);      
  73.                 }break;
  74.                 case O: {
  75.                     camps.O = pch;              
  76.                         }break;
  77.                 case D: {
  78.                     camps.D = pch;                  
  79.                 }break;
  80.             }
  81.             pch = strtok (NULL, " ");
  82.             camp++;
  83.         }
  84.     if(camp != I+1){ cout << "ERROR DE FITXER DE LECTURA"; return 1;}
  85.     if((camps.FLU==fl) && (ori.compare(camps.O)==0) && (de.compare(camps.D)==0) && (((camps.EV == '+') && (camps.E==em)) || ((camps.EV=='r')&&(camps.R==re)))){
  86.         fitxerFiltrat << linia << endl;
  87.     }
  88.     //BUIDAR STRUCT
  89.     memcpy(&camps, &zero, sizeof(DADES_ARXIU));
  90.     //BUIDAR CHAR
  91.     delete[] copia;
  92.     }
  93.     fitxer.close();
  94.     fitxerFiltrat.close();
  95.     return 0;
  96. }