-
Notifications
You must be signed in to change notification settings - Fork 1
/
BufferHandling.c
107 lines (85 loc) · 3.41 KB
/
BufferHandling.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
#include <stdio.h>
#include <string.h>
#define MAX_BUFFER_SIZE 1024
#define MAX_PERSON_SIZE 256
typedef struct {
char Size[4]; // Assuming size is less than 1000 characters
char Flag;
char ID[20];
char Age[4]; // Assuming age is less than 100
char Name[50];
char Surname[50];
} Person;
void printPerson(const Person *person) {
printf("Size: %s, Flag: %c, ID: %s, Age: %s, Name: %s, Surname: %s\n",
person->Size, person->Flag, person->ID, person->Age, person->Name, person->Surname);
}
void addPersonToFile(const char *filename, const Person *newPerson) {
FILE *file = fopen(filename, "rb+");
if (file == NULL) {
printf("Error opening the file.\n");
return;
}
Person currentPerson;
fread(¤tPerson, sizeof(Person), 1, file);
// Copy the existing person to the buffer
char buffer[MAX_BUFFER_SIZE];
memcpy(buffer, ¤tPerson, sizeof(Person));
// Check if there is enough space in the buffer for the new person
int remainingSpace = MAX_BUFFER_SIZE - sizeof(Person);
int newPersonSize = atoi(newPerson->Size);
if (remainingSpace < newPersonSize) {
// Not enough space, use a second buffer
char secondBuffer[MAX_BUFFER_SIZE];
memcpy(secondBuffer, newPerson, sizeof(Person));
// Copy the first part of the new person to the first buffer
memcpy(buffer + sizeof(Person), secondBuffer, remainingSpace);
// Paste the content of the first buffer back to the file
fseek(file, 0, SEEK_SET);
fwrite(buffer, sizeof(char), sizeof(buffer), file);
// Flush the first buffer
memset(buffer, 0, sizeof(buffer));
// Copy the remaining part of the new person to the first buffer
memcpy(buffer, secondBuffer + remainingSpace, newPersonSize - remainingSpace);
// Create a new block for the remaining content of the second buffer
fseek(file, 0, SEEK_END);
fwrite(secondBuffer + remainingSpace, sizeof(char), newPersonSize - remainingSpace, file);
} else {
// Enough space, add the new person to the first buffer
memcpy(buffer + sizeof(Person), newPerson, newPersonSize);
// Paste the content of the first buffer back to the file
fseek(file, 0, SEEK_SET);
fwrite(buffer, sizeof(char), sizeof(buffer), file);
}
fclose(file);
}
int BufferHandling() {
// Example: create a new person
Person newPerson;
strcpy(newPerson.Size, "256");
newPerson.Flag = '1';
strcpy(newPerson.ID, "ID2");
strcpy(newPerson.Age, "25");
strcpy(newPerson.Name, "John");
strcpy(newPerson.Surname, "Doe");
// Print the new person
printf("New Person:\n");
printPerson(&newPerson);
// Add the new person to the file
addPersonToFile("file.txt", &newPerson);
// Print the updated content of the file
FILE *file = fopen("file.txt", "r");
if (file != NULL) {
fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
rewind(file);
char fileContent[MAX_BUFFER_SIZE + 1];
fread(fileContent, sizeof(char), fileSize, file);
fileContent[fileSize] = '\0';
printf("\nUpdated File Content:\n%s\n", fileContent);
fclose(file);
} else {
printf("Error opening the file for reading.\n");
}
return 0;
}