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:
<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.