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)
| | |-+  Problema al leer registros en ficheros
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Problema al leer registros en ficheros  (Leído 1,461 veces)
neveldine

Desconectado Desconectado

Mensajes: 17


Ver Perfil
Problema al leer registros en ficheros
« en: 25 Noviembre 2015, 13:35 pm »

Hola,
Tengo un problema con este programa. Como podéis ver cuando muestro los registros del fichero los duplica.

Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #include "MemoryManager.h"
  6.  
  7. #define N 80
  8. #define SALIR 4
  9.  
  10. typedef struct {
  11. char nombre[N];
  12. char direccion[N];
  13. int telefono;
  14. } t_persona;
  15.  
  16.  
  17. int tolower(int);
  18. int menu(void);
  19. void crearFichero(FILE **pf);
  20. void introducirRegistros(FILE *pf);
  21. void visualizarFichero(FILE *pf);
  22.  
  23. /**
  24.  * Punto de entrada y salida al programa principal
  25.  */
  26. int main()
  27. {
  28. int op = 0;
  29. FILE *pf = NULL;
  30.  
  31. do {
  32. op = menu();
  33.  
  34. switch (op)
  35. {
  36. case 1: // crear fichero
  37. crearFichero(&pf);
  38. break;
  39.  
  40. case 2: // introducir registros
  41. introducirRegistros(pf);
  42. break;
  43.  
  44. case 3: // visualizar registros
  45. visualizarFichero(pf);
  46. break;
  47.  
  48. case 4: // salir
  49. printf("bye bye\n");
  50. break;
  51. }
  52. } while (op != SALIR);
  53.  
  54. fclose(pf);
  55. MemoryManager_DumpMemoryLeaks();
  56. return 0;
  57. }
  58.  
  59. int tolower(int c)
  60. {
  61. const int dsp = 'a' - 'A';
  62.  
  63. if (c >= 'A' && c <= 'Z')
  64. return c + dsp;
  65. return c;
  66. }
  67.  
  68. int menu(void)
  69. {
  70. int op = 0;
  71. char linea[N] = { 0 };
  72.  
  73. do {
  74. printf("\n");
  75. printf("1. Crear fichero\n");
  76. printf("2. Introducir registros\n");
  77. printf("3. visualizar registros\n");
  78. printf("4. salir\n");
  79.  
  80. printf("\topcion: ");
  81. fgets(linea, N, stdin);
  82. sscanf(linea, "%d", &op);
  83. } while (op < 1 || op > 4);
  84.  
  85. return op;
  86. }
  87.  
  88. void crearFichero(FILE **pf)
  89. {
  90. char nomFichero[N] = { 0 };
  91. char resp = 0;
  92. char linea[N] = { 0 };
  93.  
  94. printf("Nombre del fichero: ");
  95. fgets(nomFichero, N, stdin);
  96. nomFichero[strlen(nomFichero) - 1] = 0;
  97.  
  98. if (*pf != NULL) {
  99.  
  100. // existe, preguntar si se sobreesribe
  101. do {
  102. printf("Desea sobreescribir el fichero");
  103. fgets(linea, N, stdin);
  104. sscanf(linea, "%c", &resp);
  105. } while (tolower(resp) != 's'
  106. && tolower(resp) != 'n');
  107.  
  108. if (tolower(resp) == 'n')
  109. return;
  110. }
  111.  
  112. *pf = fopen(nomFichero, "w+b");
  113. }
  114.  
  115. void introducirRegistros(FILE *pf)
  116. {
  117. char resp = 0;
  118. char linea[N] = { 0 };
  119.  
  120. t_persona *p_per = malloc(sizeof(t_persona));
  121. memset(p_per, 0, sizeof(t_persona));
  122. if (p_per == NULL)
  123. {
  124. printf("memoria insuficiente");
  125. exit(-1);
  126. }
  127.  
  128.  
  129. do {
  130. printf("\n------\n");
  131. printf("Nombre: ");
  132. fgets(p_per->nombre, N, stdin);
  133. p_per->nombre[strlen(p_per->nombre) - 1] = 0;
  134.  
  135. printf("Direccion: ");
  136. fgets(p_per->direccion, N, stdin);
  137. p_per->direccion[strlen(p_per->direccion) - 1] = 0;
  138.  
  139. printf("Telefono: ");
  140. fgets(linea, N, stdin);
  141. sscanf(linea, "%d", &p_per->telefono);
  142.  
  143. rewind(pf);
  144. fwrite(p_per, sizeof(t_persona), 1, pf);
  145.  
  146. do {
  147. printf("\nDesea introducir otro registro: ");
  148. fgets(linea, N, stdin);
  149. sscanf(linea, "%c", &resp);
  150. } while (tolower(resp) != 's'
  151. && tolower(resp) != 'n');
  152. } while (tolower(resp) == 's');
  153.  
  154. free(p_per);
  155. }
  156.  
  157. void visualizarFichero(FILE *pf)
  158. {
  159. char linea[N] = { 0 };
  160. int pos = 0;
  161.  
  162. t_persona *p_per = malloc(sizeof(t_persona));
  163. if (p_per == NULL)
  164. {
  165. printf("memoria insuficiente");
  166. exit(-1);
  167. }
  168.  
  169. rewind(pf);
  170.  
  171. while (!feof(pf) && !ferror(pf)) {
  172. fread(p_per, sizeof(t_persona), 1, pf);
  173. printf("%s\n", p_per->nombre);
  174. printf("%s\n", p_per->direccion);
  175. printf("%d\n", p_per->telefono);
  176. }
  177.  
  178. free(p_per);
  179. }
  180.  


Código:
1. Crear fichero
2. Introducir registros
3. visualizar registros
4. salir
        opcion: 1
Nombre del fichero: red.datos

1. Crear fichero
2. Introducir registros
3. visualizar registros
4. salir
        opcion: 2

------
Nombre: Marina
Direccion: vereda
Telefono: 234
Desea introducir otro registro: n

1. Crear fichero
2. Introducir registros
3. visualizar registros
4. salir
        opcion: 3

Marina
vereda
234
Marina
vereda
234

Gracias anticipadas.

Un cordial saludo


En línea

neveldine

Desconectado Desconectado

Mensajes: 17


Ver Perfil
Re: Problema al leer registros en ficheros
« Respuesta #1 en: 25 Noviembre 2015, 14:09 pm »

Ya está solucionado, el problema era que al visualizar los registros el bucle tiene que ser:

Código
  1. fread(p_per, sizeof(t_persona), 1, pf);
  2. while (!feof(pf) && !ferror(pf)) {
  3. printf("%s\n", p_per->nombre);
  4. printf("%s\n", p_per->direccion);
  5. printf("%d\n", p_per->telefono);
  6. fread(p_per, sizeof(t_persona), 1, pf);
  7. }
  8.  


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines