1414``` c
1515#include < stdio.h>
1616#include < string.h>
17+ 18+ // Define a structure to represent a book
1719typedef struct book
1820{
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
2224} book_t ;
25+ 26+ // Function to read books from a file and write them to separate files based on their type
2327int print (char filename[ ] )
2428{
2529 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
2832 {
2933 printf("File Not Found");
3034 return 0;
3135 }
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
3541 while (fscanf(fp, " %c, %[^,],%[^\n]\n", &b.type, b.name, b.author) != EOF)
3642 {
37- if (b.type == 'b')
43+ if (b.type == 'b') // If the book is of type 'b', write it to both output files
3844 {
3945 fprintf(fp1, "%s, %s\n", b.name, b.author);
4046 fprintf(fp2, "%s, %s\n", b.name, b.author);
4147 }
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
4349 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
4551 fprintf(fp2, "%s, %s\n", b.name, b.author);
4652 }
53+ 54+ // Close all open files
4755 fclose(fp1);
4856 fclose(fp2);
4957 fclose(fp);
58+ 5059 return 0;
5160}
61+ 5262int main()
5363{
5464 int i;
5565 char filename[ 20] ;
66+ 67+ // Prompt the user for the name of the input file
5668 printf("Enter the name of the file where the records are kept: ");
5769 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+ 6075 return 0;
6176}
6277```
@@ -76,55 +91,77 @@ int main()
7691```c
7792#include <stdio.h>
7893#include <string.h>
94+
95+ // Define a structure for a vehicle with registration number, name, and address.
7996typedef struct vehicle
8097{
8198 char reg[7];
8299 char name[30];
83100 char addr[30];
84101} vehicle_t;
102+
103+ // Function to search and print records from a file.
85104int print(char filename[])
86105{
87106 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.
90109 {
91110 printf("FILE NOT FOUND!\n");
92111 return 0;
93112 }
94- fclose(fp);
113+ fclose(fp); // Close the file immediately after checking its existence.
114+
95115 vehicle_t v;
96116 int n, flag = 1;
97117 char rg[7];
118+
119+ // Get the number of records the user wants to search.
98120 printf("Enter the number of records you want to search: ");
99121 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.
101124 {
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.
103128 printf("\nEnter the registration number: ");
104129 scanf(" %s", rg);
105130
131+ // Read records from the file and search for the given registration number.
106132 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.
108135 {
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.
112139 }
140+ }
141+
113142 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.
117147 }
148+
118149 return 0;
119150}
151+
120152int main()
121153{
122154 int i;
123155 char filename[20];
156+
157+ // Ask the user to input the name of the file.
124158 printf("Enter the name of the file: ");
125159 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+
128165 return 0;
129166}
130167```
@@ -146,39 +183,50 @@ int main()
146183``` c
147184#include < stdio.h>
148185#include < string.h>
186+ 187+ // Function to read events from a file and count the number of events on each date
149188void check (char filename1[ ] , char filename2[ ] )
150189{
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
153192 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
156197 while (fscanf(fp2, "%d\n", &day1) != EOF)
157198 {
199+ // Read events from the first file and check if they match the current date
158200 while (fscanf(fp1, "%d, %[^\n]\n", &day2, events) != EOF)
159201 {
160- if (day2 == day1)
202+ if (day2 == day1) // If the event date matches the current date, increment the count
161203 count++;
162- else
204+ else // If the event date does not match the current date, print the count and reset it
163205 {
164206 printf("%d %d\n", day1, count);
165207 count = 1;
166208 break;
167209 }
168210 }
169211 }
170- printf("%d %d\n", day1, count);
212+ printf("%d %d\n", day1, count); // Print the final count
171213}
214+ 172215int main()
173216{
174217 char filename1[ 20] , filename2[ 20] ;
218+ 219+ // Prompt the user for the names of the input files
175220 printf("Enter the filename containing the events: ");
176221 scanf("%s", filename1);
177- strcat(filename1, ".txt");
222+ strcat(filename1, ".txt"); // Append ".txt" to the filename
223+ 178224 printf("Enter the filename containing the dates: ");
179225 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+ 182230 return 0;
183231}
184232```
0 commit comments