我学习IT和编程删除结构对我来说是新的东西。上周我有一个任务要做。我必须编写一个可以将结构(公司工作人员的个人数据)保存到file.txt的程序。程序应该能够找到一个特定的人(把他的姓名),并删除所有用户给出的姓氏。这些所有任务应该在不同的功能。直到现在我写了这个功能:保存,查找和txt文件
void struktura()
{
struct Person
{
char name[30];
char surname[30];
int age;
int height;
int weight;
};
int ile;
int i = 0;
printf("\n");
printf("How many persons would you like add to database (max 10 at once): ");
scanf("%d", &ile);
printf("\n");
if (ile >= 0)
{
printf("You added no one.\n");
}
else
{
struct Person *osoba[10]; //
while (i < ile)
{
osoba[i] = (struct Person*) malloc(sizeof (struct Person));
printf("Give a name: ");
scanf("%s", osoba[i]->name);
printf("Give a surname: ");
scanf("%s", osoba[i]->surname);
printf("Age: ");
scanf("%d", &osoba[i]->age);
printf("Height: ");
scanf("%d", &osoba[i]->height);
printf("Weight: ");
scanf("%d", &osoba[i]->weight);
printf("\n");
i = i + 1;
}
printf("You have added: \n\n");
i = 0;
while (i < ile)
{
printf("%d) Name: %s, Surname: %s, Age: %d years, Height: %d cm, Weight: %d kg.\n", i + 1, osoba[i]->name, osoba[i]->surname, osoba[i]->age, osoba[i]->height, osoba[i]->weight);
i = i + 1;
}
for (i = 0; i < ile; i++)
{
free(osoba[i]);
}
}
}
另外,我有一个代码,这些人保存到file.txt。我想分开代码(下面)到另一个函数,但我不知道如何传递一个结构来运行。
FILE *save;
char name[30];
printf("\nWhere to save a database: ");
scanf("%s", name);
save = fopen(name, "a");
if (save == NULL)
{
printf("This file doesn't exist!");
getchar();
exit(1);
}
i = 0;
while (i < ile)
{
fprintf(save, "Name: %s, Surname: %s, Age: %d years, Height: %d cm, Weight: %d kg.\n",osoba[i]->name, osoba[i]->surname, osoba[i]->age, osoba[i]->height, osoba[i]->weight);
i = i + 1;
}
fclose(save);
我写了一个函数,它也打开了file.txt的全部内容。我所缺的是:如何找到一个特定的人,以及如何删除这些人(我正在考虑打开第二个临时文件并复制原始文件的内容,除了包含给定名称/姓氏的这些行)(全部在单独的函数中) 。你有什么想法,我怎么能做到这一点?我在一些书中寻找它,但我找不到任何帮助。