将数据写入文本文件后,根据全局变量(#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
那么,我怎样才能删除这些零呢?