0% found this document useful (0 votes)
10 views20 pages

File Management & Organization

The document outlines a file management assignment in C programming, detailing two tasks involving the creation and manipulation of student records. It includes code for defining student record structures, initializing files with sample data, and functions for adding, deleting, updating, and displaying records. The assignment is aimed at enhancing understanding of file handling and data structures in C.

Uploaded by

olamohammed2101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views20 pages

File Management & Organization

The document outlines a file management assignment in C programming, detailing two tasks involving the creation and manipulation of student records. It includes code for defining student record structures, initializing files with sample data, and functions for adding, deleting, updating, and displaying records. The assignment is aimed at enhancing understanding of file handling and data structures in C.

Uploaded by

olamohammed2101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 20

File Management Assignment

Name:Ola Othman Mohamed Ahmed


ID: 512-21(2nd year)
Department: Math&Cs
Programming language: C

TASK ONE:
part 1.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define RECORD_SIZE 41 // Each record has a fixed size of 41 bytes


#define FILENAME "records.dat"
#define INITIAL_RECORDS 51 // Minimum number of records

// Structure to define a record


typedef struct {
char student_id[8]; // 8 bytes
char student_name[20]; // 20 bytes
char student_score[3]; // 3 bytes
char date_of_birth[10]; // 10 bytes
} StudentRecord; // Total: 8 + 20 + 3 + 10 = 41 bytes

// Structure to define an available record


typedef struct AvailableRecord {
int index;
struct AvailableRecord* next;
} AvailableRecord;

// Sample data provided by user


StudentRecord sample_data[INITIAL_RECORDS] = {
{"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", "Luke Morris", "84", "2004-05-06"},
{"10034", "Victoria Rogers", "88", "2003-11-13"},
{"10035", "Wyatt Patterson", "81", "2005-04-09"},
{"10036", "Hannah Bailey", "92", "2004-06-30"},
{"10037", "Carter Bell", "85", "2003-09-26"},
{"10038", "Aria Murphy", "90", "2005-03-14"},
{"10039", "Isaac Rivera", "83", "2004-10-02"},
{"10040", "Aiden Cooper", "86", "2003-02-27"},
{"10041", "Ellie Richardson", "88", "2005-08-19"},
{"10042", "Nathan Cox", "82", "2004-07-04"},
{"10043", "Madison Howard", "89", "2003-01-30"},
{"10044", "Sebastian Ward", "87", "2005-09-12"},
{"10045", "Layla Torres", "90", "2004-02-16"},
{"10046", "Julian Peterson", "84", "2003-10-24"},
{"10047", "Sofia Gray", "91", "2005-01-08"},
{"10048", "Levi Ramirez", "88", "2004-11-27"},
{"10049", "Zoey James", "85", "2003-05-13"},
{"10050", "Eli Watson", "82", "2005-06-29"},
{"10051", "Mila Brooks", "90", "2004-12-20"},
// Add more records if needed
};

// Function to open file with error handling


FILE* open_file(const char* mode) {
FILE *file = fopen(FILENAME, mode);
if (!file) {
perror("Error opening file");
exit(1);
}
return file;
}

// Function to initialize the file with sample data


void initialize_file() {
FILE *file = open_file("wb");

// Write the sample data to the file


for (int i = 0; i < INITIAL_RECORDS; i++) {
fwrite(&sample_data[i], sizeof(StudentRecord), 1, file);
}

fclose(file);
printf("File initialized with %d records.\n", INITIAL_RECORDS);
}

// Function to add a new record


