Advertisement
Namaru

Lectura y escritura de un archivo binario

May 12th, 2023 (edited)
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <functional>
  3. #include <conio.h>
  4. #include <string>
  5. #include <filesystem>
  6. #include <fstream>
  7. #include <cstring>
  8.  
  9. using namespace std;
  10.  
  11. struct Personales
  12. {
  13.     char nombre[50];
  14.     char apellido[50];
  15.     int edad;
  16. };
  17.  
  18. bool verificarArchivo(string archivoNombre)
  19. {
  20.     fstream archivo;
  21.     archivo.open("..\\output\\" + archivoNombre, ios::out);
  22.     // ruta relativa del archivo para windows --
  23.     // ios::app verifica si existe ya el archivo sino lo crea ademas de escribir en el ultimo punto.
  24.     if (!archivo)
  25.     {
  26.         cout << "No se puede abrir el archivo";
  27.         exit(EXIT_FAILURE);
  28.         return false;
  29.     }
  30.     archivo.flush();
  31.     archivo.close();
  32.     return true;
  33. }
  34.  
  35. void escribirArchivo(Personales per[],string archivoNombre, bool verificar)
  36. {
  37.    
  38.     if (verificar)
  39.     {
  40.         fstream archivo;
  41.         archivo.open("..\\output\\" + archivoNombre, ios::out | ios::binary);
  42.         // ruta relativa del archivo para windows --
  43.         /*ios::binary*/
  44.         if (!archivo)
  45.         {
  46.             cout << "No se puede abrir el archivo";
  47.             exit(EXIT_FAILURE);
  48.         }
  49.         int i = 0;
  50.         while (i < 3) {
  51.             archivo.write(reinterpret_cast<char *>(&per[i]), sizeof(Personales));
  52.             i++;
  53.         }        
  54.         archivo.flush();
  55.         archivo.close();
  56.     }
  57. }
  58.  
  59. void llenarStruct(Personales per[],int cantidad, bool verificar, string archivoNombre)
  60. {
  61.      char pal[50];
  62.     for (int i = 0; i < cantidad; i++)
  63.     {
  64.         cout << "Ingrese el Nombre: ";
  65.         //cin >> pal;        
  66.         cin>>per[i].nombre;
  67.         //strcpy(per[i].nombre, pal);        
  68.         cout << "Ingrese el Apellido: ";
  69.         cin >> pal;
  70.         strcpy(per[i].apellido, pal);        
  71.         cout << "Ingrese la edad: ";
  72.         cin >> per[i].edad;        
  73.     }
  74.     escribirArchivo(per,archivoNombre, verificar);
  75. }
  76.  
  77. int leerArchivo(string archivoNombre, int cantidad)
  78. {
  79.     Personales2 per;
  80.     Personales person[cantidad];
  81.     string linea;
  82.     ifstream archivo;
  83.     int posicion = 0;
  84.     int tamaƱo = sizeof(Personales);
  85.     archivo.open("..\\output\\" + archivoNombre,ios::out | ios::binary);
  86.     if (!archivo)
  87.     {
  88.         cout << "No se puede abrir el archivo";
  89.         exit(EXIT_FAILURE);
  90.     }
  91.     int i = 0;
  92.     archivo.seekg(0, ios::beg);//el error es la posicion hay que leer desde el principio no el final. osea 0 de N (0/n) no n/0
  93.     while (i < cantidad) {
  94.         archivo.read(reinterpret_cast<char *>(&person[i]), sizeof(Personales));
  95.         i++;
  96.     }
  97.  
  98.     archivo.close();    
  99.     i = 0;
  100.     while (i <= cantidad)
  101.     {
  102.         cout << "Datos en la posicion: " << i+1<< endl;
  103.         cout << "Nombre: ";
  104.         cout << person[i].nombre <<endl;
  105.         cout << "Apellido: ";
  106.         cout << person[i].apellido <<endl;
  107.         cout << "Edad: ";
  108.         cout << person[i].edad <<endl;
  109.         i = i+2;        
  110.     }
  111. }
  112.  
  113. int main()
  114. {    
  115.     string archivo;
  116.     int cantidad = 3;
  117.     Personales per[cantidad];
  118.     archivo = "Archivo.bin";
  119.     bool verificar = verificarArchivo(archivo);
  120.     llenarStruct(per, cantidad, verificar, archivo);
  121.     leerArchivo(archivo, cantidad);
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement