Search
 
SCRIPT & CODE EXAMPLE
 

C

how to delete data and add from file in c language

#include <stdio.h>
#include <stdlib.h>

void Menu();
void New_Staff();
void Delete_Staff();
void Export_Profile();

struct element{
    char id[20];
    char name[20];
    char gender[20];
    char phone[20];
    char email[20];
}profile;

int main(void){      //The program will continue running until option 4 selected                                   
    int a;  

    for(a=0;;a++){
       Menu();
    }
    return 0;
}

void Menu()      //Main Menu to select option
{
    int n;
    printf("
**********************************************");
    printf("
MAIN MENU-STAFF INFORMATION SYSTEM");
    printf("
**********************************************");
    printf("
1. Add profile Staff Profile");
    printf("
2. Delete Staff Profile");
    printf("
3. Export All Profiles to 'output.txt'");
    printf("
4. Exit");
    printf("
**********************************************");
    printf("
Please enter your option< 1 / 2 / 3 / 4 >: ");
    scanf("%d", &n);

    switch(n){
    case 1:
        New_Staff();
        break;
    case 2:
        Delete_Staff();
        break;
    case 3:
        Export_Profile();
        break;
    case 4:
        printf("
*** Thanks for using the program! Goodbye. ***");
        exit(0);
        break;
    default:
        printf("
Error! Wrong Number is Entered
Please Try Again");
        break;
    }
}

void New_Staff()                        //Add New Staff Profile 
{   
    int x;
    struct element profile;

    printf("
===Add New Staff Profile===");
    printf("

Please enter the following staff information.");

    printf("

Staff ID: ");
    scanf("%s", &profile.id);

    printf("Name	: ");
    fflush(stdin);
    fgets(profile.name,20,stdin);

    for(x=0 ; x<20 ; x++)
    {
    if(profile.name[x] == '
')
        profile.name[x] = '';
    }

    printf("Gender	: ");
    scanf("%s", &profile.gender);

    printf("Phone	: ");
    scanf("%s", &profile.phone);

    printf("Email	: ");
    scanf("%s", &profile.email);

    printf("
SYSTEM: New Staff Profile is Added Successfully.");

}

void Delete_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("
Delete 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 == '
')
    temp++;
    if (temp != Delete_Id){
    putc(c, fWrite); 
       } 
    }

    fclose(fRead);
    fclose(fWrite);

    remove(TextFile);

    rename("copy.c", TextFile);
    printf("
The contents of file after being modified are as follows:
");

    fRead = fopen(TextFile, "r");
    c = getc(fRead);
    while (c != EOF) {
    printf("%c", c);
    c = getc(fRead);
    }

    fclose(fRead);
}

void Export_Profile()           //Export Staff Profile to Text
{       
    struct element profile;

    FILE *fPtr;
    fPtr=fopen("output.txt","w");
    FILE *fPtr1;
    fPtr1=fopen("output.txt","a+");

    if (fPtr == NULL)
    printf("Error in opening file
");
    if (fPtr1 == NULL)
    printf("Error in opening file
");

    fprintf(fPtr,"%10s %10s %10s %10s %10s","Staff","ID",
             "Name","Gender","Phone","Email");

    fprintf(fPtr1,"
%10s %10s %10s %10s %10s", profile.id, profile.name,   
             profile.gender, profile.phone, profile.email);

    printf("
%10s %10s %10s %10s %10s", "Staff", "ID", "Name",
             "Gender", "Phone",  "Email");
    printf("
%10s %10s %10s %10s %10s", profile.id, 
             profile.name, profile.gender, profile.phone, profile.email);

    printf("
SYSTEM: All staff profile have been exported to output.txt file");

    fclose(fPtr);
    fclose(fPtr1);
}
Comment

PREVIOUS NEXT
Code Example
C :: how to compress a file in c 
C :: Letters and Digits Total 
C :: type conversion 
C :: How to open terminal cs50 ide 
C :: variadic macros in c 
C :: C linked sorted lists 
C :: Single-line Comments in C 
C :: bash sed crop cut file line number 
C :: send array to child process c 
C :: write a ppm image 
C :: Uri/Beecrowd problem no - 1149 solution in C 
C :: unconstrained box flutter 
C :: attiny pinout 
C :: The closest in between 
C :: pygraphviz show 
C :: memset c 
C :: array of pointers to functions 
C :: mark rober 
C :: visual studio code 
Dart :: flutter text form field change underline color 
Dart :: flutter check if string is number 
Dart :: flutter textformfield decimal 
Dart :: flutter checkbox 
Dart :: How to add a circular dot as an indicator in Flutter Tabs? 
Dart :: File dart get file extension 
Dart :: dart move item in stack to bottom 
Dart :: how to change flutter text font 
Dart :: RenderFlex overflowed 
Dart :: add dollar sign in flutter 
Dart :: dart loop through object 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =