Ver Mensaje Individual
  #4 (permalink)  
Antiguo 22/12/2013, 11:49
baterista41
 
Fecha de Ingreso: junio-2012
Mensajes: 32
Antigüedad: 11 años, 10 meses
Puntos: 1
Respuesta: Sumar Numeros de un fichero

Amigos lo logre resolver por si alguien le ayuda aqui esta

Código C++:
Ver original
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. #include <stdlib.h>
  5.  
  6. using namespace std;
  7.  
  8. void separando(char[]);
  9.  
  10.  
  11.                 void separando(char cadena[]){
  12.  
  13.                 char *contenedor;
  14.                 string convertirString;
  15.                 long long suma=0;
  16.  
  17.  
  18.                 contenedor=strtok(cadena," ");
  19.                 while(contenedor!=NULL){
  20.  
  21.                 convertirString=string(contenedor);
  22.                 suma+=atoll(convertirString.c_str());
  23.  
  24.  
  25.                 contenedor=strtok(NULL," ");
  26.  
  27.  
  28.                 }
  29.                 if(suma!=0)
  30.                 cout<<suma<<endl;
  31.  
  32.  
  33.                 }
  34.  
  35.  
  36.  
  37.  
  38.  
  39. int main(){
  40.  
  41.     ifstream archivo;
  42.     char cadena[80];
  43.  
  44.  
  45.                   archivo.open("SumaDificil.txt",ios::in);
  46.  
  47.                  if(archivo.fail())
  48.                  cout<<"Error al abrir el archivo"<<endl;
  49.  
  50.                  else{
  51.                  while(!archivo.eof()){
  52.  
  53.                        archivo.getline(cadena,sizeof(cadena),'\n');
  54.                        separando(cadena);
  55.  
  56.                  }
  57.  
  58.  
  59.  
  60.  
  61.  
  62.                   archivo.close();
  63.  
  64.                   }
  65.  
  66.  
  67.  
  68.  
  69.  
  70. cin.get();
  71.  
  72.  
  73.  
  74. return 0;
  75. }