void add_record(AvailableRecord **avail_list) {
FILE *file = open_file("rb+");
StudentRecord record;

// Check for available slot in avail list first


int available_slot = -1;
if (*avail_list != NULL) {
available_slot = (*avail_list)->index;
*avail_list = (*avail_list)->next;
} else {
fseek(file, 0, SEEK_END);
if (ftell(file) / sizeof(StudentRecord) >= INITIAL_RECORDS) {
printf("No empty slots available. File is full.\n");
fclose(file);
return;
}
available_slot = ftell(file) / sizeof(StudentRecord);
}

printf("Enter Student ID: ");


scanf("%7s", record.student_id); // Limit to 7 characters for 8 bytes
printf("Enter Name: ");
scanf(" %19[^\n]", record.student_name); // Limit to 19 characters for 20
bytes
printf("Enter Score: ");
scanf("%2s", record.student_score); // Limit to 2 characters for 3 bytes
o 7 characters for 8 bytes
update_record(id);
break;
case 4:
printf("Enter Student ID to display: ");
scanf("%7s", id); // Limit to 7 characters for 8 bytes
display_record(id);
break;
case 5:
printf("Exiting program.\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
}

int main() {
// Initialize file with sample data if it doesn't exist
FILE *file = fopen(FILENAME, "rb");
if (!file) {
initialize_file();
} else {
fclose(file);
}

AvailableRecord *avail_list = NULL; // Initialize the available list

// 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);

// Run the main menu


main_menu(&avail_list);

return 0;
}

TASK ONE:
part 2.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define FILENAME "var_records.dat"


#define ID_SIZE 10
#define INIT_AVAIL_LIST_SIZE 10
typedef struct {
long offset;
long length;
} AvailListEntry;

typedef struct {
char id[ID_SIZE];
char *name;
char *score;
char *dob;
} Record;

typedef struct {
AvailListEntry *entries;
size_t size;
size_t count;
} AvailList;

AvailList avail_list = {NULL, INIT_AVAIL_LIST_SIZE, 0};

// Sample dataset for testing


Record sample_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"}
};

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 add_to_avail_list(long offset, long length) {


if (avail_list.count == avail_list.size) {
resize_avail_list();
}
avail_list.entries[avail_list.count].offset = offset;
avail_list.entries[avail_list.count].length = length;
avail_list.count++;
}

long get_free_space(long length) {


for (size_t i = 0; i < avail_list.count; i++) {
if (avail_list.entries[i].length >= length) {
long offset = avail_list.entries[i].offset;
if (avail_list.entries[i].length > length) {
avail_list.entries[i].offset += length;
avail_list.entries[i].length -= length;
} else {
avail_list.entries[i] = avail_list.entries[--avail_list.count];
}
return offset;
}
}
return -1;
}

void write_record(FILE *file, long offset, Record *record) {


fseek(file, offset, SEEK_SET);
long length = ID_SIZE + strlen(record->name) + 1 + strlen(record->score) + 1 +
strlen(record->dob) + 1;
fwrite(&length, sizeof(long), 1, file);
fwrite(record->id, ID_SIZE, 1, file);
fwrite(record->name, strlen(record->name) + 1, 1, file);
fwrite(record->score, strlen(record->score) + 1, 1, file);
fwrite(record->dob, strlen(record->dob) + 1, 1, file);
}

void read_record(FILE *file, long offset, Record *record) {


fseek(file, offset, SEEK_SET);
long length;
fread(&length, sizeof(long), 1, file);
fread(record->id, ID_SIZE, 1, file);
record->name = malloc(length - ID_SIZE);
fread(record->name, length - ID_SIZE, 1, file);
char *ptr = record->name + strlen(record->name) + 1;
record->score = ptr;
ptr += strlen(record->score) + 1;
record->dob = ptr;
}

void free_record(Record *record) {


free(record->name);
free(record->score);
free(record->dob);
}

void initialize_file() {
FILE *file = fopen(FILENAME, "wb");
if (!file) {
perror("Error opening file for initialization");
return;
}

for (int i = 0; i < sizeof(sample_records) / sizeof(sample_records[0]); i++) {


Record record = sample_records[i];
long length = ID_SIZE + strlen(record.name) + 1 + strlen(record.score) + 1
+ strlen(record.dob) + 1;
long offset = get_free_space(length + sizeof(long));
if (offset == -1) {
fseek(file, 0, SEEK_END);
offset = ftell(file);
}
write_record(file, offset, &record);
}

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);

long length = ID_SIZE + strlen(record.name) + 1 + strlen(record.score) + 1 +


strlen(record.dob) + 1;
long offset = get_free_space(length + sizeof(long));
if (offset == -1) {
fseek(file, 0, SEEK_END);
offset = ftell(file);
}

write_record(file, offset, &record);


fclose(file);

free_record(&record);
}

void delete_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) {
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);

long length = ftell(file) - offset;


add_to_avail_list(offset, length);
write_record(file, offset, &record);
free_record(&record);
break;
}
offset += sizeof(long) + strlen(record.name) + 1 + strlen(record.score) + 1
+ strlen(record.dob) + 1;
}

fclose(file);
}

