Advertisement
gmmmarcos

BRANKO SENSEI

Jul 1st, 2019
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct
  6. {
  7. int documento;
  8. char nombre[30];
  9. int edad;
  10. } persona;
  11.  
  12. void cargarhumanos (char archivo[]);
  13. void mostrarlacosa (char archivo[]);
  14. void modificarpersona (char archivo[],char personamod[]);
  15.  
  16. int main()
  17. {
  18.  
  19.  
  20. char archivo[30]="archivo.dat";
  21. cargarhumanos(archivo);
  22. printf("\n");
  23. system("pause");
  24. system("cls");
  25. mostrarlacosa(archivo);
  26. printf("\n");
  27. system("pause");
  28. system("cls");
  29. modificarpersona(archivo,"pepita");
  30. printf("\n");
  31. system("pause");
  32. system("cls");
  33. mostrarlacosa(archivo);
  34. return 0;
  35. }
  36.  
  37. void cargarhumanos (char archivo[])
  38. {
  39. persona aux;
  40. FILE *archi = fopen(archivo,"a+b");
  41. //devuelve los datos del archivo
  42.  
  43.  
  44. if (archi!=NULL)
  45. {
  46. char control='s';
  47.  
  48. printf ("desea cargar personas? s/n\n");
  49. fflush(stdin);
  50. scanf ("%c",&control);
  51.  
  52. while (control=='s')
  53. {
  54.  
  55. printf ("ingrese el documento;\n");
  56. scanf("%d",&aux.documento);
  57. printf ("Ingrese el nombre:\n");
  58. fflush(stdin);
  59. gets(aux.nombre);
  60. printf ("ingrese edad:\n");
  61. scanf ("%d",&aux.edad);
  62. fwrite(&aux,sizeof(persona),1,archi);
  63. printf ("desea cargar otra persona? s/n\n");
  64. fflush(stdin);
  65. scanf ("%c",&control);
  66. }
  67. }
  68. fclose(archi);
  69.  
  70.  
  71. }
  72. void mostrarlacosa (char archivo[])
  73. {
  74. persona aux;
  75. FILE *archi = fopen(archivo,"rb");
  76.  
  77. if (archi==NULL)
  78. {
  79. printf("el archivo esta vacio ERROR DE TU VIEJA\n");
  80. }
  81. else
  82. {
  83. while (fread(&aux,sizeof(persona),1,archi)>0)
  84. {
  85. printf("Documento=%d\nNombre=%s\nEdad=%d\n",aux.documento,aux.nombre,aux.edad);
  86. printf("\n\n");
  87. }
  88.  
  89. }
  90.  
  91. fclose(archi);
  92.  
  93. }
  94.  
  95.  
  96. void modificarpersona (char archivo[],char personamod[])
  97. {
  98. persona aux;
  99. char control='s';
  100. int opcion=0;
  101.  
  102. FILE *archi=fopen(archivo,"r+b");
  103.  
  104. if (archi!=NULL)
  105. {
  106. int flag=0,i=0;
  107.  
  108. while ((fread(&aux,sizeof(persona),1,archi)>0)&&(flag==0))
  109. {
  110. if(strcmp(personamod,aux.nombre)==0)
  111. {
  112. flag=1;
  113. }
  114. i++;
  115. }
  116. if(flag==1)
  117. {
  118.  
  119.  
  120.  
  121. printf("ingrese la opcion que quiere modificar\nopcion1=documento\nopcion2=nombre\nopcion3=edad\n");
  122.  
  123. scanf("%i",&opcion);
  124.  
  125. switch (opcion)
  126. {
  127. case 1:
  128. {
  129. printf("ingrese documento\n");
  130. scanf("%d",&aux.documento);
  131. }
  132. break;
  133. case 2:
  134. {
  135. printf("ingrese nombre\n");
  136. fflush(stdin);
  137. gets(aux.nombre);
  138. }
  139. break;
  140. case 3:
  141. {
  142. printf("ingrese edad\n");
  143. scanf("%d",&aux.edad);
  144. }
  145. break;
  146. }
  147.  
  148. fseek(archi,sizeof(persona)*(i-1),SEEK_SET);
  149. fwrite(&aux,sizeof(persona),1,archi);
  150. }
  151.  
  152. }
  153.  
  154.  
  155.  
  156.  
  157. fclose(archi);
  158.  
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement