構造体例1

構造体を利用した社内スタッフの整理プログラム例です。

構造体例1(list_15.c)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define F_NAME "992766A3.txt"
#define TEMP_F ".tmp"
//-----------------------------------------------------------------
typedef struct tabPersonal {  //staff detail
    char name[20]; // name
    char pos[20];  // position
    char dep[15];  // department
    int no;        //staff No
    long int sal;  // salary
} personal;
//-----------------------------------------------------------------
int read(personal []);
void list(personal [],int);
int newstaff(personal [],int);
void search(personal [],int);
void sort(personal [],int);
void swap_record(personal* a,personal* b);
//-----------------------------------------------------------------

int main()
{
    personal record[50];
    int i,s;
    read(record);
    while (1){
    printf("\n\t\tM E N U\n");
    printf("\t1.\tSearching by department\n");
    printf("\t2.\tSort by staff numbert\n");
    printf("\t3.\tExit program\n");
    printf("\n\tPlease enter your choice : "); scanf("%d",&i);
    switch(i){
    case 1: s=read(record); search(record,s); break;
    case 2: s=read(record); sort(record,s); break;
    case 3: exit(0);
    default: puts("\nPlease enter only 1 to 4"); }
    }
    return 0;
}

int read(personal record[])
{
    FILE *fp; int n=0;
    if((fp=fopen(F_NAME,"r"))==NULL)
    printf("\nCan not open staff records file.\n");
    else {
    while((fscanf(fp,"%d%s%s%s%d",
              &record[n].no,record[n].name,
              record[n].pos,record[n].dep,
              &record[n].sal))!=EOF) n++;
    fclose(fp);
    }
    return n;
}

void search(personal record[],int s)
{
    int i,x=0,total=0,count=0;
    float avg;
    char dep[25];
    printf("Enter the department name : "); scanf("%s",dep);
    for(i=0;i<s;i++){
    if((strcmp(record[i].dep,dep))==0){
        printf("\n\%s \t\t%s",record[i].name,record[i].pos);
        count++;
        total+=record[i].sal;
        avg=total/count;
        x=1;
    }
    }
    if(x==1)
    printf("\n\nAverage salary : %.0f",avg);
    else printf("\nThe department does not exist.");
    getchar();
}

void sort(personal record[],int s)
{
    FILE *ofp;
    int inner,outer,j;
    for(outer=0;outer<s-1;outer++){
    for(inner=outer+1;inner<s;inner++){
        if(record[inner].no<record[outer].no){
        swap_record(&record[inner],&record[outer]);
        }
    }
    }
    /* ファイルの書き出し */
    ofp=fopen(TEMP_F,"w");
    for(j=0;j<s;j++){
    fprintf(ofp,"%d %s %s %s %d\n",
        record[j].no,record[j].name,
        record[j].pos,record[j].dep,
        record[j].sal);
    }
    fclose(ofp);
    /* tmp作業ファイルとの置き換え */
    unlink(F_NAME);
    rename(TEMP_F,F_NAME);
}

void swap_record(personal* a,personal* b)
{
    personal temp;
    memcpy(&temp,a,sizeof(personal));
    
    strncpy(a->name,b->name,20);
    strncpy(a->pos,b->pos,20);
    strncpy(a->dep,b->dep,15);
    a->no = b->no;
    a->sal = b->sal;

    memcpy(b,&temp,sizeof(personal));
}

準備ファイル例(992766A3.txt)
9 murakami shachou room1 10000
5 akitsugu buka room2 2000
6 morimoto hisho room1 3000

           ↓

ID 氏名 身分 部門 給料

result
Gami[895]% list_15.exe

                M E N U
        1.      Searching by department
        2.      Sort by staff numbert
        3.      Exit program

        Please enter your choice : 1
Enter the department name : room1

murakami                shachou
morimoto                hisho

Average salary : 6500
                M E N U
        1.      Searching by department
        2.      Sort by staff numbert
        3.      Exit program

        Please enter your choice : 2

                M E N U
        1.      Searching by department
        2.      Sort by staff numbert
        3.      Exit program

        Please enter your choice : 3
Gami[896]%

2. の命令によりファイルの中身は以下のように変更されます。

Gami[896]% cat 992766A3.txt
5 akitsugu buka room2 2000
6 morimoto hisho room1 3000
9 murakami shachou room1 10000
Gami[897]%
inserted by FC2 system