Code:
1 //
2 // This is a program to list presious moments figurens and keep track of those owned.
3 // This program reads a random-access file sequentially, updates data already written to the file,
4 // creates new data to be placed in the file, and deletes data previously in the file.
5 #include <stdio.h>
6
7
8 // pres Moments data structure definition
9 struct pmomentsData{
10 unsigned int itemNum; // item index number
11 char figName[25]; // name of firurine
12 char datePur[11]; // date purchased
13 float figValue; // purchase price
14 char figRetired[1]; // y if retired, n if not retired
15 char figLoc[30]; // location of piece
16 }; // end structure pmomentsData
17
18 // prototypes
19 unsigned int enterChoice(void);
20 void textFile(FILE *readPtr);
21 void updateRecord(FILE *fPtr);
22 void newRecord(FILE *fPtr);
23 void deleteRecord(FILE *fPtr);
24
25 int main(void)
26 {
27 FILE *cfPtr; // figurine.dat file pointer
28 unsigned int choice; // user's choice
29
30 // fopen opens the file: exits if file cannot be opened
31 if ((cfPtr = fopen("figurine.dat", "rb+")) == NULL){
32 puts("File could not be opened.");
33 } // end of if
34 else{
35 // enable user to specify action
36 while ((choice = enterChoice()) != 5){
37 switch (choice){
38 // create text file from record file
39 case 1:
40 textFile(cfPtr);
41 break;
42 // update record
43 case 2:
44 updateRecord(cfPtr);
45 break;
46 // create record
47 case 3:
48 newRecord(cfPtr);
49 break;
50 // delete existing record
51 case 4:
52 deleteRecord(cfPtr);
53 break;
54 // display message if user does not select valid choice
55 default:
56 puts("Incorrect choice");
57 break;
58 } // end switch
59 } // end while
60
61 fclose(cfPtr); // closes the file
62 } // end else
63 } // end main
64
65 // create formatted text file for printing
66 void textFile(FILE *readPtr)
67 {
68 FILE *writePtr; // text file pointer
69 int result; // used to test whether fread read any bytes
70
71 // create pmomrntsData file with default information
72 struct pmomentsData pmoments = { 0, "", "", 0.0, "", "" };
73
74 // fopen opens the file: exits if file cannot be opened
75 if ((writePtr = fopen("collection.txt", "w")) == NULL){
76 puts("File could not be opened.");
77 } // end if
78 else{
79 rewind(readPtr); // sets pointer to beginning of file
80 fprintf(writePtr, "%-6u%-24s%-10s%-4.2f%-2s%-29s\n",
81 "Item", "name", "Date", "Value", "Retired", "Location");
82
83 // copy all records random-access file into text file
84 while (!feof(readPtr)){
85 result = fread(&pmoments, sizeof(struct pmomentsData), 1, readPtr);
86
87 // write a single record to text file
88 if (result != 0 && pmoments.itemNum != 0){
89 fprintf(writePtr, "%6u%24s%10s%.2f%1s%29s\n",
90 pmoments.itemNum, pmoments.figName, pmoments.datePur,
91 pmoments.figValue, pmoments.figRetired, pmoments.figLoc);
92 } // end if
93 } // end while
94
95 fclose(writePtr); // close file
96 } // end else
97 } // end function textFile
98
99 // update record
100 void updateRecord(FILE *fPtr)
101 {
102 unsigned int itemnum; // item number
103 double val; // item purchase price
104
105 // crtate pmomentsData with no data in it
106 struct pmomentsData pmoments = { 0, "", "", 0.0, "", "" };
107
108 // obtain number of figurine to update
109 printf_s("%s", "Enter the number of the firurine you want to update(1 - 100) :");
110 scanf_s("%u", &itemnum);
111
112 // move file pointer to correct record in file
113 fseek(fPtr, (itemnum - 1) * sizeof(struct pmomentsData), SEEK_SET);
114
115 // read record from file
116 fread(&pmoments, sizeof(struct pmomentsData), 1, fPtr);
117
118 // display error if item does not exist
119 if (pmoments.itemNum == 0){
120 printf_s("Item #%u has no information.\n", itemnum);
121 } // end if
122 else{ // update record
123 printf_s("%-6u%-24s%-10s%-4.2f%-1s%-29s\n\n",
124 pmoments.itemNum, pmoments.figName, pmoments.datePur,
125 pmoments.figValue, pmoments.figRetired, pmoments.figLoc);
126
127 // request change from user
128 printf_s("%s", "Enter change (+ or -).");
129 scanf_s("%f", &val);
130 pmoments.figValue += val; // update record value
131
132 printf_s("%-6u%-24s%-10s%-4.2f%-1s%-29s\n\n",
133 pmoments.itemNum, pmoments.figName, pmoments.datePur,
134 pmoments.figValue, pmoments.figRetired, pmoments.figLoc);
135
136 // move file pointer to correct record file
137 fseek(fPtr, (itemnum - 1) * sizeof(struct pmomentsData), SEEK_SET);
138
139 // write updated record over old record in file
140 fwrite(&pmoments, sizeof(struct pmomentsData), 1, fPtr);
141 } // end else
142 } // end function updateRecord
143
144 // delete an existing record
145 void d145eleteRecord(FILE *fPtr)
146 {
147 struct pmomentsData pmoments; // stores record read from file
148 struct pmomentsData blankpmoments = { 0, "", "", 0.0, "", "" };
149
150 unsigned int itemNum; // item number
151
152 // obtain number of item to delete
153 printf_s("%s", "Enter item number to delete (1 - 100):");
154 scanf_s("%u", &itemNum);
155
156 // move to correct record in file
157 fseek(fPtr, (itemNum - 1) * sizeof(struct pmomentsData), SEEK_SET);
158
159 // read record from file
160 fread(&pmoments, sizeof(struct pmomentsData), 1, fPtr);
161
162 // display error if record does not exist
163 if (pmoments.itemNum == 0 ){
164 printf_s("Item %d does not exist.\n", itemNum);
165 } // end if
166 else { // delete record
167 // move file pointer to correct record in file
168 fseek(fPtr, (itemNum - 1) * sizeof(struct pmomentsData), SEEK_SET);
169
170 // replace existing record with blank record
171 fwrite(&blankpmoments, sizeof(struct pmomentsData), 1, fPtr);
172 } // end else
173 } // end function deleteRecord
174
175 // create and insert record
176 void newRecord(FILE *fPtr)
177 {
178 // create pmomentsData with default information
179 struct pmomentsData pmoments = { 0,"", "", 0.0, "", "" };
180
181 unsigned int itemNum; // item number
182
183 // obtain number of item to create
184 printf_s("%s", "Enter new item number (1 - 100):");
185 scanf("%d", &itemNum);
186
187 // move file pointer to correct record in file
188 fseek(fPtr, (itemNum - 1) * sizeof(struct pmomentsData), SEEK_SET);
189
190 // read record from file
191 fread(&pmoments, sizeof(struct pmomentsData), 1, fPtr);
192
193 // display error if item already exists
194 if (pmoments.itemNum != 0) {
195 printf_s("Item #%d already contains information.\n", pmoments.itemNum);
196 } // end if
197 else{ // create record
198 // user enters name date price retired and location
199 printf_s("%s", "Enter name, date of purchase, purchase price, retied, and location\n? ");
200 scanf_s("%24s%10s%.2f%1s%29s\n\n",
201 &pmoments.figName, &pmoments.datePur,
202 &pmoments.figValue, &pmoments.figRetired, &pmoments.figLoc);
203
204 pmoments.itemNum = itemNum;
205
206 // move file pointer to correct record in file
207 fseek(fPtr, (pmoments.itemNum - 1) * sizeof(struct pmomentsData), SEEK_SET);
208
209 // insert record in file
210 fwrite(&pmoments, sizeof(struct pmomentsData), 1, fPtr);
211 } // end else
212 } // end function newRecord
213
214 // enable user to enter choice
215 unsigned int enterChoice(void)
216 {
217 unsigned int menuChoice; // var to store user's choice
218
219 // display available options
220 printf_s("%s", "\nEnter your choice\n"
221 "1 - store a formatted text file of items called\n"
222 " \collection.txt\" for printing\n"
223 "2 - update an item\n"
224 "3 - add a new item\n"
225 "4 - delete an item\n"
226 "5 - end program\n? ");
227
228
229 scanf_s("%u", &menuChoice); // recieve choice from user
230 return menuChoice;
231 } // end function enterChoice