-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject7.c
More file actions
218 lines (169 loc) · 6.27 KB
/
project7.c
File metadata and controls
218 lines (169 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// colan connon project 7 december 7, 2012//
#include <stdio.h>
#include <string.h>
struct inventorydata{
int recordnum;
char toolname [25];
int quanity;
double cost;
};
/* create a struct for data */
/*function prototypes */
int menu( void );
void updatetool(FILE *secondptr);
void newtool(FILE *secondptr);
void deletetool(FILE *secondptr);
void intalizefile(FILE *secondptr);
int main(void){
FILE *fptr;
int choice;
int i;
struct inventorydata blankdata = {0,"",0,0.0};
if((fptr = fopen("hardware.txt","rb+"))== NULL){
printf("ERROR!!!");
}
/*Open the file */
else{
while(( choice = menu() ) != 5){
switch(choice){
case 1:
updatetool(fptr);
break;
case 2:
newtool(fptr);
break;
case 3:
deletetool(fptr);
break;
case 4:
intalizefile(fptr);
break;
default:
printf("Not a Valid Choice\n");
break;
/*choices to get what the user wants to do */
}
}
fclose(fptr);
}
return 0;
}
int menu(void){
int choice;
/*menu function to list the choices for the user */
printf("Enter a choice\n");
printf("1) update a tool\n");
printf("2) add a new tool\n");
printf("3) delete a tool\n");
printf("4) intalize the text file and store the intial records\n");
printf("5) end the program\n");
scanf("%d" ,&choice);
return choice;
}
void newtool(FILE *secondptr){
int recordnum;
struct inventorydata data= {0,"",0,0.0};
printf("Enter a record number for your tool\n");
scanf("%d",&recordnum);
/* get input */
fseek(secondptr,(recordnum - 1) * sizeof(struct inventorydata), SEEK_SET);
fread(&data, sizeof(struct inventorydata),1,secondptr);
/*check and see if there is already a tool under that recordnumber */
if(data.recordnum != 0){
printf("record number #%d already exists\n", data.recordnum);
}
else{
/*get the new tool input and write it to the text file */
printf("enter toolname(if toolname is more than one word seperate the words with a -,quanity, and cost\n");
scanf("%s%d%lf",&data.toolname,&data.quanity,&data.cost);
data.recordnum = recordnum;
fseek(secondptr,(data.recordnum -1)* sizeof(struct inventorydata), SEEK_SET);
fwrite(&data, sizeof(struct inventorydata),1,secondptr);
}
}
void updatetool(FILE *secondptr){
int recordnum;
int quanity;
int choice;
struct inventorydata data = {0,"",0,0.0};
printf("Enter record number to update (1-100):\n");
scanf("%d",&recordnum);
/* get the record number */
fseek(secondptr,(recordnum-1)* sizeof(struct inventorydata) , SEEK_SET);
fread(&data, sizeof(struct inventorydata),1,secondptr);
/* get the information to find the tool to update */
if(data.recordnum == 0){
printf("record number %d doesn't have info\n", recordnum);
}
else{
printf("%d %s %d %lf\n" ,data.recordnum, data.toolname, data.quanity, data.cost);
printf("What do you want to update?\n");
printf("1) record number\n");
printf("2) tool name \n");
printf("3) tool quanity \n");
printf("4) tool cost \n");
scanf("%d",&choice);
/*get input */
if(choice == 1){
int newrecordnum;
printf("Enter the new tool record number\n");
scanf("%d",&newrecordnum);
data.recordnum = newrecordnum;
}
if(choice == 2){
char newtoolname [25];
printf("Enter new toolname.(If tool name is more than oneword seperate with a - \n");
scanf("%s",&newtoolname);
strcpy(data.toolname , newtoolname);
}
if(choice == 3){
int newquanity;
printf("Enter the new tool quanity\n");
scanf("%d", &newquanity);
data.quanity = newquanity;
}
if(choice == 4){
double newcost;
printf("enter the new tool cost \n");
scanf("%lf",&newcost);
data.cost = newcost;
}
fseek(secondptr,(data.recordnum-1) * sizeof(struct inventorydata ), SEEK_SET);
fwrite(&data, sizeof(struct inventorydata),1,secondptr);
/* get which part user wants to update and write that part to the file */
}
}
void deletetool(FILE *secondptr){
struct inventorydata inventory;
struct inventorydata data = {0,"",0,0.0};
int recordnum;
printf("Enter Record number to delete 1-100.\n");
scanf("%d",&recordnum);
/* get input find the record number and delete that tool */
fseek(secondptr,(recordnum -1)* sizeof(struct inventorydata), SEEK_SET);
fread(&inventory, sizeof(struct inventorydata),1,secondptr);
if(data.recordnum == 0){
printf("record number doesnt have any data\n");
}
else{
fseek(secondptr,(inventory.recordnum-1) *sizeof(struct inventorydata),SEEK_SET);
fwrite(&data,sizeof(struct inventorydata),1,secondptr);
}
}
void intalizefile(FILE *secondptr){
int i;
struct inventorydata blankdata = {0,"",0,0.0};
for( i = 1; i<=100;i++){
fwrite(&blankdata,sizeof(struct inventorydata),1,secondptr);
}
struct inventorydata data1 = {3,"Electric Sander",7,57.98};
struct inventorydata data2 = {17,"Hammer",76,11.99};
struct inventorydata data3 = {24,"Jig Saw",21,11.00};
fseek(secondptr,(data1.recordnum-1) *sizeof(struct inventorydata),SEEK_SET);
fwrite(&data1, sizeof(struct inventorydata),1,secondptr);
fseek(secondptr,(data2.recordnum-1) *sizeof(struct inventorydata),SEEK_SET);
fwrite(&data2, sizeof(struct inventorydata),1,secondptr);
fseek(secondptr,(data3.recordnum-1) *sizeof(struct inventorydata),SEEK_SET);
fwrite(&data3, sizeof(struct inventorydata),1,secondptr);
/* this function intalizes the text file and writes the 3 items to it to start it off*/
}