refactor the code for arbitrary file access

I need to help for rebuild my cod for arbitraty file access. Cause i dint know what to do, and didnt understand what difference between them. And need to rebuild it for my study. My cod below:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Person {
int id;
char name[50];
char surname[50];
char fatherName[50];
int birthYear;
};
void createFile() {
struct Person person;
FILE *file = fopen("data.txt", "r");
if (file != NULL) {
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
fclose(file);
if (file_size > 0) {
printf("File is not empty. Cannot create file.n");
return;
}
}
file = fopen("data.txt", "w");
if (file == NULL) {
printf("Error creating filen");
return;
}
fclose(file);
printf("File created successfullyn");
printf("Enter the identification number: ");
scanf("%d", &person.id);
printf("Enter the surname: ");
scanf("%s", person.surname);
printf("Enter the name: ");
scanf("%s", person.name);
printf("Enter the father's name: ");
scanf("%s", person.fatherName);
printf("Enter the birth year: ");
scanf("%d", &person.birthYear);
file = fopen("data.txt", "a");
fprintf(file, "%d %s %s %s %dn", person.id, person.surname, person.name, person.fatherName, person.birthYear);
fclose(file);
printf("Information added to the filen");
}
void addPerson() {
FILE *file = fopen("data.txt", "a");
if (file == NULL) {
printf("Error opening filen");
return;
}
struct Person person;
printf("Enter the ID: ");
scanf("%d", &person.id);
printf("Enter the surname: ");
scanf("%s", person.surname);
printf("Enter the name: ");
scanf("%s", person.name);
printf("Enter the father's name: ");
scanf("%s", person.fatherName);
printf("Enter the birth year: ");
scanf("%d", &person.birthYear);
fprintf(file, "%d %s %s %s %dn", person.id, person.surname, person.name, person.fatherName, person.birthYear);
fclose(file);
printf("Information added to the filen");
}
void displayAll() {
FILE *file = fopen("data.txt", "r");
if (file == NULL) {
printf("Error opening filen");
return;
}
struct Person person;
while (fscanf(file, "%d %s %s %s %d", &person.id, person.surname, person.name, person.fatherName, &person.birthYear) != EOF) {
printf("ID: %dn", person.id);
printf("Surname: %sn", person.surname);
printf("Name: %sn", person.name);
printf("Father's Name: %sn", person.fatherName);
printf("Birth Year: %dn", person.birthYear);
printf("------------------------n");
}
fclose(file);
}
void searchPerson() {
FILE *file = fopen("data.txt", "r");
if (file == NULL) {
printf("Error opening filen");
return;
}
int searchId;
printf("Enter the ID to search: ");
scanf("%d", &searchId);
struct Person person;
int found = 0;
while (fscanf(file, "%d %s %s %s %d", &person.id, person.surname, person.name, person.fatherName, &person.birthYear) != EOF) {
if (person.id == searchId) {
printf("ID: %dn", person.id);
printf("Surname: %sn", person.surname);
printf("Name: %sn", person.name);
printf("Father's Name: %sn", person.fatherName);
printf("Birth Year: %dn", person.birthYear);
printf("------------------------n");
found = 1;
break;
}
}
if (!found) {
printf("Information not foundn");
}
fclose(file);
}
void deletePerson() {
FILE *file = fopen("data.txt", "r");
if (file == NULL) {
printf("Error opening filen");
return;
}
int deleteId;
printf("Enter the ID to delete: ");
scanf("%d", &deleteId);
FILE *tempFile = fopen("temp.txt", "w");
if (tempFile == NULL) {
printf("Error creating temporary filen");
fclose(file);
return;
}
struct Person person;
int deleted = 0;
while (fscanf(file, "%d %s %s %s %d", &person.id, person.surname, person.name, person.fatherName, &person.birthYear) != EOF) {
if (person.id != deleteId) {
fprintf(tempFile, "%d %s %s %s %dn", person.id, person.surname, person.name, person.fatherName, person.birthYear);
} else {
deleted = 1;
}
}
fclose(file);
fclose(tempFile);
if (deleted) {
remove("data.txt");
rename("temp.txt", "data.txt");
printf("Information deleted from the filen");
} else {
remove("temp.txt");
printf("Information not foundn");
}
}
int main() {
int choice;
do {
printf("Menu:n");
printf("1) Create file and enter informationn");
printf("2) Display all informationn");
printf("3) Search information by key fieldn");
printf("4) Add information to the filen");
printf("5) Delete information from the filen");
printf("6) Exit the programn");
printf("Choose an option: ");
scanf("%d", &choice);
switch (choice) {
case 1:
createFile();
break;
case 2:
displayAll();
break;
case 3:
searchPerson();
break;
case 4:
addPerson();
break;
case 5:
deletePerson();
break;
case 6:
printf("Goodbye!n");
break;
default:
printf("Invalid choice. Please try again.n");
break;
}
} while (choice != 6);
return 0;
}
</code>
<code>#include <stdio.h> #include <stdlib.h> #include <string.h> struct Person { int id; char name[50]; char surname[50]; char fatherName[50]; int birthYear; }; void createFile() { struct Person person; FILE *file = fopen("data.txt", "r"); if (file != NULL) { fseek(file, 0, SEEK_END); long file_size = ftell(file); fclose(file); if (file_size > 0) { printf("File is not empty. Cannot create file.n"); return; } } file = fopen("data.txt", "w"); if (file == NULL) { printf("Error creating filen"); return; } fclose(file); printf("File created successfullyn"); printf("Enter the identification number: "); scanf("%d", &person.id); printf("Enter the surname: "); scanf("%s", person.surname); printf("Enter the name: "); scanf("%s", person.name); printf("Enter the father's name: "); scanf("%s", person.fatherName); printf("Enter the birth year: "); scanf("%d", &person.birthYear); file = fopen("data.txt", "a"); fprintf(file, "%d %s %s %s %dn", person.id, person.surname, person.name, person.fatherName, person.birthYear); fclose(file); printf("Information added to the filen"); } void addPerson() { FILE *file = fopen("data.txt", "a"); if (file == NULL) { printf("Error opening filen"); return; } struct Person person; printf("Enter the ID: "); scanf("%d", &person.id); printf("Enter the surname: "); scanf("%s", person.surname); printf("Enter the name: "); scanf("%s", person.name); printf("Enter the father's name: "); scanf("%s", person.fatherName); printf("Enter the birth year: "); scanf("%d", &person.birthYear); fprintf(file, "%d %s %s %s %dn", person.id, person.surname, person.name, person.fatherName, person.birthYear); fclose(file); printf("Information added to the filen"); } void displayAll() { FILE *file = fopen("data.txt", "r"); if (file == NULL) { printf("Error opening filen"); return; } struct Person person; while (fscanf(file, "%d %s %s %s %d", &person.id, person.surname, person.name, person.fatherName, &person.birthYear) != EOF) { printf("ID: %dn", person.id); printf("Surname: %sn", person.surname); printf("Name: %sn", person.name); printf("Father's Name: %sn", person.fatherName); printf("Birth Year: %dn", person.birthYear); printf("------------------------n"); } fclose(file); } void searchPerson() { FILE *file = fopen("data.txt", "r"); if (file == NULL) { printf("Error opening filen"); return; } int searchId; printf("Enter the ID to search: "); scanf("%d", &searchId); struct Person person; int found = 0; while (fscanf(file, "%d %s %s %s %d", &person.id, person.surname, person.name, person.fatherName, &person.birthYear) != EOF) { if (person.id == searchId) { printf("ID: %dn", person.id); printf("Surname: %sn", person.surname); printf("Name: %sn", person.name); printf("Father's Name: %sn", person.fatherName); printf("Birth Year: %dn", person.birthYear); printf("------------------------n"); found = 1; break; } } if (!found) { printf("Information not foundn"); } fclose(file); } void deletePerson() { FILE *file = fopen("data.txt", "r"); if (file == NULL) { printf("Error opening filen"); return; } int deleteId; printf("Enter the ID to delete: "); scanf("%d", &deleteId); FILE *tempFile = fopen("temp.txt", "w"); if (tempFile == NULL) { printf("Error creating temporary filen"); fclose(file); return; } struct Person person; int deleted = 0; while (fscanf(file, "%d %s %s %s %d", &person.id, person.surname, person.name, person.fatherName, &person.birthYear) != EOF) { if (person.id != deleteId) { fprintf(tempFile, "%d %s %s %s %dn", person.id, person.surname, person.name, person.fatherName, person.birthYear); } else { deleted = 1; } } fclose(file); fclose(tempFile); if (deleted) { remove("data.txt"); rename("temp.txt", "data.txt"); printf("Information deleted from the filen"); } else { remove("temp.txt"); printf("Information not foundn"); } } int main() { int choice; do { printf("Menu:n"); printf("1) Create file and enter informationn"); printf("2) Display all informationn"); printf("3) Search information by key fieldn"); printf("4) Add information to the filen"); printf("5) Delete information from the filen"); printf("6) Exit the programn"); printf("Choose an option: "); scanf("%d", &choice); switch (choice) { case 1: createFile(); break; case 2: displayAll(); break; case 3: searchPerson(); break; case 4: addPerson(); break; case 5: deletePerson(); break; case 6: printf("Goodbye!n"); break; default: printf("Invalid choice. Please try again.n"); break; } } while (choice != 6); return 0; } </code>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Person {
    int id;
    char name[50];
    char surname[50];
    char fatherName[50];
    int birthYear;
};

