为什么这个程序读不了文件呢,出错了!
#include<stdio.h>#include<stdlib.h>
#include<string.h>
#define NUMB_RECS 30
#define LNAME_SIZE 25
#define FNAME_SIZE 25
#define PNUM_SIZE 12
#define MAX_REC_SIZE (LNAME_SIZE+FNAME_SIZE+PNUM_SIZE)
#define NO_FIND (-999)
#define BLANK ' '
void selection_sort(char array[][MAX_REC_SIZE+2],int size1);
void twapt(char array[][MAX_REC_SIZE+2],int current,int low);
int binary_search(char candidate[],char array[][MAX_REC_SIZE+2],int size);
main()
{
char students[NUMB_RECS][MAX_REC_SIZE+2];
char candidate[LNAME_SIZE+2];
int cand_len;
int index;
int i;
FILE *fp;
fp=fopen("students.dat","r");
for(i=0;i<NUMB_RECS;++i)
fgets(students[i],MAX_REC_SIZE+2,fp);
selection_sort(students,NUMB_RECS);
printf("\n\nEnter a students name, or signal EOF to halt");
while (fgets(candidate,LNAME_SIZE+2,stdin)!=NULL){
cand_len=strlen(candidate)-1;
for(i=cand_len;i<LNAME_SIZE;i++)
candidate[i]=BLANK;
candidate[LNAME_SIZE]='\0';
index=binary_search(candidate,students,NUMB_RECS);
if(index!=NO_FIND)
printf("\nRecord: %s",students[index]);
else {
candidate[cand_len]='\0';
printf("\n\t%s is not in our directory.",candidate);
}
printf("\n\nEnter a student's name,or signal EOF to halt:");
}
return EXIT_SUCCESS;
}
void selection_sort(char array[][MAX_REC_SIZE+2],int size1)
{
int smallest_index;
int i,j;
for(i=0;i<size1-1;++i){
smallest_index=i;
for(j=i+1;j<size1;++j)
if(strcmp(array[j],array[smallest_index])<0)
smallest_index=j;
if(i!=smallest_index)
twapt(array,i,smallest_index);
}
}
void twapt(char array[][MAX_REC_SIZE+2],int current,int low)
{
char temp[MAX_REC_SIZE+2];
strcpy(temp,array[current]);
strcpy(array[current],array[low]);
strcpy(array[low],temp);
}
int binary_search(char candidate[],char array[][MAX_REC_SIZE+2] ,int size)
{
int first=0;
int last=size-1;
int mid;
int flag;
while (first<=last){
mid=(first+last)/2;
flag=strncmp(candidate,array[mid],LNAME_SIZE);
if(flag==0)
return mid;
if(flag>0)
first=mid+1;
else
last=mid-1;
}
return NO_FIND;
}
出现了这个图像
各位高手帮帮忙啊啊