elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: AIO elhacker.NET 2021 Compilación herramientas análisis y desinfección malware


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  [Aporte] Archivos Binarios en C
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: [Aporte] Archivos Binarios en C  (Leído 2,887 veces)
nicolasblues86

Desconectado Desconectado

Mensajes: 38



Ver Perfil
[Aporte] Archivos Binarios en C
« en: 19 Mayo 2010, 23:38 pm »

consiste en dos programas  en el primero se hacen la entrada de datos y en el segundo se realizan modificaciones...
mucha ganas de explicarlo no tengo  :P jeje de ultima si no se entiende un joroca me explico con mas detalle mas tarde..

arch.h

Código
  1.  
  2.  
  3. #include <stdio.h>
  4.  
  5. typedef struct FECHA{
  6.  
  7. int d, m, a;
  8. }fecha;
  9.  
  10. typedef struct TIEMPO{
  11.  
  12. int h, m, s;
  13.  
  14. }tiempo;
  15.  
  16. struct atleta{
  17.  
  18. char nombre[30];
  19. fecha f;
  20. char sx; //sexo
  21. char cat; //categoria
  22. tiempo t;
  23. unsigned int dorsal;
  24. unsigned short puesto;
  25.  
  26. };
  27.  
  28. typedef struct atleta atle;
  29. #define desplz(n) (n-1) *sizeof (atle)
  30.  
  31.  

archivo de entrada.c

Código
  1.  
  2.  
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7. #include <string.h>
  8. #include "atleta.h"
  9. #define randomize (srand(time(NULL)))
  10. #define random(num) (rand()%(num))
  11.  
  12. void inicializar (FILE* pf);
  13. void igresar_A (FILE* pf, atle* at);
  14. unsigned numdorsal (char s, char cat, FILE * pf);
  15.  
  16.  
  17.  
  18. int main(int argc, char** argv)
  19. {
  20. FILE *pf;
  21. atle atletas;
  22. char *archivo = "Carrera.dat";
  23. randomize;
  24.  
  25. if ((pf= fopen( archivo, "wb+")) == NULL)
  26. {
  27. printf ("\n Error al abrir archivo ");
  28. return -1;
  29. }
  30.  
  31. inicializar(pf);
  32.  
  33. igresar_A(pf, &atletas);
  34. do
  35. {
  36. fseek(pf, desplz(atletas.dorsal), 0);
  37. fwrite (&atletas, sizeof(atletas),1,pf);
  38. igresar_A(pf, &atletas);
  39.  
  40. }while (strcmp (atletas.nombre, "FIN"));
  41.  
  42.  
  43.  
  44. fclose (pf);
  45.  
  46.  
  47. return 0;
  48. }
  49.  
  50. void inicializar (FILE* pf)
  51. {
  52. int i;
  53. atle a;
  54.  
  55. a.nombre[0] = '\0';
  56.  
  57. for (i=0; i<1000; i++)
  58. fwrite(&a, sizeof(atle), 1, pf);
  59.  
  60.  
  61. }
  62.  
  63. void igresar_A (FILE* pf, atle* at)
  64. {
  65. char buf[30];
  66. int n;
  67.  
  68. buf[0]='\0';
  69. printf ("Nombre    ");
  70. fgets (buf, 30, stdin);
  71. n = strlen (buf);
  72. if (buf[n] == '\n');
  73. buf[n] = '\0';
  74.  
  75. sscanf(buf,"%s", at->nombre);
  76. if (strcmp (at->nombre, "FIN"))
  77. {
  78. printf ("ingrese la fecha de naciemiento (dd mm aaaa)");
  79. buf[0]='\0';
  80. fgets (buf, 30, stdin);
  81. sscanf (buf, "%d %d %d", &at->f.d, &at->f.m, &at->f.a);
  82. if (at->f.a < 1954)
  83. at->cat = 'V';
  84. else
  85. at->cat = 'S';
  86.  
  87. printf ("Sexo (M/F)");
  88. fgets (buf, 3, stdin);
  89. sscanf (buf, "%c", &at->sx);
  90.  
  91. at->sx = (char) toupper(at->sx);
  92.  
  93.  
  94. at->t.h = 0;
  95. at->t.m = 0;
  96. at->t.s = 0;
  97.  
  98. at->dorsal = numdorsal(at->sx, at->cat, pf);
  99. printf ("%d", at->dorsal);
  100.  
  101. }
  102. }
  103.  
  104. unsigned numdorsal (char s, char cat, FILE * pf)
  105.  
  106. {
  107. unsigned base, tope, d;
  108. atle at;
  109.  
  110. if (s == 'M' && cat=='V')
  111. {
  112. base = 251;
  113. tope = 500;
  114. }
  115. else if (s == 'M' && cat == 'S')
  116. {
  117. base = 501;
  118. tope = 1000;
  119. }
  120. else if (s == 'F' && cat == 'V')
  121. {
  122. base = 51;
  123. tope = 100;
  124. }
  125. else if (s == 'F' && cat == 'S')
  126. {
  127. base = 101;
  128. tope = 200;
  129. }
  130.  
  131. d = (unsigned) random(tope+1-base)+base;
  132. fseek(pf, desplz(d),0);
  133. fread(&at,sizeof(atle),1,pf);
  134. if (!(*at.nombre))
  135. return d;
  136. else
  137. return numdorsal(s, cat, pf);
  138.  
  139. }
  140.  
  141.  


