Программа, которая реализует с файлом последовательного доступа некоторые операции: создание, ввод, вывод и поиск по значению ключевого поля
Проблема возникает, когда я:
1)пытаюсь сделать удаление информации по ключевому полю(данные из файла просто удаляются);
2)не получается реализовать поиск информации по ключевому полю.
Ключевое поле - значение code в структуре Research_station.
Информация - все объекты в структуре Research_station (code, name, number_of_cultures).
Вот весь код(извините, что такой огромной, но пришлось вывести весь для общей картины):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRING 255
typedef struct {
int code;
char name[STRING];
int number_of_cultures;
} Research_station;
void Creating(Research_station *H, int n) {
FILE *fp;
fp=fopen("test.txt", "w");
if(fp==NULL)
{
printf("Can't create file");
getch();
exit(1);
}
printf("\n\tHow many research stations you want to enter: ");
scanf("%d",&n);
for(int i=0; i<n; i++)
{
printf("\n****************************************");
printf("\nEnter research station code: ");
scanf("%d",&(H+i)->code);
printf("\nEnter research station name: ");
scanf("%s",&(H+i)->name);
printf("\nEnter the number of cultures of the research station: ");
scanf("%d",&(H+i)->number_of_cultures);
fprintf(fp,"Research station code: %d \nResearch station name: %s \nThe number of cultures of the research station: %d \n", (H+i)->code, (H+i)->name, (H+i)->number_of_cultures);
}
fclose(fp);
}
void Outputting(Research_station *H, int n) {
FILE *fp;
fp=fopen("test.txt","r");
if(fp==NULL)
{
printf("Can't read file");
getch();
exit(1);
}
char m[STRING];
printf("\n\tHow many research stations you entered: ");
scanf("%d", &n);
for(int i=0; i<n*3; i++){
fgets(m, STRING, fp);
printf("%s", m);
}
fclose(fp);
}
void Searching() {
FILE *fp;
fp=fopen("test.txt", "r+");
if(fp==NULL)
{
printf("Can't create file");
getch();
exit(1);
}
int search_with_code;
printf("\nEnter the research station code: ");
scanf("%d",&search_with_code);
}
void Adding(Research_station *H, int n, int n1) {
FILE *fp;
fp=fopen("test.txt", "a");
if(fp==NULL)
{
printf("Can't create file");
getch();
exit(1);
}
for(int i=0; i<n1; i++)
{
printf("\n****************************************\n");
printf("\nEnter research station code: ");
scanf("%d",&(H+i)->code);
printf("\nEnter research station name: ");
scanf("%s",&(H+i)->name);
printf("\nEnter the number of cultures of the research station: ");
scanf("%d",&(H+i)->number_of_cultures);
fprintf(fp,"Research station code: %d \nResearch station name: %s \nThe number of cultures of the research station: %d \n", (H+i)->code, (H+i)->name, (H+i)->number_of_cultures);
}
fclose(fp);
}
void Deleting(Research_station *H, int n) {
FILE *fp;
fp=fopen("test.txt", "w+");
if(fp==NULL)
{
printf("Can't create file");
getch();
exit(1);
}
int delete_with_code;
printf("\nEnter the research station code: ");
scanf("%d",&delete_with_code);
for(int i = 0; i<n; i++){
if(delete_with_code == (H+i)->code) continue;
fprintf(fp,"Research station code: %d \nResearch station name: %s \nThe number of cultures of the research station: %d \n",(H+i)->code,(H+i)->name,(H+i)->number_of_cultures);
}
fclose(fp);
}
void print_menu() {
system("cls");
printf("What do you want to do?\n");
printf("1. Create a file and enter information\n");
printf("2. Output of all information (from the file)\n");
printf("3. Search for information from a file by key field\n");
printf("4. Adding information to a file\n");
printf("5. Delete information from a file by key field\n");
printf("6. Exit the program\n");
printf("> ");
}
int get_variant(int count) {
int variant;
char s[100];
scanf("%s", s);
while (sscanf(s, "%d", &variant) != 1 || variant < 1 || variant > count)
{
printf("Incorrect input. Try again: ");
scanf("%s", s);
}
return variant;
}
int main(void) {
Research_station *pH;
Research_station H;
pH=&H;
int n, n1, c;
int variant;
do {
print_menu();
variant = get_variant(6);
switch (variant) {
case 1:
Creating(pH, n);
break;
case 2:
Outputting(pH, n);
break;
case 3:
Searching();
break;
case 4:
printf("\n\tHow many research stations you want to add: ");
scanf("%d",&n1);
Adding(pH, n, n1);
n+=n1;
break;
case 5:
Deleting(pH, n);
n-=1;
break;
}
if (variant != 6)
system("pause");
} while (variant != 6);
return 0;
}