我有一个void display_a_student()使用两个二进制文件的函数 .首先是binary1.dat和index.dat,它包含添加到binary1.dat的每个学生的偏移量.
我正在尝试使用索引来查找用户输入的学生的偏移值,我在使用strcmp()函数将输入的值与index.dat文件中保存的值进行比较时遇到问题.
任何帮助将非常感谢这里是目前为止的代码.
void display_a_student()
{
struct student aStudent;
char studentNumSearch[11];
int index=0;
int found = false;
fp = fopen("binary1.dat", "a+b");
fp1 = fopen("index.dat", "a+b");
printf("\n\nWhich student are you searching for?");
scanf("%s", studentNumSearch);
fflush(stdin);
while(!found && index < 10)
{
if(strcmp(studentNumSearch,fp1[index].studentNum)==0)
{
found = true;
}
index++;
}
if (found)
{
fseek(fp, fp1[index].offset, SEEK_SET);
fread(&aStudent,sizeof(struct student),1,fp);
printf("\n\nThe student name is %s\n",aStudent.firstName);
}
else
{
printf("\n\nNo such student\n");
}
fclose( fp ); /* fclose closes file */ …Run Code Online (Sandbox Code Playgroud)