📄 clearv.c
字号:
#include "stdio.h"
#include "shead.h"
void ClearVote()
{
char DataFile[10]="houshuan";
candidate *s,*t,TempS;
int total=CANDIDATENUM,i;
int recNumber=0;
FILE * fp;
/* 分配内存空间存从文件中读出的信息 */
s=(candidate*)malloc(total*sizeof(candidate));
if(s==NULL)
{
printf("malloc memory error!\n");
printf("press any key to end program");
fflush(stdin);
getch();
exit(0);
}
clrscr();
fp=fopen(DataFile,"rb");
if(fp==NULL)
{
printf("Clear Vote Error");
printf("\nPress any key to continu...\n");
free(s);
getch();
return;
}
while(fread(&s[recNumber],sizeof(candidate),1,fp)!=(int)NULL)
{
s[recNumber].votenum=0;/* 清空投票信息 */
recNumber++;
if(recNumber>=total)
{
total+=INCREASE;/* 增加存储空间 */
t=(candidate*)realloc(s,total*sizeof(candidate));
if(t==NULL)
{
free(s);
printf("rellot memory fail");
fflush(stdin);
getch();
exit(0);
}
s=t;
t=NULL;
}
}
fclose(fp);
fp=fopen(DataFile,"wb+");
if(fp==NULL)
{
printf("Clear Vote Error");
printf("\nPress any key to continu...\n");
free(s);
getch();
return;
}
for(i=0;i<recNumber;i++)
{
if(fwrite(&s[i],sizeof(candidate),1,fp)!=1)
{
printf("write file %s fail\n",DataFile);
perror("write file fail");
fflush(stdin);
getch();
exit(0);
}
}
fclose(fp);
fp=fopen(DataFile,"rb");
printf("Clear vote success!!\n\nNumber\tName\t\tvote\n");
while(fread(&TempS,sizeof(candidate),1,fp)!=(int)NULL)
{
printf("%ld\t%-8s\t\t%d\n",TempS.Number,TempS.Name,TempS.votenum);
}
fclose(fp);
fflush(stdin);
printf("Press any key to continue...");
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -