帮助将Strcmp()与二进制文件一起使用
我有一个函数
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("nnWhich 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("nnThe student name is %sn",aStudent.firstName);
}
else
{
printf("nnNo such studentn");
}
fclose( fp ); /* fclose closes file */
fclose (fp1);
getchar();
}
我确定这行:if(strcmp(studentNumSearch,fp1 [index] .studentNum)== 0)
是我出错的地方,因为我不确定如何在使用strcmp()函数时指向该文件。
- 编辑相关代码。
没有找到相关结果
已邀请:
2 个回复
娠频摩
用于字符串比较。使用
进行二进制比较。 主要问题是您访问
。当您访问从未分配的FILE元素时,这将不起作用。 fp1不是数组,而是FILE指针。 您需要使用
或
从文件中读取和
根据每个条目的索引和大小在文件中正确定位。
盛虱