-
Notifications
You must be signed in to change notification settings - Fork 1
/
TrueSearch.c
72 lines (57 loc) · 2.32 KB
/
TrueSearch.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 1024
void searchPersonById(const char *idToSearch) {
FILE *file = fopen("users.txt", "r");
if (file == NULL) {
printf("Error opening users file.\n");
exit(1);
}
// Create buffer for processing
char buffer[BUFFER_SIZE];
// Initialize the buffer
memset(buffer, 0, sizeof(buffer));
while (fgets(buffer, sizeof(buffer), file) != NULL) {
// Check if ID is found in the current block
char *idLocation = strstr(buffer, idToSearch);
if (idLocation != NULL) {
// Find the position of the specified ID
int position = idLocation - buffer;
// Extract the ID
printf("Your ID: %s\n", idToSearch);
// Extract the age
char *ageStart = strchr(buffer + position, '#') + 1; // Move to the character after the first #
char *ageEnd = strchr(ageStart, '#'); // Find the next #
*ageEnd = '\0'; // Null-terminate to print only the age
printf("Your age: %s\n", ageStart);
// Extract the name
char *nameStart = ageEnd + 1; // Move to the character after the second #
char *nameEnd = strchr(nameStart, '#'); // Find the next #
*nameEnd = '\0'; // Null-terminate to print only the name
printf("Your name: %s\n", nameStart);
// Extract the surname
char *surnameStart = nameEnd + 1; // Move to the character after the third #
char *surnameEnd = strchr(surnameStart, '%'); // Find the next %
*surnameEnd = '\0'; // Null-terminate to print only the surname
printf("Your surname: %s\n", surnameStart);
// Close the file
fclose(file);
return;
}
}
// ID not found
printf("ID not found in the file.\n");
// Close the file
fclose(file);
}
int main() {
// Prompt user for the ID to search
char idToSearch[BUFFER_SIZE];
printf("Enter the ID to search: ");
fgets(idToSearch, sizeof(idToSearch), stdin);
idToSearch[strcspn(idToSearch, "\n")] = '\0'; // Remove newline character
// Call the search function
searchPersonById(idToSearch);
return 0;
}