Ver Mensaje Individual
  #19 (permalink)  
Antiguo 17/12/2010, 11:34
vnvnation
 
Fecha de Ingreso: marzo-2009
Mensajes: 74
Antigüedad: 15 años, 1 mes
Puntos: 1
Respuesta: leer una matriz desde fichero en C

men no se muy bien pero tengo yo esto no se si te sirva checalo es escritura y lectura fijate como salen como si fuera una matriz nada mas falta que vos los metas a un arreglo.
Recuerda en en C/C++ los archivos se leen por campo y regitro en este codigo tengo nada mas 4 campos y registros los que quieras

Código C++:
Ver original
  1. #include <iostream.h>
  2. #include <stdio.h>
  3. #include <fstream.h>
  4. #include <cstdlib>
  5. #include<conio.h>
  6. int main()
  7. {
  8.     ofstream Leer("Matriz.txt",ios::out);
  9.     if (!Leer)
  10.     {
  11.                               cerr<<"No se pudo abrir el archivo"<<endl;
  12.                               exit(1);
  13.                               }
  14.                              
  15. cout<<"Esribir datos ?:"<<endl;
  16. int a,b,c,d,contador=0;
  17.      while (cin>> a >> b >> c >> d)
  18.     {
  19.            contador=contador+1;
  20.           Leer<<a<<' '<<b<<' '<<c<<' '<<d<<endl;
  21.     }
  22. getch ();
  23. return 0;
  24. }



Código C++:
Ver original
  1. #include <iostream.h>
  2. #include <conio.h>
  3. #include <stdio.h>
  4. #include <fstream.h>
  5. #include <iomanip.h>
  6. #include <string.h>
  7. #include <cstdlib>
  8. void imprimirLinea(int,int,int,int);
  9. #include "stdlib.h"
  10. int main ()
  11. {
  12.     ifstream Leer("Matriz.txt",ios::in);
  13.     if (!Leer)
  14.     {
  15.                                  cerr<<"No se pudo abrir el archivo"<< endl;
  16.                                  exit(1);
  17.     }
  18.     int a,b,c,d;
  19.      while (Leer>>a >> b >> c >> d)
  20.     imprimirLinea(a,b,c,d);
  21.     getch ();
  22.     return 0;
  23. }
  24. void imprimirLinea(int a,int b,int c,int d)      
  25. {
  26.      cout<<left<<setw(10)<<a<<setw(10)<<b<<setw(10)<<c<<setprecision(10)<<right<<d<<endl;
  27.    
  28.  
  29. }