0

テキストファイルにデータを書き込んだ後、グローバル変数(#define totalstaff 100)の値に応じて'0'が表示されます。5 に設定すると、5 つのゼロが表示されます。これらのゼロを削除するにはどうすればよいですか? スタッフIDを整数ではなく文字で宣言することを考えていますが、プログラム全体を実行するにはIDを整数として宣言する必要があるため、それは不可能です。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define totalstaff 5

void new_staff();
void delete_staff();
void export_profile();
void refresh();

struct profile
{
   int ID;
   char Name[30];
   char Gender[10];
   char Phone[15];
   char Email[30];
};

int x;
int y;

int main(){

struct profile Info[totalstaff];
int n;

refresh(Info);

do
{
    printf("**********************************************");
    printf("\nMAIN MENU-STAFF INFORMATION SYSTEM");
    printf("\n**********************************************");
    printf("\n1. Add New Staff Profile");
    printf("\n2. Delete Staff Profile");
    printf("\n3. Export All Profiles to 'output.txt'");
    printf("\n4. Exit");
    printf("\n**********************************************");
    printf("\nPlease enter your option< 1 / 2 / 3 / 4 >: ");
    scanf("%d", &n);

    switch(n)
    {
    case 1:
        new_staff(Info);
        break;
    case 2:
        delete_staff(Info);
        break;
    case 3:
        export_profile(Info);
        break;
    case 4:
        return 0;
    default:
        printf("Please enter the correct option");
        break;
    }
}
while(1);

return 0;
}

void new_staff(struct profile *Info)
{
struct profile staff;

    printf("\n\n");
    printf("== ADD NEW STAFF PROFILE ==\n\n");
    printf("Please enter the following staff information:\n\n");
    printf("Staff ID: ");
    scanf("%d", &staff.ID);
    fflush(stdin);

    // detect whether the current staff ID was entered

    for (x=0;x<totalstaff;x++)
    {
    if(Info[x].ID == staff.ID)
        {
        printf("\nSYSTEM: Staff ID %d exists. Please try with other ID.\n\n",x+1);
        return;
        }
    }

    printf("Name\t: ");
    gets(staff.Name);

    printf("Gender\t: ");
    gets(staff.Gender);

    printf("Phone\t: ");
    gets(staff.Phone);

    printf("E-mail\t: ");
    gets(staff.Email);

    Info[staff.ID-1] = staff;

    printf("\nSYSTEM: New staff profile is added successfully.\n\n");
}

void delete_staff(struct profile *Info)
{
 struct profile staff;
     int ID_Number;

     printf("\nenter ID:");
     scanf("%d", &ID_Number);
     fflush(stdin);
     for(x=0;x<totalstaff;x++)
     {
                 if(Info[x].ID == ID_Number)
                 {
                        Info[x].ID=0;
                        strcpy(Info[x].Name,"");
                        strcpy(Info[x].Gender,"");  
                        strcpy(Info[x].Phone,"");           
                        strcpy(Info[x].Email,"");           

                        printf("\nSYSTEM: Staff with ID %d is 
                                  deleted successfully.\n\n", ID_Number);
                        return;
                 }
                 else if(x==totalstaff-1)
                 {
                     printf("\nSYSTEM: No staff found.\n\n");
                     return;
                 }
     }
}

void export_profile(struct profile *Info)
{
    FILE *fExport, *fStore;

    fExport=fopen("output.txt", "w");
    if (fExport == NULL)        //Catch error in fopen      function
    printf("\nError in opening file.\n\n");
fprintf(fExport, "ID\tName\t\t\tGender\t\tPhone\t\tEmail");
    for(y=0;y<totalstaff;y++)
    fprintf(fExport, "\n%d\t%s\t\t%s\t\t%s\t%s", Info[y].ID, 
            Info[y].Name, Info[y].Gender, Info[y].Phone, Info[y].Email);

fStore=fopen("database.dat", "w");
if (fStore == NULL)             //Catch error in fopen function
    printf("\nError in opening file.\n\n");
    fprintf(fStore, "ID\tName\t\t\tGender\t\tPhone\t\tEmail");
    for(y=0;y<totalstaff;y++)
    fprintf(fStore, "\n%d\t%s\t\t%s\t\t%s\t%s", Info[y].ID, 
             Info[y].Name, Info[y].Gender, Info[y].Phone, Info[y].Email);

    printf("\nSYSTEM: All staff profile have been exported to 'output.txt' file.\n\n");

    fclose(fExport);
    fclose(fStore);
}

void refresh(struct profile *Info)
{   
struct profile staff;

for (x=0;x<totalstaff;x++)
    {
        Info[x].ID=0;
        strcpy(Info[x].Name,"");
        strcpy(Info[x].Gender,"");
        strcpy(Info[x].Phone,"");
        strcpy(Info[x].Email,"");
    }
}

すべての情報を 1 で入力すると、テキスト ファイルの出力は次のようになります。

ID  Name            Gender      Phone       Email
1   1               1       1           1
0                       
0                       
0                       
0

では、どうすればこれらのゼロを削除できますか?

4

2 に答える 2

0

これは、書き込み時に常にループするためです。totalstaff最大数だけでなく、現在のスタッフ数を追跡する必要があります。


ところで、関数プロトタイプは本当に修正する必要があります。適切な関数プロトタイプを使用すると、関数が特定のパラメーターを受け取るとコンパイラが認識しているが、実際には別のものを使用して未定義の動作につながる可能性がある、他の奇妙なエラーを見つけるのに役立つ場合があります。

于 2013-08-27T12:51:46.263 に答える