十分感谢大家的赐教 ,我已经成功完成了课设,但我又换了一种方法,以下是代码,大家看看是不是可以更精简,或有没有问题bug:
以下只是部分代码,希望大家可以大体看明白什么意思:
int AddfromText(struct Records records[],int m,char filename[]){
int i,j;
FILE *fp1;
if((fp1=fopen(filename,"r+"))==NULL){
printf("Can not write !\n");
exit(2);
}
for(i=0;!feof(fp1);i++){
//feof()函数判断文件是否结尾
fscanf(fp1,"%s%d%s%d%d%d%d",records[m+i].driver,&records[m+i].car,records[m+i].driver,&records[m+i].car,records[m+i].police,&records[m+i].num,&records[m+i].year,&records[m+i].month,&records[m+i].date);
//结构体的变量相应的增多了
}
for(j=0;j<i-1;j++){
Insert(records,m,records[m+j]);
//已经定义了的有序插入函数
}
for(j=0;j<m+i-1;j++){
printf("%s\t%d\t%s\t%d\t%d\t%d\%d\n",records[j].driver, records[j].car,records[j].driver,records[j].car,records[j].police,records[j].num,records[j].year,records[j].month,records[j].date);
}
fclose(fp1);
return (m+i-1);
}
void WritetoText(struct Records records[],int m,char filename[]){
FILE *fp2;
int i=0;
if((fp2=fopen(filename,"a"))==NULL){
printf("Can not write !\n");
exit(2);
}
for(i=0;i<m;i++){
fprintf(fp2,"\n%s\t%d\t%s\t%d\t%d\t%d\%d",records[i].driver,records[i].car,records[i].driver,records[i].car,records[i].police,records[i].num,records[i].year,records[i].month,records[i].date);
fprintf(fp2,"\n");
}
fclose(fp2);
}