C 编程 - 如何删除文本文件中的尾随零

标签 c file-io text-files

将数据写入文本文件后,根据全局变量的值(#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

那么,我怎样才能删除这些零呢?

最佳答案

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++)
        if (Info[y].ID != 0)
            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);
}

关于C 编程 - 如何删除文本文件中的尾随零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18465832/

相关文章:

ChangeDisplaySettings 成功但不执行任何操作。我如何让它工作?

c - 在c中的数组中查找 'X'数字的部分和

c - 每第 N 个单词插入字符串

c - 代码的输出是什么?

c# - (C# WinFoms) 写入文件和换行

java文本区域和文件输入

python - 文件不会用python写

c++ - 是否可以将文本文件设置为UTF-16?

java - 从文本文件中提取匹配行

php - 我需要一种人类可读但可解析的文档格式