c语言课程设计

源代码在线查看: delete.c

软件大小: 329 K
上传用户: feiguohaihu
关键词: c语言
下载地址: 免注册下载 普通下载 VIP

相关代码

				/* delete.c:删除员工工资信息记录*/
				#include "stdio.h"
				void DelRecord()
				 {
				    int i,j,k;
				    long delnum;      /*存放用户输入的要删除员工序号*/
				    yuangong TmpS;    /*定义进行操作时的临时结构体变量*/
				    yuangong s[SIZE];/*SIZE,在shead.h头文件中定义的常量,值为100  */
				    int recNumber;    /*原文件中的记录数*/
				    char DataFile[40] = "",next;
				    /*DataFile存储员工工资信息的文件名,next为是否进行下一次删除操作的选项*/
				    FILE *fp;/*====fp指针指向存储数据的文件名====*/
				    printf("\nplease input the name of file where data is stored,end with enter key.\n");
				    gets(DataFile);
				   /*提示用户输入要进行删除纪录的文件名*/
				    while(*DataFile == ('\0'))
				    {
				        printf("\nplease input the name of file where data is stored,end with enter key.\n");
				        gets(DataFile);
				    }
				begin:
				    /*以二进制读的方式打开文件*/
				    fp=fopen(DataFile,"rb");
				    if (fp == NULL)
				    {
				        printf("\nOpen file %s fail!End with any key\n",DataFile);
				        perror("Open file fail");
				        getch();
				        exit(1);
				    }
				    /*输入要删除的员工序号*/
				    printf("please input the Employee'seatnum which you will delete:");
				    scanf("%ld",&delnum);
				    printf("the yuangong you will delete is:%ld\n",delnum);
				   /*将文件中信息存入结构体数组*/
				   /*与要删除的员工序号相匹配的项不写入数组,
				   循环后数组中即为去掉了要删除记录后的剩余记录*/
				   recNumber=0;
				    while((fread(&TmpS,sizeof(yuangong),1,fp)) != (int)NULL)
				    {
				        if(TmpS.Number!=delnum)
				            {
				            s[recNumber].Number = TmpS.Number;
				            strcpy(s[recNumber].Name, TmpS.Name);
				            s[recNumber].gongzi = TmpS.gongzi;
				            s[recNumber].jiangjin = TmpS.jiangjin;
				            s[recNumber].baoxian = TmpS.baoxian;
				            s[recNumber].shuijin = TmpS.shuijin;
				            s[recNumber].heji = TmpS.heji;
				            recNumber++;
				            }
				    }
				    fclose(fp);
				        /*====将删除后的剩余结构体记录写入文件====*/
				        fp=fopen(DataFile,"wb+");
				        if (fp == NULL)
				        {
				            printf("\nSet up file %sfail !end with anykey.\n",DataFile);
				            perror("Set up fail");
				            getch();
				            exit(1);
				        }
				        for(i=0; i				        {
				            if(fwrite(&s[i],sizeof(yuangong),1,fp)!=1)
				            {
				                printf("\nWrite file %s fail!end with anykey.\n",DataFile);
				                perror("Write file fail!");
				                    getch();
				                    exit(1);
				            }
				        }
				        fclose(fp);
				    /*====显示删除后的文件====*/
				    fp=fopen(DataFile,"rb");
				    if (fp == NULL)
				    {
				        printf("\nOpen file%sfail!End with any key \n",DataFile);
				        perror("Open file fail");
				        getch();
				        exit(1);
				    }
				    printf("the file after delete is:\n");
				    printf("\nNumber\tName\tgongzi\tjiangjin\tbaoxian\n");
				    while(fread(&TmpS,sizeof(yuangong),1,fp) != (int)NULL)
				    {
				        if(TmpS.Number!=0)
				        printf("\n%ld\t%s\t%4.1f\t%4.1f\t%4.1f\t%4.1f\t%4.1f\n",TmpS.Number,TmpS.Name,TmpS.gongzi,TmpS.jiangjin,TmpS.baoxian,TmpS.shuijin,TmpS.heji);
				    }
				    fclose(fp);
				    printf("\nGo on ?(y/n)");
				    next=getche();
				    putchar('\n');
				    if ( next =='y' || next =='Y') goto begin;
				}
				
							

相关资源