0

So I have this function that shows a list of contacts separated by pages. The thing is that when the function runs, sometimes makes random fails. So I opened the debugger and the line c=fopen("Contacts.dat","rb"); says SIGSEGV (I know its an error of pointers or such, so i guess it has something to do with FILE*c, but i can't get what the problem is... so here's the code. (Variable names are in spanish, so i changed it a bit for you guys to understand it better).

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;
}
Guido
  • 11
  • 3
  • 1
    The beginning of the code (`fopen` and above) is completely fine. Probably you have undefined behaviour somewhere else, and it just happends to manifest itself when you `fopen`. – HolyBlackCat May 16 '18 at 20:39
  • If the error occurs at `c=fopen("Contact.dat","rb");` what happens if you remove the rest of the function? – user4581301 May 16 '18 at 20:39
  • 3
    If `fopen` returns a null pointer you should not pass that pointer to `fclose`. – Some programmer dude May 16 '18 at 20:39
  • 2
    And if you're programming in C++, why are you using to much old C code? C files, C [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array) (which doesn't actually exist in C++), C strings. Oh and what do you think `cout<<("Opcion: ",strlen("Opcion: "),0,0)` will do? – Some programmer dude May 16 '18 at 20:43
  • @Someprogrammerdude - quite possibly the reason for the actual problem? May be a dupe: https://stackoverflow.com/questions/16922871/why-glibcs-fclosenull-cause-segmentation-fault-instead-of-returning-error – SergeyA May 16 '18 at 20:44
  • Oh, you mean to say that the crash happens *inside* the `fopen` call? Then you have some very bad problem, quite probably some [*undefined behavior*](https://en.wikipedia.org/wiki/Undefined_behavior). If you look at the function call stack, does it look okay? If it does look okay, then the problem is most likely before the call to your function (in the code you don't show), otherwise it could be anywhere. If possible, use tools such as [Valgrind](http://valgrind.org/) to help you diagnose and locate your problem. – Some programmer dude May 16 '18 at 20:48
  • @Someprogrammerdude Idk, they teach us to code that way at University. As for `cout<<("Opcion: ",strlen("Opcion: "),0,0)`, it was actually a function created by me to display mesages in a special way with 4 parameters... but changed them all to `cout<<` to post it here for easier understanding undertanding, just missed that one. I assure you that's not the problem. Will try to delete the `fclose(c)`, and then the rest of the code to see if the error still shows up. Thanks you all for the responses. Will keep you posted. – Guido May 17 '18 at 04:43
  • If that's what your teachers are telling you to use, then I suggest you get a [few good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) of your own to study. – Some programmer dude May 17 '18 at 06:20
  • Well, I think it was actually a CodeBlocks problem... I ran the program in the university and didn't happen, so after I came back, I downloaded the last version of CodeBlocks, started the debugger and I did not happen again... I've ran it more than 40 times since then and didn't have any problem at all. Thanks all for the help and advices. – Guido May 18 '18 at 17:50

0 Answers0