-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDelete.c
177 lines (139 loc) · 5.65 KB
/
Delete.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 1024
#define BLOCK_SEPARATOR "*"
// Structure to represent an individual
struct Person {
char *id;
char *age;
char *name;
char *surname;
char flag; // Use char for flag
char *size; // Dynamically allocated for unknown length
struct Person *next;
};
// Function to dynamically allocate memory for a string input
char *getUserInput(const char *prompt);
// Function to delete a person from the file (logically)
void deletePersonFromFile(const char *idToDelete);
int main() {
// Prompt user for the ID to delete
char *idToDelete = getUserInput("Enter the ID to delete:");
// Test the delete function
deletePersonFromFile(idToDelete);
// Free allocated memory for user input
free(idToDelete);
return 0;
}
void deletePersonFromFile(const char *idToDelete) {
FILE *file = fopen("users.txt", "r+");
if (file == NULL) {
printf("Error opening users file.\n");
exit(1);
}
// Create buffers for processing
char prevBlock[BUFFER_SIZE];
char currentBlock[BUFFER_SIZE];
char *idLocation;
long blockStartPosition = 0; // Position where the current block starts in the file
// Initialize the buffers
memset(prevBlock, 0, sizeof(prevBlock));
memset(currentBlock, 0, sizeof(currentBlock));
while (1) {
// Read the next character
char bufferChar = fgetc(file);
// Check for the end of file
if (bufferChar == EOF) {
// End of file reached, ID not found
printf("ID not found in the file.\n");
fclose(file);
return;
}
// Add the character to the current block
strncat(currentBlock, &bufferChar, 1);
// Check if the current block contains the block separator
if (strstr(currentBlock, BLOCK_SEPARATOR) != NULL) {
// Move the content of the current block to the previous block
strcpy(prevBlock, currentBlock);
// Update blockStartPosition to the start of the next block
blockStartPosition = ftell(file);
// Reset the current block
memset(currentBlock, 0, sizeof(currentBlock));
// Read the next block into currentBlock
fread(currentBlock, 1, BUFFER_SIZE, file);
}
// Check if ID is found in the current block
idLocation = strstr(currentBlock, idToDelete);
if (idLocation != NULL) {
break;
}
}
// Locate the position before the last '#' before the specified ID in the current block
long positionBeforeLastHash = blockStartPosition + (idLocation - currentBlock - 2);
if (positionBeforeLastHash >= 0) {
// Go back and set the character before the last '#' to '0'
fseek(file, positionBeforeLastHash, SEEK_SET);
fputc('0', file);
fflush(file); // Flush changes to file
}
// Close the file
fclose(file);
printf("Person with ID %s logically deleted from the file.\n", idToDelete);
}
// Function to dynamically allocate memory for a string input
char *getUserInput(const char *prompt) {
printf("%s", prompt);
char *buffer = NULL;
size_t bufferSize = 0;
// Read the input using getline
if (getline(&buffer, &bufferSize, stdin) == -1) {
printf("Error reading input.\n");
exit(1);
}
// Remove newline character if present
size_t length = strlen(buffer);
if (length > 0 && buffer[length - 1] == '\n') {
buffer[length - 1] = '\0';
} else {
// Clear the input buffer if necessary
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
return buffer;
}
// Function to calculate the size string
void calculateSize(struct Person *person) {
// Calculate the size of the data for this person
int dataSize = strlen(person->id) + strlen(person->age) + strlen(person->name) +
strlen(person->surname) + 7; // +5 accounts for '#' and '%'
// Calculate the size of the entire string, including the size variable
int totalSize = dataSize + snprintf(NULL, 0, "%d", dataSize);
// Convert totalSize to a string and store it in the size variable
snprintf(person->size, BUFFER_SIZE, "%d", totalSize);
}
// Function to add a person to the buffer
void addPersonToBuffer(struct Person *person, char *buffer, int *bufferIndex, int *remainingBufferSpace) {
// Calculate the space needed for the person's data
int spaceNeeded = strlen(person->id) + strlen(person->age) + strlen(person->name) +
strlen(person->surname) + strlen(person->size) + 7; // +5 accounts for '#' and '%'
// Check if there is enough space in the buffer
if (spaceNeeded > *remainingBufferSpace) {
// Not enough space, reset buffer index and space
*bufferIndex = 0;
*remainingBufferSpace = BUFFER_SIZE;
}
// Add the person's data to the buffer
sprintf(buffer + *bufferIndex, "%s#%c#%s#%s#%s#%s%%",
person->size, person->flag, person->id, person->age, person->name, person->surname);
// Update buffer index and remaining space
*bufferIndex += spaceNeeded;
*remainingBufferSpace -= spaceNeeded;
}
// Function to write the buffer to the file
void writeBufferToFile(FILE *file, char *buffer, int *bufferIndex, int *remainingBufferSpace) {
// Write the buffer to the file if it's not empty
if (*remainingBufferSpace < BUFFER_SIZE) {
fwrite(buffer, 1, *bufferIndex, file);
}
}