所以我有这个功能,显示按页面分隔的联系人列表。 事情是,当函数运行时,有时会使随机失败。所以我打开了调试器和行c = fopen(“Contacts.dat”,“rb”); SIGSEGV说(我知道它是指针或类似的错误,所以我想它与FILE * c有关,但我无法得到问题所在......所以这里是代码。 (变量名是西班牙语,所以我改变了一点让你们更好地理解它。)
void listarContactos(){
struct tContact contact;
FILE*c;
c=fopen("Contact.dat","rb");
if(c==NULL){
pNULL(4);
fclose(c);
exit(1);
}
int pageLimit=3;
int qContacts=0;
while(fread(&contact,sizeof(tContact),1,c)){
if(contact.Active){
qContacts++;
}
}
int aux=0;
if(qContacts%pageLimit!=0){
aux=1;
}
int vecPag[qContacts/pageLimit+aux];
int qPages=qContacts/pageLimit+aux;
fseek(c,0,0);
vecPag[0]=ftell(c);
qContacts=0;
int nPag=0;
while(fread(&contact,sizeof(tContact),1,c)){
if(contact.Active){
qContacts++;
}
if(qContacts%pageLimit==0){
nPag++;
vecPag[nPag]=ftell(c);
}
}
nPag=0;
fseek(c,vecPag[nPag],0);
char choice[4];
bool exit=0;
while(!exit){
system("cls");
cout<<"CONTACT LIST")<<endl;
qContacts=0;
fseek(c,vecPag[nPag],0);
while(qContacts<pageLimit){
if(fread(&contact,sizeof(tContact),1,c)){
if(contact.Active){
showContacto(contact);
qContacts++;
}
}
}
cout<<"Page number: "<<nPag+1<<endl;
cout<<"4 = PREVIOUS PAGE"<<endl;
cout<<"6 = NEXT PAGE"<<endl;
cout<<"0 = EXIT"<<endl;
cout<<"Option: ";
sys::getline(choice,3);
switch(choice[0]){
case '4':{
if(nPag>0){
nPag--;
}
break;}
case '6':{
if(nPag<qPages){
nPag++;
}
break;}
case '0':{ //salir
exit=1;
break;}
default:{
cout<<"wrong option!"<<endl;
break;}
}
}
fclose(c);
return;
}