Saltar al contenido

cómo eliminar un registro de un archivo de texto en c ejemplo de código de programación

Después de tanto batallar pudimos encontrar la solución de esta escollo que agunos lectores de nuestra web han tenido. Si deseas compartir algún dato no dudes en aportar tu comentario.

Ejemplo: cómo eliminar datos y agregar desde un archivo en lenguaje c

#include#includevoidMenu();voidNew_Staff();voidDelete_Staff();voidExport_Profile();structelementchar id[20];char name[20];char gender[20];char phone[20];char email[20];profile;intmain(void)//The program will continue running until option 4 selected                                   int a;for(a=0;;a++)Menu();return0;voidMenu()//Main Menu to select optionint n;printf("n**********************************************");printf("nMAIN MENU-STAFF INFORMATION SYSTEM");printf("n**********************************************");printf("n1. Add profile Staff Profile");printf("n2. Delete Staff Profile");printf("n3. Export All Profiles to 'output.txt'");printf("n4. Exit");printf("n**********************************************");printf("nPlease enter your option< 1 / 2 / 3 / 4 >: ");scanf("%d",&n);switch(n)case1:New_Staff();break;case2:Delete_Staff();break;case3:Export_Profile();break;case4:printf("n*** Thanks for using the program! Goodbye. ***");exit(0);break;default:printf("nError! Wrong Number is EnterednPlease Try Again");break;voidNew_Staff()//Add New Staff Profile int x;structelement profile;printf("n===Add New Staff Profile===");printf("nnPlease enter the following staff information.");printf("nnStaff ID: ");scanf("%s",&profile.id);printf("Namet: ");fflush(stdin);fgets(profile.name,20,stdin);for(x=0; x<20; x++)if(profile.name[x]=='n')
        profile.name[x]='';printf("Gendert: ");scanf("%s",&profile.gender);printf("Phonet: ");scanf("%s",&profile.phone);printf("Emailt: ");scanf("%s",&profile.email);printf("nSYSTEM: New Staff Profile is Added Successfully.");voidDelete_Staff()//Delete Staff Profile                   
    FILE *fRead,*fWrite;char*TextFile;char c;int Delete_Id, temp =1;

    TextFile="output.txt";

    fRead =fopen(TextFile,"r");
    c =getc(fRead);while(c !=EOF)printf("%c", c);
    c =getc(fRead);rewind(fRead);printf("nDelete Staff with ID: ");scanf("%d",&Delete_Id);

    Delete_Id=Delete_Id+1;

    fWrite =fopen("copy.c","w");
    c =getc(fRead);while(c !=EOF)
    c =getc(fRead);if(c =='n')
    temp++;if(temp != Delete_Id)putc(c, fWrite);fclose(fRead);fclose(fWrite);remove(TextFile);rename("copy.c", TextFile);printf("nThe contents of file after being modified are as follows:n");

    fRead =fopen(TextFile,"r");
    c =getc(fRead);while(c !=EOF)printf("%c", c);
    c =getc(fRead);fclose(fRead);voidExport_Profile()//Export Staff Profile to Textstructelement profile;

    FILE *fPtr;
    fPtr=fopen("output.txt","w");
    FILE *fPtr1;
    fPtr1=fopen("output.txt","a+");if(fPtr ==NULL)printf("Error in opening filen");if(fPtr1 ==NULL)printf("Error in opening filen");fprintf(fPtr,"%10s %10s %10s %10s %10s","Staff","ID","Name","Gender","Phone","Email");fprintf(fPtr1,"n%10s %10s %10s %10s %10s", profile.id, profile.name,   
             profile.gender, profile.phone, profile.email);printf("n%10s %10s %10s %10s %10s","Staff","ID","Name","Gender","Phone","Email");printf("n%10s %10s %10s %10s %10s", profile.id, 
             profile.name, profile.gender, profile.phone, profile.email);printf("nSYSTEM: All staff profile have been exported to output.txt file");fclose(fPtr);fclose(fPtr1);

No se te olvide mostrar esta sección si te ayudó.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)


Tags : /

Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *