how to delete data and add from file in c language

Solutions on MaxInterview for how to delete data and add from file in c language by the best coders in the world

showing results for - "how to delete data and add from file in c language"
Abril
02 Jan 2017
1#include <stdio.h>
2#include <stdlib.h>
3
4void Menu();
5void New_Staff();
6void Delete_Staff();
7void Export_Profile();
8
9struct element{
10    char id[20];
11    char name[20];
12    char gender[20];
13    char phone[20];
14    char email[20];
15}profile;
16
17int main(void){      //The program will continue running until option 4 selected                                   
18    int a;  
19
20    for(a=0;;a++){
21       Menu();
22    }
23    return 0;
24}
25
26void Menu()      //Main Menu to select option
27{
28    int n;
29    printf("\n**********************************************");
30    printf("\nMAIN MENU-STAFF INFORMATION SYSTEM");
31    printf("\n**********************************************");
32    printf("\n1. Add profile Staff Profile");
33    printf("\n2. Delete Staff Profile");
34    printf("\n3. Export All Profiles to 'output.txt'");
35    printf("\n4. Exit");
36    printf("\n**********************************************");
37    printf("\nPlease enter your option< 1 / 2 / 3 / 4 >: ");
38    scanf("%d", &n);
39
40    switch(n){
41    case 1:
42        New_Staff();
43        break;
44    case 2:
45        Delete_Staff();
46        break;
47    case 3:
48        Export_Profile();
49        break;
50    case 4:
51        printf("\n*** Thanks for using the program! Goodbye. ***");
52        exit(0);
53        break;
54    default:
55        printf("\nError! Wrong Number is Entered\nPlease Try Again");
56        break;
57    }
58}
59
60void New_Staff()                        //Add New Staff Profile 
61{   
62    int x;
63    struct element profile;
64
65    printf("\n===Add New Staff Profile===");
66    printf("\n\nPlease enter the following staff information.");
67
68    printf("\n\nStaff ID: ");
69    scanf("%s", &profile.id);
70
71    printf("Name\t: ");
72    fflush(stdin);
73    fgets(profile.name,20,stdin);
74
75    for(x=0 ; x<20 ; x++)
76    {
77    if(profile.name[x] == '\n')
78        profile.name[x] = '\0';
79    }
80
81    printf("Gender\t: ");
82    scanf("%s", &profile.gender);
83
84    printf("Phone\t: ");
85    scanf("%s", &profile.phone);
86
87    printf("Email\t: ");
88    scanf("%s", &profile.email);
89
90    printf("\nSYSTEM: New Staff Profile is Added Successfully.");
91
92}
93
94void Delete_Staff()         //Delete Staff Profile
95{                   
96    FILE *fRead, *fWrite;
97    char *TextFile;
98    char c;
99    int Delete_Id, temp = 1;
100
101    TextFile="output.txt";
102
103    fRead = fopen(TextFile, "r");
104    c = getc(fRead);
105
106    while (c != EOF){
107    printf("%c", c);
108    c = getc(fRead); 
109    }
110
111    rewind(fRead);
112
113    printf("\nDelete Staff with ID: ");
114    scanf("%d", &Delete_Id);
115
116    Delete_Id=Delete_Id+1;
117
118    fWrite = fopen("copy.c", "w");
119    c = getc(fRead);
120    while (c != EOF) {
121    c = getc(fRead);
122    if (c == '\n')
123    temp++;
124    if (temp != Delete_Id){
125    putc(c, fWrite); 
126       } 
127    }
128
129    fclose(fRead);
130    fclose(fWrite);
131
132    remove(TextFile);
133
134    rename("copy.c", TextFile);
135    printf("\nThe contents of file after being modified are as follows:\n");
136
137    fRead = fopen(TextFile, "r");
138    c = getc(fRead);
139    while (c != EOF) {
140    printf("%c", c);
141    c = getc(fRead);
142    }
143
144    fclose(fRead);
145}
146
147void Export_Profile()           //Export Staff Profile to Text
148{       
149    struct element profile;
150
151    FILE *fPtr;
152    fPtr=fopen("output.txt","w");
153    FILE *fPtr1;
154    fPtr1=fopen("output.txt","a+");
155
156    if (fPtr == NULL)
157    printf("Error in opening file\n");
158    if (fPtr1 == NULL)
159    printf("Error in opening file\n");
160
161    fprintf(fPtr,"%10s %10s %10s %10s %10s","Staff","ID",
162             "Name","Gender","Phone","Email");
163
164    fprintf(fPtr1,"\n%10s %10s %10s %10s %10s", profile.id, profile.name,   
165             profile.gender, profile.phone, profile.email);
166
167    printf("\n%10s %10s %10s %10s %10s", "Staff", "ID", "Name",
168             "Gender", "Phone",  "Email");
169    printf("\n%10s %10s %10s %10s %10s", profile.id, 
170             profile.name, profile.gender, profile.phone, profile.email);
171
172    printf("\nSYSTEM: All staff profile have been exported to output.txt file");
173
174    fclose(fPtr);
175    fclose(fPtr1);
176}