archivo de entrada a tiempos de atletas.c


Código
  1.  
  2.  
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7. #include <string.h>
  8. #include "atleta.h"
  9.  
  10.  
  11. void muestraDatos (atle at);
  12. void dorsalesjuego (FILE* pf );
  13.  
  14.  
  15. int main(int argc, char** argv)
  16. {
  17. FILE * pf;
  18. atle atletas;
  19.  
  20. unsigned dorsal1;
  21. char dor[20], *archivo = "Carrera.dat";
  22.  
  23.  
  24. if ((pf = fopen (archivo, "rb+")) == NULL)
  25. {
  26. printf ("Error al abrir archivo");
  27. return -1;
  28. }
  29.  
  30. dorsalesjuego(pf);
  31.  
  32. printf ("Escriba el numero de dorsal: ");
  33. fgets (dor, 5, stdin);
  34. sscanf (dor, "%u", &dorsal1);
  35. while (dorsal1)
  36. {
  37. fseek(pf,desplz(dorsal1), 0);
  38. fread(&atletas, sizeof(atletas),1,pf);
  39.  
  40. if (*atletas.nombre)
  41. {
  42.  
  43. muestraDatos(atletas);
  44. printf ("\n Tiempo realizado en minutos y segundos: ");
  45.    fgets (dor, 15, stdin);
  46.    sscanf (dor, "%d %d", &atletas.t.m, &atletas.t.s);
  47.    fseek(pf,desplz(dorsal1), 0);
  48.    fwrite(&atletas, sizeof(atle),1,pf);
  49. }
  50. else
  51. printf ("Dorsal no encontrado, vuelva a intentar\n");
  52.  
  53. fgets (dor, 5, stdin);
  54. sscanf (dor, "%u", &dorsal1);
  55.  
  56. }
  57.  
  58.  
  59. fclose(pf);
  60.  
  61.  
  62.  
  63.  
  64. return 0;
  65. }
  66.  
  67.  
  68. void muestraDatos (atle at)
  69.  
  70. {
  71. printf ("Nombre : %s\n", at.nombre);
  72. printf ("Fecha de naciemiento : %d-%d-%d\n", at.f.d, at.f.m, at.f.a);
  73. printf ("Categoria : %c\nDorsal: %u\n", at.cat, at.dorsal);
  74. if (at.t.m > 0)
  75. printf ("\ntiempo de carrera : %d Min %d Seg", at.t.m, at.t.s );
  76.  
  77.  
  78.  
  79. }
  80.  
  81.  
  82. void dorsalesjuego (FILE* pf )
  83.  
  84. {
  85. atle at;
  86.  
  87. printf ("jugadores en jeugo \n");
  88.  
  89. while ( !feof(pf) )
  90. {
  91. fread (&at, sizeof(atle),1,pf);
  92. if (*at.nombre)
  93. printf ("numero de dorsal %d\n", at.dorsal);
  94. }
  95.  
  96.  
  97. }
  98.  



eso fue todo saludos

 
  


« Última modificación: 19 Mayo 2010, 23:59 pm por nicolasblues86 » En línea

Solamente hay 10 clases de personas en el mundo los que saben leer binario y los que no
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
[aporte] MIME identificar archivos por su contenido
Programación Visual Basic
Yoghurt 9 8,806 Último mensaje 31 Agosto 2012, 12:25 pm
por Yoghurt
archivos binarios
Programación C/C++
marcico 0 1,468 Último mensaje 26 Julio 2012, 14:14 pm
por marcico
[Aporte]Cripter en python(Encriptador de archivos)
Scripting
Baal. 4 3,076 Último mensaje 11 Enero 2013, 21:34 pm
por Baal.
[?] Archivos Binarios
Programación C/C++
MeCraniDOS 3 2,784 Último mensaje 20 Octubre 2013, 15:05 pm
por rir3760
Archivos bINARIOS c++ « 1 2 »
Programación C/C++
SojatDotar 11 4,722 Último mensaje 9 Febrero 2016, 16:36 pm
por SojatDotar
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines