Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct
- {
- int documento;
- char nombre[30];
- int edad;
- } persona;
- void cargarhumanos (char archivo[]);
- void mostrarlacosa (char archivo[]);
- void modificarpersona (char archivo[],char personamod[]);
- int main()
- {
- char archivo[30]="archivo.dat";
- cargarhumanos(archivo);
- printf("\n");
- system("pause");
- system("cls");
- mostrarlacosa(archivo);
- printf("\n");
- system("pause");
- system("cls");
- modificarpersona(archivo,"pepita");
- printf("\n");
- system("pause");
- system("cls");
- mostrarlacosa(archivo);
- return 0;
- }
- void cargarhumanos (char archivo[])
- {
- persona aux;
- FILE *archi = fopen(archivo,"a+b");
- //devuelve los datos del archivo
- if (archi!=NULL)
- {
- char control='s';
- printf ("desea cargar personas? s/n\n");
- fflush(stdin);
- scanf ("%c",&control);
- while (control=='s')
- {
- printf ("ingrese el documento;\n");
- scanf("%d",&aux.documento);
- printf ("Ingrese el nombre:\n");
- fflush(stdin);
- gets(aux.nombre);
- printf ("ingrese edad:\n");
- scanf ("%d",&aux.edad);
- fwrite(&aux,sizeof(persona),1,archi);
- printf ("desea cargar otra persona? s/n\n");
- fflush(stdin);
- scanf ("%c",&control);
- }
- }
- fclose(archi);
- }
- void mostrarlacosa (char archivo[])
- {
- persona aux;
- FILE *archi = fopen(archivo,"rb");
- if (archi==NULL)
- {
- printf("el archivo esta vacio ERROR DE TU VIEJA\n");
- }
- else
- {
- while (fread(&aux,sizeof(persona),1,archi)>0)
- {
- printf("Documento=%d\nNombre=%s\nEdad=%d\n",aux.documento,aux.nombre,aux.edad);
- printf("\n\n");
- }
- }
- fclose(archi);
- }
- void modificarpersona (char archivo[],char personamod[])
- {
- persona aux;
- char control='s';
- int opcion=0;
- FILE *archi=fopen(archivo,"r+b");
- if (archi!=NULL)
- {
- int flag=0,i=0;
- while ((fread(&aux,sizeof(persona),1,archi)>0)&&(flag==0))
- {
- if(strcmp(personamod,aux.nombre)==0)
- {
- flag=1;
- }
- i++;
- }
- if(flag==1)
- {
- printf("ingrese la opcion que quiere modificar\nopcion1=documento\nopcion2=nombre\nopcion3=edad\n");
- scanf("%i",&opcion);
- switch (opcion)
- {
- case 1:
- {
- printf("ingrese documento\n");
- scanf("%d",&aux.documento);
- }
- break;
- case 2:
- {
- printf("ingrese nombre\n");
- fflush(stdin);
- gets(aux.nombre);
- }
- break;
- case 3:
- {
- printf("ingrese edad\n");
- scanf("%d",&aux.edad);
- }
- break;
- }
- fseek(archi,sizeof(persona)*(i-1),SEEK_SET);
- fwrite(&aux,sizeof(persona),1,archi);
- }
- }
- fclose(archi);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement