2015-05-11 38 views
1

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

回答

0

我想分开代码(下面)到另一个函数,但我没有 知道如何传递一个结构到函数。

定义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 

调用它删除所有的人,他们得到了由用户给出的姓氏...... (我在想打开第二个临时文件,并复制原始文件的内容除了这些线路...

你在想右:

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"); 
    }