mardi 6 octobre 2015

"if" statements work individually, but not in an else if check

I have the following two soon to be three functions, the issue I am running into is: If I input the command "ADD" with the "LIST" command commented out it works great, if I uncomment "LIST" and run the "LIST" command that executes fine. However if I have "LIST" uncommented and run the "ADD" command it will write to the file however it crashes the application giving this wonderful error :

* glibc detected * ./server: double free or corruption (top): 0x0000000001680240 ***

Followed by ALOT more stuff under "memory map" etc.

    if (cmd.compare(add) == 0) /* Adds whatever follows the word "ADD" to the text file"*/

                    {

                            file_ptr = fopen ("file1.txt", "a+");
                            fprintf(file_ptr, "%d" "%s" "%s" "%s" "%s", r," ", fname, lname, phone);
                            fprintf(file_ptr, "\n");
                            fclose(file_ptr);
                            ++r;
                    }

                    else if (cmd.compare(list) ==0) /* LIST user input */
                    {
                        file_ptr = fopen("file1.txt", "r");

                    size_t count;
                    while ((count = fread(buf, 1, sizeof buf, file_ptr)) > 0)
                    {
                    send(new_s, buf, count, 0);
                    }

                    }

                            fclose(file_ptr);
                    }

Aucun commentaire:

Enregistrer un commentaire