void createFile() {
    struct Person person;
    FILE *file = fopen("data.txt", "r");
    if (file != NULL) {
        fseek(file, 0, SEEK_END);
        long file_size = ftell(file);
        fclose(file);
        if (file_size > 0) {
            printf("File is not empty. Cannot create file.n");
            return;
        }
    }

    file = fopen("data.txt", "w");
    if (file == NULL) {
        printf("Error creating filen");
        return;
    }
    fclose(file);

    printf("File created successfullyn");
    printf("Enter the identification number: ");
    scanf("%d", &person.id);
    printf("Enter the surname: ");
    scanf("%s", person.surname);
    printf("Enter the name: ");
    scanf("%s", person.name);
    printf("Enter the father's name: ");
    scanf("%s", person.fatherName);
    printf("Enter the birth year: ");
    scanf("%d", &person.birthYear);

    file = fopen("data.txt", "a");
    fprintf(file, "%d %s %s %s %dn", person.id, person.surname, person.name, person.fatherName, person.birthYear);
    fclose(file);

    printf("Information added to the filen");
}

void addPerson() {
    FILE *file = fopen("data.txt", "a");
    if (file == NULL) {
        printf("Error opening filen");
        return;
    }

    struct Person person;
    printf("Enter the ID: ");
    scanf("%d", &person.id);
    printf("Enter the surname: ");
    scanf("%s", person.surname);
    printf("Enter the name: ");
    scanf("%s", person.name);
    printf("Enter the father's name: ");
    scanf("%s", person.fatherName);
    printf("Enter the birth year: ");
    scanf("%d", &person.birthYear);

    fprintf(file, "%d %s %s %s %dn", person.id, person.surname, person.name, person.fatherName, person.birthYear);
    fclose(file);
    printf("Information added to the filen");
}

