-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdatedfile.c
173 lines (139 loc) · 5.29 KB
/
updatedfile.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 1024
// 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 save individual details to a text file
void saveToFile(struct Person *person, FILE *file) {
fprintf(file, "%s#%c#%s#%s#%s#", person->size, person->flag, person->id, person->age, person->name);
// Add '%' at the end of the surname
fprintf(file, "%s%%", person->surname);
}
// Function to finalize the person details
void finalizePerson(struct Person *person);
// Function to calculate the size string
void calculateSize(struct Person *person);
int main() {
// Declare a pointer to the head of the linked list
struct Person *head = NULL;
// Check if the users file is not empty and clear it if needed
FILE *usersFile = fopen("users.txt", "r+");
if (usersFile != NULL) {
fseek(usersFile, 0, SEEK_END);
long size = ftell(usersFile);
// Clear the file if it's not empty
if (size > 0) {
freopen("users.txt", "w", usersFile);
} else {
// Add a newline if the file is empty
fprintf(usersFile, "\n");
}
} else {
printf("Error opening users file.\n");
exit(1);
}
int numClasses;
// Prompt the user for the number of classes
printf("Enter the number of classes: ");
scanf("%d", &numClasses);
// Clear the newline character from the input buffer
while (getchar() != '\n');
// Loop to add individuals to the linked list
for (int i = 0; i < numClasses; ++i) {
// Dynamically allocate memory for the new person
struct Person *newPerson = (struct Person *)malloc(sizeof(struct Person));
if (newPerson == NULL) {
printf("Error: Memory allocation failed.\n");
exit(1);
}
// Input custom ID, age, name, and surname
newPerson->id = getUserInput("Enter custom ID (a number):");
newPerson->age = getUserInput("Enter age:");
newPerson->name = getUserInput("Enter name:");
newPerson->surname = getUserInput("Enter surname:");
newPerson->flag = '0'; // Initially set the flag to 0
// Allocate memory for the size variable
newPerson->size = (char *)malloc(BUFFER_SIZE);
if (newPerson->size == NULL) {
printf("Error: Memory allocation failed.\n");
exit(1);
}
// Calculate and set the size string
calculateSize(newPerson);
// Set the flag to 1
newPerson->flag = '1';
// Assign the next pointer
newPerson->next = head;
// Update the head to point to the new person
head = newPerson;
// Save details to the user file
saveToFile(newPerson, usersFile);
}
fclose(usersFile);
// Display the entered information
printf("\nDetails of individuals:\n");
// Traverse the linked list and print the details
struct Person *current = head;
int i = 1;
while (current != NULL) {
printf("\nIndividual %d:\n", i++);
printf("Name: %s\n", current->name);
printf("Surname: %s\n", current->surname);
printf("Age: %s\n", current->age);
printf("Custom ID: %s\n", current->id);
printf("Size: %s\n", current->size);
// Move to the next person in the list
struct Person *temp = current;
current = current->next;
// Free allocated memory for this person
free(temp->id);
free(temp->surname);
free(temp->age);
free(temp->name);
free(temp->size); // Free dynamically allocated size
free(temp);
}
return 0;
}
// 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);
}