File Management & Organization
File Management & Organization
TASK ONE:
part 1.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
fclose(file);
printf("File initialized with %d records.\n", INITIAL_RECORDS);
}
int main() {
// Initialize file with sample data if it doesn't exist
FILE *file = fopen(FILENAME, "rb");
if (!file) {
initialize_file();
} else {
fclose(file);
}
// Load the avail list from the file (if there are any deleted records)
file = open_file("rb");
StudentRecord record;
int index = 0;
while (fread(&record, sizeof(StudentRecord), 1, file)) {
if (strlen(record.student_id) == 0) {
AvailableRecord *new_avail =
(AvailableRecord*)malloc(sizeof(AvailableRecord));
new_avail->index = index;
new_avail->next = avail_list;
avail_list = new_avail;
}
index++;
}
fclose(file);
return 0;
}
TASK ONE:
part 2.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char id[ID_SIZE];
char *name;
char *score;
char *dob;
} Record;
typedef struct {
AvailListEntry *entries;
size_t size;
size_t count;
} AvailList;
void init_avail_list() {
avail_list.entries = malloc(avail_list.size * sizeof(AvailListEntry));
}
void resize_avail_list() {
avail_list.size *= 2;
avail_list.entries = realloc(avail_list.entries, avail_list.size *
sizeof(AvailListEntry));
}
void initialize_file() {
FILE *file = fopen(FILENAME, "wb");
if (!file) {
perror("Error opening file for initialization");
return;
}
fclose(file);
}
void add_record() {
FILE *file = fopen(FILENAME, "rb+");
if (!file) file = fopen(FILENAME, "wb+");
if (!file) {
perror("Error opening file");
return;
}
Record record;
printf("Enter ID: ");
scanf("%9s", record.id);
record.name = .name = malloc(30);
printf("Enter Name: ");
scanf("%29s", record.name);
record.score = malloc(10);
printf("Enter Score: ");
scanf("%9s", record.score);
record.dob = malloc(20);
printf("Enter Date of Birth: ");
scanf("%19s", record.dob);
free_record(&record);
}
Record record;
long offset = 0;
while (fread(&record, ID_SIZE, 1, file)) {
read_record(file, offset, &record);
if (strcmp(record.id, id) == 0) {
long length = ftell(file) - offset;
add_to_avail_list(offset, length);
free_record(&record);
break;
}
offset += sizeof(long) + strlen(record.name) + 1 + strlen(record.score) + 1
+ strlen(record.dob) + 1;
}
fclose(file);
}
void update_record(const char *id) {
FILE *file = fopen(FILENAME, "rb+");
if (!file) {
perror("Error opening file");
return;
}
Record record;
long offset = 0;
while (fread(&record, ID_SIZE, 1, file)) {
read_record(file, offset, &record);
if (strcmp(record.id, id) == 0) {
printf("Enter new Name: ");
scanf("%29s", record.name);
printf("Enter new Score: ");
scanf("%9s", record.score);
printf("Enter new Date of Birth: ");
scanf("%19s", record.dob);
fclose(file);
}
Record record;
long offset = 0;
while (fread(&record, ID_SIZE, 1, file)) {
read_record(file, offset, &record);
if (strcmp(record.id, id) == 0) {
printf("ID: %s\n", record.id);
printf("Name: %s\n", record.name);
printf("Score: %s\n", record.score);
printf("Date of Birth: %s\n", record.dob);
free_record(&record);
break;
}
offset += sizeof(long) + strlen(record.name) + 1 + strlen(record.score) + 1
+ strlen(record.dob) + 1;
}
fclose(file);
}
int main() {
int choice;
char id[ID_SIZE];
init_avail_list();
while (1) {
printf("\nMenu:\n");
printf("1. Add Record\n");
printf("2. Delete Record\n");
printf("3. Update Record\n");
printf("4. Display Record\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_record();
break;
case 2:
printf("Enter ID to delete: ");
scanf("%9s", id);
delete_record(id);
break;
case 3:
printf("Enter ID to update: ");
scanf("%9s", id);
update_record(id);
break;
case 4:
printf("Enter ID to display: ");
scanf("%9s", id);
display_record(id);
break;
case 5:
free(avail_ list.entries);
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
TASK TWO:
part 1.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef struct {
char id[MAX_ID_LENGTH];
char *name;
char *score;
char *dob;
} Record;
long record_size;
if (fread(&record_size, sizeof(long), 1, file) != 1) {
fprintf(stderr, "Error: Failed to read record size.\n");
return;
}
record->name = strdup(buffer);
record->score = strdup(buffer + strlen(buffer) + 1);
record->dob = strdup(buffer + strlen(buffer) + 1 + strlen(record->score) + 1);
free(buffer);
Record record;
strncpy(record.id, id, MAX_ID_LENGTH);
record.name = strdup(name);
record.score = strdup(score);
record.dob = strdup(dob);
fseek(file, 0, SEEK_END);
long offset = ftell(file);
fclose(file);
Record record;
long offset = 0;
while (1) {
if (fread(&record.id, MAX_ID_LENGTH, 1, file) != 1) break;
read_record(file, offset, &record);
if (strcmp(record.id, id) == 0) {
printf("Record found at offset %ld\n", offset);
fclose(file);
return offset;
}
fclose(file);
printf("Record not found.\n");
return -1;
}
start = clock();
long result = find_record(id);
end = clock();
if (result != -1) {
printf("Record with ID %s found at offset %ld.\n", id, result);
} else {
printf("Record with ID %s not found.\n", id);
}
int main() {
// Add 60 records to the file
for (int i = 1; i <= 60; i++) {
char id[MAX_ID_LENGTH];
char name[20];
char score[5];
char dob[15];
return 0;
}
TASK TWO:
part 2.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char id[10]; // Fixed-length ID
char name[30]; // Fixed-length Name
char score[10]; // Fixed-length Score
char dob[15]; // Fixed-length Date of Birth (DOB in format YYYY-MM-DD)
} StudentRecord;
StudentRecord records[TOTAL_RECORDS] = {
{"10001", "John Smith", "87", "2004-01-15"},
{"10002", "Emma Johnson", "92", "2003-05-22"},
{"10003", "Liam Williams", "78", "2005-11-09"},
{"10004", "Olivia Brown", "85", "2004-02-18"},
{"10005", "Noah Jones", "90", "2003-07-25"},
{"10006", "Ava Garcia", "88", "2004-03-12"},
{"10007", "Lucas Martinez", "83", "2005-10-30"},
{"10008", "Mia Hernandez", "91", "2003-06-15"},
{"10009", "Mason Clark", "86", "2004-04-20"},
{"10010", "Amelia Lopez", "89", "2003-12-05"},
{"10011", "Ethan Perez", "84", "2005-09-17"},
{"10012", "Sophia Hall", "93", "2003-03-28"},
{"10013", "James Young", "77", "2004-08-01"},
{"10014", "Isabella Allen", "82", "2005-07-22"},
{"10015", "Benjamin King", "87", "2003-04-16"},
{"10016", "Charlotte Wright", "88", "2004-05-09"},
{"10017", "Henry Scott", "81", "2005-12-11"},
{"10018", "Harper Green", "91", "2003-02-14"},
{"10019", "Alexander Baker", "80", "2004-06-07"},
{"10052", "Leonardo Ferraro", "88", "2005-08-19"},
{"10053", "Chiara Costa", "84", "2004-07-04"},
{"10054", "Alessandro Rizzo", "89", "2003-01-30"},
{"10055", "Martina Marino", "87", "2005-09-12"},
{"10056", "Tommaso Gallo", "90", "2004-02-16"},
{"10057", "Giorgia Romano", "91", "2003-10-24"},
{"10058", "Matteo Greco", "85", "2005-01-08"},
{"10059", "Beatrice Esposito", "88", "2004-11-27"},
{"10060", "Daniele Leone", "83", "2003-05-13"},
{"10061", "Federico Ricci", "90", "2005-06-29"},
{"10062", "Sofia Conti", "86", "2004-12-20"},
{"10063", "Francesco Lombardi", "82", "2003-07-25"},
{"10064", "Alessandro Moretti", "88", "2005-04-02"},
{"10065", "Giulia Romano", "92", "2004-11-05"},
{"10066", "Matteo Fontana", "85", "2005-08-03"},
{"10067", "Alessia Gallo", "90", "2003-09-09"},
{"10068", "Luca Russo", "87", "2005-03-15"},
{"10069", "Arianna Bianchi", "89", "2004-10-14"},
{"10070", "Niccolo Guerra", "84", "2003-02-25"},
{"10071", "Ludovica Colombo", "81", "2005-05-05"},
{"10072", "Alessio Rizzo", "88", "2004-09-01"},
{"10073", "Martina Moretti", "85", "2005-06-10"},
{"10074", "Leonardo Ferrara", "90", "2003-08-13"},
{"10075", "Sofia Gallo", "91", "2004-12-16"},
{"10076", "Luca Marino", "86", "2005-07-23"},
{"10077", "Chiara Rizzo", "88", "2003-04-11"},
{"10078", "Alessandro Costa", "84", "2004-11-09"},
{"10079", "Matteo Rossi", "89", "2005-06-02"},
{"10080", "Giulia Gallo", "87", "2003-11-28"},
{"10081", "Francesco Romano", "91", "2004-05-21"},
{"10082", "Martina Ferraro", "90", "2005-02-18"},
{"10083", "Arianna Russo", "89", "2003-07-29"}
};
fclose(file);
}
StudentRecord records[TOTAL_RECORDS];
fread(records, sizeof(StudentRecord), TOTAL_RECORDS, file);
rewind(file);
fwrite(records, sizeof(StudentRecord), TOTAL_RECORDS, file);
fclose(file);
}
int main() {
add_records(); // Step 1: Add records to the file
sort_records(); // Step 2: Sort the records by ID
StudentRecord result;
int index = binary_search(file, search_id, &result);
if (index != -1) {
printf("Record found at index %d\n", index);
printf("ID: %s, Name: %s, Score: %s, DOB: %s\n", result.id, result.name,
result.score, result.dob);
} else {
printf("Record with ID %s not found.\n", search_id);
}
fclose(file);
return 0;
}
TASK THREE:
part 1.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char name[30];
int score;
char date[15];
char id[10];
} Record;
typedef struct {
char id[10];
long offset;
} IndexRecord;
Record records[TOTAL_RECORDS] = {
{"Adams", 89, "2003-11-23", "10021"}, {"Jackson", 84, "2005-03-30",
"10022"},
{"Grace", 92, "2004-09-10", "10023"}, {"Samuel", 85, "2003-01-19",
"10024"},
{"Chloe", 90, "2004-12-08", "10025"}, {"Daniel", 88, "2003-08-04",
"10026"},
{"Zoe", 83, "2005-05-20", "10027"}, {"Jack", 86, "2004-11-15", "10028"},
{"Lily", 91, "2003-06-25", "10029"}, {"David", 80, "2005-10-03", "10030"},
{"Ella", 82, "2004-03-19", "10031"}, {"Matthew", 87, "2003-07-28",
"10032"},
{"Scarlett", 89, "2005-02-21", "10033"}, {"Luke", 84, "2004-05-06",
"10034"},
{"Victoria", 88, "2003-11-13", "10035"}, {"Wyatt", 81, "2005-04-09",
"10036"},
{"Hannah", 92, "2004-06-30", "10037"}, {"Carter", 85, "2003-09-26",
"10038"},
{"Aria", 90, "2005-03-14", "10039"}, {"Isaac", 83, "2004-10-02", "10040"},
{"Aiden", 86, "2003-02-27", "10041"}, {"Ellie", 88, "2005-08-19", "10042"},
{"Nathan", 82, "2004-07-04", "10043"}, {"Madison", 89, "2003-01-30",
"10044"},
{"Sebastian", 87, "2005-09-12", "10045"}, {"Layla", 90, "2004-02-16",
"10046"},
{"Julian", 84, "2003-10-24", "10047"}, {"Sofia", 91, "2005-01-08",
"10048"},
{"Levi", 88, "2004-11-27", "10049"}, {"Zoey", 85, "2003-05-13", "10050"},
{"Eli", 82, "2005-06-29", "10051"}, {"Mila", 90, "2004-12-20", "10052"}
};
fclose(file);
}
IndexRecord index_records[TOTAL_RECORDS];
for (int i = 0; i < TOTAL_RECORDS; i++) {
Record record;
fseek(file, i * RECORD_SIZE, SEEK_SET);
fread(&record, sizeof(Record), 1, file);
strcpy(index_records[i].id, record.id);
index_records[i].offset = i * RECORD_SIZE;
}
IndexRecord index_records[TOTAL_RECORDS];
fread(index_records, sizeof(IndexRecord), TOTAL_RECORDS, index_file);
fclose(index_file);
int left = 0;
int right = TOTAL_RECORDS - 1;
Record record;
fseek(file, offset, SEEK_SET);
fread(&record, sizeof(Record), 1, file);
fclose(file);
printf("Record found:\n");
printf("ID: %s, Name: %s, Score: %d, Date: %s\n", record.id, record.name,
record.score, record.date);
} else {
printf("Record with ID %s not found.\n", id);
}
}
int main() {
add_records(); // Step 1: Add records to the file
create_index(); // Step 2: Create the index
return 0;
}
TASK THREE:
part 2.
fseek(index_file, 0, SEEK_END);
long size = ftell(index_file);
int count = size / sizeof(IndexEntry);
free(entries);
fclose(index_file);
return -1; // Not found
}
int main() {
FILE *file = fopen(FILENAME, "wb");
if (!file) {
perror("Error creating file");
return EXIT_FAILURE;
}
// Create and write new records with ID, Name, Score, and Date of Birth
Record records[] = {
{"10001", "John Smith", "87", "2004-01-15"},
{"10002", "Emma Johnson", "92", "2003-05-22"},
{"10003", "Liam Williams", "78", "2005-11-09"},
{"10004", "Olivia Brown", "85", "2004-02-18"},
{"10005", "Noah Jones", "90", "2003-07-25"},
{"10006", "Ava Garcia", "88", "2004-03-12"},
{"10007", "Lucas Martinez", "83", "2005-10-30"},
{"10008", "Mia Hernandez", "91", "2003-06-15"},
{"10009", "Mason Clark", "86", "2004-04-20"},
{"10010", "Amelia Lopez", "89", "2003-12-05"},
{"10011", "Ethan Perez", "84", "2005-09-17"},
{"10012", "Sophia Hall", "93", "2003-03-28"},
{"10013", "James Young", "77", "2004-08-01"},
{"10014", "Isabella Allen", "82", "2005-07-22"},
{"10015", "Benjamin King", "87", "2003-04-16"},
{"10016", "Charlotte Wright", "88", "2004-05-09"},
{"10017", "Henry Scott", "81", "2005-12-11"},
{"10018", "Harper Green", "91", "2003-02-14"},
{"10019", "Alexander Baker", "80", "2004-06-07"},
{"10020", "Evelyn Adams", "89", "2003-11-23"},
{"10021", "Jackson Nelson", "84", "2005-03-30"},
{"10022", "Grace Carter", "92", "2004-09-10"},
{"10023", "Samuel Mitchell", "85", "2003-01-19"},
{"10024", "Chloe Roberts", "90", "2004-12-08"},
{"10025", "Daniel Turner", "88", "2003-08-04"},
{"10026", "Zoe Phillips", "83", "2005-05-20"},
{"10027", "Jack Campbell", "86", "2004-11-15"},
{"10028", "Lily Parker", "91", "2003-06-25"},
{"10029", "David Evans", "80", "2005-10-03"},
{"10030", "Ella Edwards", "82", "2004-03-19"},
{"10031", "Matthew Collins", "87", "2003-07-28"},
{"10032", "Scarlett Stewart", "89", "2005-02-21"},
{"10033", "Luca Bianchi", "84", "2004-05-06"},
{"10034", "Sofia Rossi", "88", "2003-11-13"},
{"10035", "Leonardo Ricci", "81", "2005-04-09"},
{"10036", "Giulia Romano", "92", "2004-06-30"},
{"10037", "Matteo Greco", "85", "2003-09-26"},
{"10038", "Alessia Gallo", "90", "2005-03-14"},
{"10039", "Francesco Fontana", "83", "2004-10-02"},
{"10040", "Arianna Conti", "86", "2003-02-27"},
{"10041", "Alessandro Moretti", "88", "2005-08-19"},
{"10042", "Martina Esposito", "82", "2004-07-04"},
{"10043", "Niccolo Barbieri", "89", "2003-01-30"},
{"10044", "Chiara Marino", "87", "2005-09-12"},
{"10045", "Tommaso Pellegrino", "90", "2004-02-16"},
{"10046", "Ludovica Colombo", "84", "2003-10-24"},
{"10047", "Alessio Leone", "91", "2005-01-08"},
{"10048", "Beatrice Ferrara", "88", "2004-11-27"},
{"10049", "Daniele Rizzo", "85", "2003-05-13"},
{"10050", "Giorgia Guerra", "82", "2005-06-29"},
{"10051", "Federico Fabbri", "90", "2004-12-20"}
};
fclos e(file);
printf("Record Found:\n");
printf("ID: %s\n", result.id);
printf("Name: %s\n", result.name);
printf("Score: %s\n", result.score);
printf("Date of Birth: %s\n", result.dob);
free(result.name);
free(result.score);
free(result.dob);
} else {
printf("Record not found.\n");
}
return 0;
}