14
14
``` c
15
15
#include < stdio.h>
16
16
#include < string.h>
17
+
18
+ // Define a structure to represent a book
17
19
typedef struct book
18
20
{
19
- char type;
20
- char name[50];
21
- char author[20];
21
+ char type; // Type of book: 'p' for paperback, 'h' for hardback, 'b' for both
22
+ char name[50]; // Name of the book
23
+ char author[20]; // Author of the book
22
24
} book_t ;
25
+
26
+ // Function to read books from a file and write them to separate files based on their type
23
27
int print (char filename[ ] )
24
28
{
25
29
FILE * fp, * fp1, * fp2;
26
- fp = fopen(filename, "r");
27
- if (fp == NULL)
30
+ fp = fopen(filename, "r"); // Open the input file for reading
31
+ if (fp == NULL) // Check if the file was opened successfully
28
32
{
29
33
printf("File Not Found");
30
34
return 0;
31
35
}
32
- book_t b;
33
- fp1 = fopen("Paperbacks.txt", "w");
34
- fp2 = fopen("Hardbacks.txt", "w");
36
+ book_t b; // Create a book variable to store the data read from the file
37
+ fp1 = fopen("Paperbacks.txt", "w"); // Open the output file for paperbacks
38
+ fp2 = fopen("Hardbacks.txt", "w"); // Open the output file for hardbacks
39
+
40
+ // Read books from the input file and write them to the appropriate output files
35
41
while (fscanf(fp, " %c, %[^,],%[^\n]\n", &b.type, b.name, b.author) != EOF)
36
42
{
37
- if (b.type == 'b')
43
+ if (b.type == 'b') // If the book is of type 'b', write it to both output files
38
44
{
39
45
fprintf(fp1, "%s, %s\n", b.name, b.author);
40
46
fprintf(fp2, "%s, %s\n", b.name, b.author);
41
47
}
42
- else if (b.type == 'p')
48
+ else if (b.type == 'p') // If the book is of type 'p', write it to the paperbacks file
43
49
fprintf(fp1, "%s, %s\n", b.name, b.author);
44
- else if (b.type == 'h')
50
+ else if (b.type == 'h') // If the book is of type 'h', write it to the hardbacks file
45
51
fprintf(fp2, "%s, %s\n", b.name, b.author);
46
52
}
53
+
54
+ // Close all open files
47
55
fclose(fp1);
48
56
fclose(fp2);
49
57
fclose(fp);
58
+
50
59
return 0;
51
60
}
61
+
52
62
int main()
53
63
{
54
64
int i;
55
65
char filename[ 20] ;
66
+
67
+ // Prompt the user for the name of the input file
56
68
printf("Enter the name of the file where the records are kept: ");
57
69
scanf("%s", filename);
58
- strcat(filename, ".txt");
59
- i = print(filename);
70
+
71
+ strcat(filename, ".txt"); // Append ".txt" to the filename
72
+
73
+ i = print(filename); // Call the print function with the filename as argument
74
+
60
75
return 0;
61
76
}
62
77
```
@@ -76,55 +91,77 @@ int main()
76
91
```c
77
92
#include <stdio.h>
78
93
#include <string.h>
94
+
95
+ // Define a structure for a vehicle with registration number, name, and address.
79
96
typedef struct vehicle
80
97
{
81
98
char reg[7];
82
99
char name[30];
83
100
char addr[30];
84
101
} vehicle_t;
102
+
103
+ // Function to search and print records from a file.
85
104
int print(char filename[])
86
105
{
87
106
FILE *fp;
88
- fp = fopen(filename, "r");
89
- if (fp == NULL)
107
+ fp = fopen(filename, "r"); // Open the file in read mode.
108
+ if (fp == NULL) // Check if the file couldn't be opened.
90
109
{
91
110
printf("FILE NOT FOUND!\n");
92
111
return 0;
93
112
}
94
- fclose(fp);
113
+ fclose(fp); // Close the file immediately after checking its existence.
114
+
95
115
vehicle_t v;
96
116
int n, flag = 1;
97
117
char rg[7];
118
+
119
+ // Get the number of records the user wants to search.
98
120
printf("Enter the number of records you want to search: ");
99
121
scanf("%d", &n);
100
- for (int i = 0; i < n; i++)
122
+
123
+ for (int i = 0; i < n; i++) // Loop to search for 'n' records.
101
124
{
102
- fp = fopen(filename, "r");
125
+ fp = fopen(filename, "r"); // Re-open the file for each record search.
126
+
127
+ // Ask the user for the registration number to search.
103
128
printf("\nEnter the registration number: ");
104
129
scanf(" %s", rg);
105
130
131
+ // Read records from the file and search for the given registration number.
106
132
while (fscanf(fp, "%[^,],%[^,],%[^\n]\n", v.reg, v.name, v.addr) != EOF)
107
- if (strcmp(v.reg, rg) == 0)
133
+ {
134
+ if (strcmp(v.reg, rg) == 0) // If the registration number matches.
108
135
{
109
- printf("%s %s %s\n", v.reg, v.name, v.addr);
110
- flag = 0;
111
- break;
136
+ printf("%s %s %s\n", v.reg, v.name, v.addr); // Print the vehicle's details.
137
+ flag = 0; // Set the flag to indicate that a match was found.
138
+ break; // Exit the loop since the record was found.
112
139
}
140
+ }
141
+
113
142
if (flag)
114
- printf("Not Found\n");
115
- flag = 1;
116
- fclose(fp);
143
+ printf("Not Found\n"); // If the flag is not changed, the record was not found.
144
+
145
+ flag = 1; // Reset the flag for the next iteration.
146
+ fclose(fp); // Close the file after searching for the current record.
117
147
}
148
+
118
149
return 0;
119
150
}
151
+
120
152
int main()
121
153
{
122
154
int i;
123
155
char filename[20];
156
+
157
+ // Ask the user to input the name of the file.
124
158
printf("Enter the name of the file: ");
125
159
scanf(" %s", filename);
126
- strcat(filename, ".txt");
127
- i = print(filename);
160
+
161
+ strcat(filename, ".txt"); // Concatenate ".txt" to the filename to form the complete filename.
162
+
163
+ i = print(filename); // Call the print function to search and print records from the file.
164
+
128
165
return 0;
129
166
}
130
167
```
@@ -146,39 +183,50 @@ int main()
146
183
``` c
147
184
#include < stdio.h>
148
185
#include < string.h>
186
+
187
+ // Function to read events from a file and count the number of events on each date
149
188
void check (char filename1[ ] , char filename2[ ] )
150
189
{
151
- int day1, day2, count = 0;
152
- char events[ 20] ;
190
+ int day1, day2, count = 0; // Variables to store the current date and event count
191
+ char events[ 20] ; // Variable to store the event name
153
192
FILE * fp1, * fp2;
154
- fp1 = fopen(filename1, "r");
155
- fp2 = fopen(filename2, "r");
193
+ fp1 = fopen(filename1, "r"); // Open the file containing the events
194
+ fp2 = fopen(filename2, "r"); // Open the file containing the dates
195
+
196
+ // Read dates from the second file and count the number of events on each date
156
197
while (fscanf(fp2, "%d\n", &day1) != EOF)
157
198
{
199
+ // Read events from the first file and check if they match the current date
158
200
while (fscanf(fp1, "%d, %[^\n]\n", &day2, events) != EOF)
159
201
{
160
- if (day2 == day1)
202
+ if (day2 == day1) // If the event date matches the current date, increment the count
161
203
count++;
162
- else
204
+ else // If the event date does not match the current date, print the count and reset it
163
205
{
164
206
printf("%d %d\n", day1, count);
165
207
count = 1;
166
208
break;
167
209
}
168
210
}
169
211
}
170
- printf("%d %d\n", day1, count);
212
+ printf("%d %d\n", day1, count); // Print the final count
171
213
}
214
+
172
215
int main()
173
216
{
174
217
char filename1[ 20] , filename2[ 20] ;
218
+
219
+ // Prompt the user for the names of the input files
175
220
printf("Enter the filename containing the events: ");
176
221
scanf("%s", filename1);
177
- strcat(filename1, ".txt");
222
+ strcat(filename1, ".txt"); // Append ".txt" to the filename
223
+
178
224
printf("Enter the filename containing the dates: ");
179
225
scanf("%s", filename2);
180
- strcat(filename2, ".txt");
181
- check(filename1, filename2);
226
+ strcat(filename2, ".txt"); // Append ".txt" to the filename
227
+
228
+ check(filename1, filename2); // Call the check function with the filenames as arguments
229
+
182
230
return 0;
183
231
}
184
232
```
0 commit comments