void displayAll() {
    FILE *file = fopen("data.txt", "r");
    if (file == NULL) {
        printf("Error opening filen");
        return;
    }

    struct Person person;
    while (fscanf(file, "%d %s %s %s %d", &person.id, person.surname, person.name, person.fatherName, &person.birthYear) != EOF) {
        printf("ID: %dn", person.id);
        printf("Surname: %sn", person.surname);
        printf("Name: %sn", person.name);
        printf("Father's Name: %sn", person.fatherName);
        printf("Birth Year: %dn", person.birthYear);
        printf("------------------------n");
    }

    fclose(file);
}

void searchPerson() {
    FILE *file = fopen("data.txt", "r");
    if (file == NULL) {
        printf("Error opening filen");
        return;
    }

    int searchId;
    printf("Enter the ID to search: ");
    scanf("%d", &searchId);

    struct Person person;
    int found = 0;
    while (fscanf(file, "%d %s %s %s %d", &person.id, person.surname, person.name, person.fatherName, &person.birthYear) != EOF) {
        if (person.id == searchId) {
            printf("ID: %dn", person.id);
            printf("Surname: %sn", person.surname);
            printf("Name: %sn", person.name);
            printf("Father's Name: %sn", person.fatherName);
            printf("Birth Year: %dn", person.birthYear);
            printf("------------------------n");
            found = 1;
            break;
        }
    }

    if (!found) {
        printf("Information not foundn");
    }

    fclose(file);
}

void deletePerson() {
    FILE *file = fopen("data.txt", "r");
    if (file == NULL) {
        printf("Error opening filen");
        return;
    }

    int deleteId;
    printf("Enter the ID to delete: ");
    scanf("%d", &deleteId);

    FILE *tempFile = fopen("temp.txt", "w");
    if (tempFile == NULL) {
        printf("Error creating temporary filen");
        fclose(file);
        return;
    }

    struct Person person;
    int deleted = 0;
    while (fscanf(file, "%d %s %s %s %d", &person.id, person.surname, person.name, person.fatherName, &person.birthYear) != EOF) {
        if (person.id != deleteId) {
            fprintf(tempFile, "%d %s %s %s %dn", person.id, person.surname, person.name, person.fatherName, person.birthYear);
        } else {
            deleted = 1;
        }
    }

    fclose(file);
    fclose(tempFile);

    if (deleted) {
        remove("data.txt");
        rename("temp.txt", "data.txt");
        printf("Information deleted from the filen");
    } else {
        remove("temp.txt");
        printf("Information not foundn");
    }
}

int main() {
    int choice;

    do {
        printf("Menu:n");
        printf("1) Create file and enter informationn");
        printf("2) Display all informationn");
        printf("3) Search information by key fieldn");
        printf("4) Add information to the filen");
        printf("5) Delete information from the filen");
        printf("6) Exit the programn");
        printf("Choose an option: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                createFile();
                break;
            case 2:
                displayAll();
                break;
            case 3:
                searchPerson();
                break;
            case 4:
                addPerson();
                break;
            case 5:
                deletePerson();
                break;
            case 6:
                printf("Goodbye!n");
                break;
            default:
                printf("Invalid choice. Please try again.n");
                break;
        }
    } while (choice != 6);

    return 0;
}

I tried ask ChatGPT for explain me that, but i and still didn’t understand after that. I tried some changes in search function, but they broke all programm.

New contributor

4EL1 12 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật