我正在学习IT,编程对我来说是新的。上周我有一个任务要做。我必须编写一个程序,可以将结构(公司工人的个人数据)保存到file.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的全部内容。我缺少的是:如何找到一个特定的人以及如何删除这些人(我正在考虑打开第二个临时文件并复制原始文件的内容,除了这些包含给定名称/姓氏的行)(所有这些都在单独的函数中) 。你有什么想法我能做到这一点吗?我在一些书中寻找它,但我找不到任何帮助。
答案 0 :(得分:0)
我想将代码(下面)分开到另一个函数但我没有 知道如何将结构传递给函数。
在函数之外和之上定义glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_WRAP_T, GL_REPEAT);
GL_CHECK_ERROR("after glTexImage2DMultisample 2");
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
GL_CHECK_ERROR("after glTexImage2DMultisample 3");
,因此可以在所有函数中使用它。然后,您可以将保存人员的代码放入函数
struct Person
并使用
调用此方法void save(int ile, struct Person *osoba[ile])
{
…
}
如何找到特定的人
您可以使用以下功能,这比查找任务所需的功能稍微复杂一些,但可以重复用于其他目的:
save(ile, osoba);
用
调用它char prformat[] =
"Name: %s, Surname: %s, Age: %d years, Height: %d cm, Weight: %d kg.\n";
char scformat[] =
"Name: %[^,], Surname: %[^,], Age:%d years, Height:%d cm, Weight:%d kg.\n";
// Read the data file "file.txt", look for 'name' or 'surname' (unless NULL),
// and output line(s) to 'out' if or unless they match (depending on 'match')
scan(char *name, char *surname, _Bool match, FILE *out)
{
FILE *fp = fopen("file.txt", "r");
if (!fp) perror("file.txt"), exit(1);
struct Person osoba;
while (fscanf(fp, scformat, osoba.name, osoba.surname,
&osoba.age, &osoba.height, &osoba.weight) == 5
)
if ( name && strcmp(osoba. name, name) == 0 == match
|| surname && strcmp(osoba.surname, surname) == 0 == match
)
if (fprintf(out, prformat, osoba.name, osoba.surname,
osoba.age, osoba.height, osoba.weight)
< 0) perror(""), exit(1);
fclose(fp);
}
删除所有拥有用户姓氏的人... (我正在考虑打开第二个临时文件并复制原始文件的内容,除了这些行......
你正在思考:
char name[30];
printf("Give a name or surname to find: ");
if (scanf("%29s", name) == 1)
scan(name, name, 1, stdout); // print matching lines to stdout