void display_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("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();

// Initialize the file with sample data


initialize_file();

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>

#define FILE_NAME "records.dat"


#define MAX_ID_LENGTH 10

typedef struct {
char id[MAX_ID_LENGTH];
char *name;
char *score;
char *dob;
} Record;

void write_record(FILE *file, long offset, Record *record) {


fseek(file, offset, SEEK_SET);

// Calculate the total size of the record


long record_size = MAX_ID_LENGTH + strlen(record->name) + 1 +
strlen(record->score) + 1 + strlen(record->dob) + 1;

fwrite(&record_size, sizeof(long), 1, file);


fwrite(record->id, MAX_ID_LENGTH, 1, file);
fwrite(record->name, strlen(record->name) + 1, 1, file);
fwrite(record->score, strlen(record->score) + 1, 1, file);
fwrite(record->dob, strlen(record->dob) + 1, 1, file);

printf("Written Record: ID=%s, Name=%s, Score=%s, DOB=%s, Offset=%ld\n",


record->id, record->name, record->score, record->dob, offset);
}

void read_record(FILE *file, long offset, Record *record) {


fseek(file, offset, SEEK_SET);

long record_size;
if (fread(&record_size, sizeof(long), 1, file) != 1) {
fprintf(stderr, "Error: Failed to read record size.\n");
return;
}

fread(record->id, MAX_ID_LENGTH, 1, file);

// Allocate buffer to read remaining data


char *buffer = malloc(record_size - MAX_ID_LENGTH);
if (!buffer) {
fprintf(stderr, "Error: Memory allocation failed.\n");
exit(EXIT_FAILURE);
}
fread(buffer, record_size - MAX_ID_LENGTH, 1, file);

record->name = strdup(buffer);
record->score = strdup(buffer + strlen(buffer) + 1);
record->dob = strdup(buffer + strlen(buffer) + 1 + strlen(record->score) + 1);

free(buffer);

printf("Read Record: ID=%s, Name=%s, Score=%s, DOB=%s\n", record->id, record-


>name, record->score, record->dob);
}
void add_record(const char *id, const char *name, const char *score, const char
*dob) {
FILE *file = fopen(FILE_NAME, "ab+");
if (!file) {
perror("Error opening file");
return;
}

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);

write_record(file, offset, &record);

fclose(file);

// Free dynamically allocated memory


free(record.name);
free(record.score);
free(record.dob);
}

long find_record(const char *id) {


FILE *file = fopen(FILE_NAME, "rb");
if (!file) {
perror("Error opening file");
return -1;
}

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);

// Free dynamically allocated memory


free(record.name);
free(record.score);
free(record.dob);

fclose(file);
return offset;
}

// Move to the next record


offset += sizeof(long) + MAX_ID_LENGTH + strlen(record.name) + 1 +
strlen(record.score) + 1 + strlen(record.dob) + 1;

// Free allocated memory for the previous record


free(record.name);
free(record.score);
free(record.dob);
}

fclose(file);
printf("Record not found.\n");
return -1;
}

void measure_search_time(const char *id) {


clock_t start, end;
double time_taken;

start = clock();
long result = find_record(id);
end = clock();

time_taken = ((double)(end - start)) / CLOCKS_PER_SEC;

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);
}

printf("Search completed in %f seconds.\n", time_taken);


}

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];

snprintf(id, MAX_ID_LENGTH, "%03d", i);


snprintf(name, sizeof(name), "Name%d", i);
snprintf(score, sizeof(score), "%d", 50 + (i % 10));
snprintf(dob, sizeof(dob), "1990-01-%02d", (i % 30) + 1);

add_record(id, name, score, dob);


}

// Measure time to search for a specific record


measure_search_time("001");

return 0;
}

TASK TWO:
part 2.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define FILENAME "student_records.dat"


#define RECORD_SIZE 60
#define TOTAL_RECORDS 60

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;

// Function to write a record to the file


void write_record(FILE *file, long index, StudentRecord *record) {
fseek(file, index * sizeof(StudentRecord), SEEK_SET);
fwrite(record, sizeof(StudentRecord), 1, file);
}

// Function to add records to the file


void add_records() {
FILE *file = fopen(FILENAME, "wb");
if (file == NULL) {
perror("Error opening file");
return;
}

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"}
};

for (int i = 0; i < TOTAL_RECORDS; i++) {


write_record(file, i, &records[i]);
}

fclose(file);
}

// Function to compare student records by ID


int compare_records(const void *a, const void *b) {
return strcmp(((StudentRecord *)a)->id, ((StudentRecord *)b)->id);
}

// Function to sort the student records


void sort_records() {
FILE *file = fopen(FILENAME, "rb+");
if (file == NULL) {
perror("Error opening file");
return;
}

StudentRecord records[TOTAL_RECORDS];
fread(records, sizeof(StudentRecord), TOTAL_RECORDS, file);

qsort(records, TOTAL_RECORDS, sizeof(StudentRecord), compare_records);

rewind(file);
fwrite(records, sizeof(StudentRecord), TOTAL_RECORDS, file);

fclose(file);
}

