c - 从 txt 文件中保存、查找和删除结构

标签 c file save structure

我正在学习 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 的全部内容。我缺少的是:如何找到一个特定的人以及如何删除这些人(我正在考虑打开第二个临时文件并复制原始文件的内容,除了这些包含给定姓名/姓氏的行)(全部在单独的功能中) .你知道我该怎么做吗?我在一些书中寻找它,但找不到任何帮助。

最佳答案

I would like to separate code (below) to another function but I don't know how to pass a structure to function.

在函数之外和之上定义struct Person,这样它就可以在所有函数中使用。然后你可以把将人员保存到文件的代码放在一个函数中

void save(int ile, struct Person *osoba[ile])
{
    …
}

并调用它

        save(ile, osoba);

how to find a specific person

您可以使用以下函数,它比查找任务所需的函数稍微复杂一点,但可以重复用于其他目的:

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

delete all people whose got a surname given by user … (I was thinking about opening a second temporary file and copy an content of original file except these lines …

你想对了:

    printf("Give a surname to delete: ");
    if (scanf("%29s", name) == 1)
    {
        FILE *fp = fopen("file.tmp", "w");
        if (!fp) perror("file.tmp"), exit(1);
        scan(NULL, name, 0, fp);    // print non-matching lines to tempfile
        if (fclose(fp) < 0) perror("file.tmp"), exit(1);
        remove("file.txt");
        if (rename("file.tmp", "file.txt") != 0) perror("file.tmp");
    }

关于c - 从 txt 文件中保存、查找和删除结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30164542/

相关文章:

c - 是否有一种优雅的行业标准方式在 C 中实现 substr ?

c - 如何从字符串中删除重复的元音?

css - 用图像替换输入类型=文件

c# - 加载 BitmapSource 并在 WPF -> IOException 中使用相同的名称保存

wpf - 如果存储库在 View 模型中不可用,如何从绑定(bind)到 View 模型的 DetailView 中保存数据?

c - 制作简单的设置文件时遇到问题

java - 从 native pthread 调用 Java 方法时出现 SIGSEGV

EC 公钥/私钥的文件格式?

c++ - 写入文件时检查错误

java - 如何编写扫描仪代码来加载先前保存在 save.txt 中的内容?