【发布时间】:2020-02-22 19:01:38
【问题描述】:
我是编程新手,我想使用C 中的qsort 函数进行数据库排序。
我们有一个包含 100 人的文件,我想通过将文本文件中的数据读取到 100 人的数据库变量中并使用struct,按他们的姓氏对其进行排序。我是新手。
这是我的代码:
int compare(const void *pa,const void *pb)
{
char *Ina = *((person_t*)pa)->last_name;
char *Inb = *((person_t*)pb)->last_name;
return strcmp(Ina,Inb);
};
while(fgetc(fp)!= EOF)
{
strcpy(dbasepeople[ctr].last_name,str1);
strcpy(dbasepeople[ctr].first_name,str2);
strcpy(dbasepeople[ctr].city_name,str3);
++ ctr;
}
所以我得到了这个错误或输出 这不是我想要的。
297608 ���� ��e2� -1674750400 0
6297698 ���� ��e2� -1674750400 0
6297788 ���� ��e2� -1674750400 0
6297878 ���� ��e2� -1674750400 0
6297968 ���� ��e2� -1674750400 0
6298058 ���� ��e2� -1674750400 0
6298148 ���� ��e2� -1674750400 0
6298238 ���� ��e2� -1674750400 0
6298328 ���� ��e2� -1674750400 0
Segmentation fault (core dumped)
我收到的警告是
main.c:16:15: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
main.c:17:15: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
Before sorting:
6295808 ���� P|�t� -1 0
6295898 ���� P|�t� 873305664 0
6295988 ���� P|�t� 873305664 0
6296078 ���� P|�t� 873305664 0
6296168 ���� P|�t� 873305664 0
【问题讨论】:
-
您没有从编译器获得任何诊断信息吗?请用
-Werror编译! -
崩溃原因会给出警告
-
您是否尝试在调试器中逐行运行程序,同时监视所有变量的值以查看它们是否具有您期望的值?您是否使用调试器来确定程序在哪一行导致了分段错误?
-
第一个格式是
%d,但是第一个打印的项目是一个字符串。 -
完成 %d 个警告@stark
标签: c file sorting qsort fgetc