// Function to perform binary search on records by ID


int binary_search(FILE *file, const char *id, StudentRecord *result) {
int left = 0;
int right = TOTAL_RECORDS - 1;

while (left <= right) {


int mid = (left + right) / 2;
StudentRecord record;

fseek(file, mid * sizeof(StudentRecord), SEEK_SET);


fread(&record, sizeof(StudentRecord), 1, file);

int cmp = strcmp(record.id, id);


if (cmp == 0) {
*result = record;
return mid;
} else if (cmp < 0) {
left = mid + 1;
} else {
right = mid - 1;
}
}

return -1; // Record not found


}

int main() {
add_records(); // Step 1: Add records to the file
sort_records(); // Step 2: Sort the records by ID

FILE *file = fopen(FILENAME, "rb");


if (file == NULL) {
perror("Error opening file");
return EXIT_FAILURE;
}

// Step 3: Perform binary search for a specific ID


char search_id[10];
printf("Enter ID to search: ");
scanf("%9s", search_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>

#define FILENAME "fixed_records.dat"


#define INDEX_FILENAME "index.dat"
#define RECORD_SIZE 60
#define TOTAL_RECORDS 30 // Adjusted to the new data set size

typedef struct {
char name[30];
int score;
char date[15];
char id[10];
} Record;

typedef struct {
char id[10];
long offset;
} IndexRecord;

// Function to write a record to the file


void write_record(FILE *file, long index, Record *record) {
fseek(file, index * RECORD_SIZE, SEEK_SET);
fwrite(record, sizeof(Record), 1, file);
}

// Function to add records to the file


void add_records() {
FILE *file = fopen(FILENAME, "wb");
if (file == NULL) {
perror("Error opening file");
return;
}

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"}
};

for (int i = 0; i < TOTAL_RECORDS; i++) {


write_record(file, i, &records[i]);
}

fclose(file);
}

// Function to create an index


void create_index() {
FILE *file = fopen(FILENAME, "rb");
FILE *index_file = fopen(INDEX_FILENAME, "wb");
if (file == NULL || index_file == NULL) {
perror("Error opening file");
return;
}

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;
}

fwrite(index_records, sizeof(IndexRecord), TOTAL_RECORDS, index_file);


fclose(file);
fclose(index_file);
}

// Function to perform binary search on the index


int binary_search_index(const char *id, long *offset) {
FILE *index_file = fopen(INDEX_FILENAME, "rb");
if (index_file == NULL) {
perror("Error opening index file");
return -1;
}

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;

while (left <= right) {


int mid = (left + right) / 2;
int cmp = strcmp(index_records[mid].id, id);
if (cmp == 0) {
*offset = index_records[mid].offset;
return mid;
} else if (cmp < 0) {
left = mid + 1;
} else {
right = mid - 1;
}
}

return -1; // ID not found


}

// Function to search for a record using the index


void search_record_by_id(const char *id) {
long offset;
int index = binary_search_index(id, &offset);
if (index != -1) {
FILE *file = fopen(FILENAME, "rb");
if (file == NULL) {
perror("Error opening file");
return;
}

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

// Example: search for a record using the index


char search_id[10];
printf("Enter ID to search: ");
scanf("%9s", search_id);

search_record_by_id(search_id); // Step 3: Search using the index

return 0;
}

TASK THREE:
part 2.

fseek(index_file, 0, SEEK_END);
long size = ftell(index_file);
int count = size / sizeof(IndexEntry);

IndexEntry *entries = malloc(size);


rewind(index_file);
fread(entries, sizeof(IndexEntry), count, index_file);
// Perform binary search
int left = 0, right = count - 1;
while (left <= right) {
int mid = (left + right) / 2;
int cmp = strcmp(entries[mid].name, name);
if (cmp == 0) {
long offset = entries[mid].offset;
free(entries);
fclose(index_file);
return offset; // Found
} else if (cmp < 0) {
left = mid + 1;
} else {
right = mid - 1;
}
}

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"}
};

for (int i = 0; i < 51; i++) {


write_record(file, &records[i]);
}

fclos e(file);

// Build and sort the index


build_index();
sort_index();

// Search for a record by name


char name_to_search[50];
printf("Enter name to search: ");
scanf("%49s", name_to_search);

long offset = search_index(name_to_search);


if (offset != -1) {
Record result;
file = fopen(FILENAME, "rb");
read_record(file, offset, &result);
fclose(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;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy