文件处理相关的问题,求解答!怎么解啊??

我这代码写出来会陷入死循环不知道为啥。。。有大佬能帮忙看看吗
题目是显示一个清单显示工具的数量,名称,号码,成本,需要用文件的相关知识,程序需要能罗列,删除,覆盖,加入数据
// wenj.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
struct Tools
{
unsigned int record;
char name[20];
unsigned int number;
double cost;
};//定义结构体储存工具相关信息

void textfile(FILE *readptr);
void deleterecord(FILE *fptr);
void newrecord(FILE *fptr);

int main(int argc, char* argv[])
{
FILE *cfptr;
if((cfptr=fopen("hardware.dat","wb"))==NULL)
{
printf("文件不能打开\n");
}
else
{
struct Tools blanktool={0,"",0,0};
int i=0;
for(i=0;i<100;i++)
{
fwrite(&blanktool,sizeof(struct Tools),1,cfptr);
}
}//先储存100条空记录

int j=0;
for(j=0;;j++)
{
    int choice;
    printf("输入你的选择\n");
    printf("1表示保存\n2表示更新数据\n3表示加入数据记录\n4表示删除数据\n5表示结束程序\n?");
    scanf("%d",&choice);
    if(choice==1)   
    {
        textfile(cfptr);continue;//输入1表示保存txt文件
    }
    else if(choice==2)  
    {
        deleterecord(cfptr);
        newrecord(cfptr);//输入2表示删除一条旧的记录并保存一条新的记录
        continue;
    }
    else if(choice==3)  
    {
        newrecord(cfptr);continue;//输入3表示输入一条新纪录
    }
    else if(choice==4)  
    {
        deleterecord(cfptr);continue;//输入4表示删除某一条记录
    }
    else 
    {
        break;
    }//输入相关选择
}
fclose(cfptr);
return 0;

}

void textfile(FILE *readptr)//保存txt文件
{
FILE *writeptr;
if((writeptr=fopen("hardware.txt","w"))==NULL)
{
printf("文件无法打开\n");
}
else
{
rewind(readptr);
fprintf(writeptr,"%d %-20s %d %lf\n","record","name","number","cost");
while(!feof(readptr))
{
struct Tools gongjv={0,"",0,0};
int result=fread(&gongjv,sizeof(struct Tools),1,readptr);
if(result!=0&&gongjv.record!=0)
{
fprintf(writeptr,"%d %-20s %d %d\n",gongjv.record,gongjv.name,gongjv.number,gongjv.cost);
}
}
fclose(writeptr);
}
}

void deleterecord(FILE *fptr)//删除记录
{
printf("输入你想删除的工具的序号1-100\n");
int num;
scanf("%d",&num);
fseek(fptr,(num-1) *sizeof(struct Tools),SEEK_SET);
struct Tools gongjv;
fread(&gongjv,sizeof(struct Tools),1,fptr);
if(gongjv.record==0)
{
printf("记录不存在\n");
}
else
{
fseek(fptr,(num-1) *sizeof(struct Tools),SEEK_SET);
struct Tools blanktool={0,"",0,0};
fwrite(&blanktool,sizeof(struct Tools),1,fptr);
}
}

void newrecord(FILE *fptr)//输入新记录
{
printf("输入你想加入的工具的序号1-100\n");
int num;
scanf("%d",&num);
fseek(fptr,(num-1) *sizeof(struct Tools),SEEK_SET);
struct Tools gongjv={0,"",0,0};
fread(&gongjv,sizeof(struct Tools),1,fptr);
if(gongjv.record!=0)
{
printf("该记录非空\n");
}
else
{
printf("输入数据\n");
scanf("%-20s %d %d\n",&gongjv.name,&gongjv.number,&gongjv.cost);
gongjv.record=num;
fseek(fptr,(gongjv.record-1)*sizeof(struct Tools),SEEK_SET);
fwrite(&gongjv, sizeof(struct Tools),1,fptr);
}
}

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问