帮助将 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("\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 */
fclose (fp1);
getchar();
}
我确信这一行: if(strcmp(studentNumSearch,fp1[index].studentNum)==0) 这是我出错的地方,因为我不确定如何在使用 strcmp() 函数时指向文件。 - 编辑相关代码。
I have a function void display_a_student()
which uses two binary files. Firstly a binary1.dat and and index.dat which contains the offset of each student added to the binary1.dat.
I am trying to use the index to find the offset value for a student which is entered by the user, I am having trouble using the strcmp() function to compare the value entered to those values held in the index.dat file.
Any help would be much appreciated here is the code so far.
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 */
fclose (fp1);
getchar();
}
I am certain the line: if(strcmp(studentNumSearch,fp1[index].studentNum)==0)
is where i am going wrong as i am unsure how to point to the file while using the strcmp() function.
- edited code for relevance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
strcmp
用于字符串比较。使用memcmp进行二进制比较。主要问题是您对
fp1[index]
的访问。当您访问从未分配的 FILE 元素时,这将不起作用。 fp1 不是一个数组,而是一个 FILE 指针。您需要使用
fscanf
或fread
从文件中读取数据,并使用fseek
根据每个条目的索引和大小在文件中正确定位。strcmp
is for string comparison. Usememcmp
for binary comparison.The main issue is your access to
fp1[index]
. This won't work as you access a FILE element never allocated. fp1 is not an array, but a FILE pointer.You need to use
fscanf
orfread
to read from the file andfseek
to position correctly in the file based on index and size of each entry.我认为你不应该使用 strcmp,你必须使用 fread 复制到结构中,然后使用你想要的 strcmp 。
如果你必须使用你所做的方式..你必须使用 memcmp 而不是 strcmp,但正如 Benoit 所说,你需要在执行 cmp 之前知道长度。
I dont think u should use strcmp, you have to use fread to copy into a structure and then use strcmp of what ever you want.
If you have to use the way u did.. u have use memcmp instead of strcmp, but as Benoit said u need to know the length before u do a cmp.