My code its a double linked list of a products database, i have the options to input a product, search, delete, modify, show, save (.bin) and upload(.bin) All the options works before i use the load binary option. My problem is that when i upload the .bin with products the input a product option doesn´t and the delete a product does not work when i want to delete the last product in the list (all the others options works).
This is the input a product code:
void inP() {
    product* nuevo = new product();
    cout << "Ingrese el nombre del producto: ";
    cin >> nuevo->name;
    if (first == NULL) {
        first = nuevo;
        first->next = NULL;
        first->previous = NULL;
        last = first;
    }
    else {
        last->next = nuevo;
        nuevo->next = NULL;
        nuevo->previous = last;
        last = nuevo;
    }
    cout << "Producto agregado correctamente a la lista" << endl;
}
This is the delete a product code:
void deleteP() {
    product* current = new product();
    current = first;
    product* prev = new product();
    prev = NULL;
    bool found = false;
    string searchP;
    cout << "Ingrese el producto a eliminar: ";
    cin >> searchP;
    if (first != NULL) {
        while (current != NULL && found != true) {
            if (current->name == searchP) {
                cout << "\n Producto ( " << searchP << " ) Encontrado \n" << endl;
                cout << "\n" << current->name << " " << current->cant << " " << current->code << " " << current->marca << " " << current->descr << " " << current->monto << "\n";
                if (current == first) {
                    if (first->next == NULL) {
                        first = NULL;
                    }
                    else {
                        first = first->next;
                        first->previous = NULL;
                    }
                }
                else if (current == last) {
                    prev->next = NULL;
                    last = prev;
                }
                else {
                    prev->next = current->next;
                    current->next->previous = prev;
                
                }
                cout << "\n Producto Eliminado" << endl;
                found = true;
            }
            prev = current;
            current = current->next;
        }
        if (!found) {
            cout << "\n Producto no encontrado \n" << endl;
        }
    }
    else {
        cout << "\n La lista de productos esta vacia \n" << endl;
    }
}
This is the load binary code:
void loadBin() {
    ifstream is(registryName, ios::in | ios::binary);
    product* reader;
    
    if (is.is_open()) {
        is.seekg(0, ios::end);
        int size = is.tellg();
        is.seekg(0, ios::beg);
        while (is.tellg() < size) {
            reader = new product;
            is.read((char*)reader, sizeof(product));
            reader->next = NULL;
            cout << reader->name << endl;
            
            if (first == NULL) {
                first = reader;
            }
            else {
                product* indice = first;
                while (indice->next != NULL) {
                    indice = indice->next;
                }
                indice->next = reader;
            }
        }
        is.close();
    }
}
And all the code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string registryName = "products.bin";
struct product {
    string name;
    product* next;
    product* previous;
}*first, * last;
void inP();
void outP();
void searchP();
void modifyP();
void deleteP();
void saveBin();
void loadBin();
int main() {
    
    int op;
    do {
        system("CLS");
        cout << "Menu:" << endl;
        cout << "1. Input a Product " << endl;
        cout << "2. Show the list " << endl;
        cout << "3. Search a Product " << endl;
        cout << "4. Modify a product " << endl;
        cout << "5. Delete a product " << endl;
        cout << "6. Save List " << endl;
        cout << "7. Load Binary List " << endl;
        cout << "8. Exit" << endl;
        cin >> op;
        cout << "\n";
        switch (op) {
        case 1: {
            inP();
            cout << "\n";
            system("PAUSE");
            break;
        }
        case 2: {
            outP();
            system("PAUSE");
            break;
        }
        case 3: {
            searchP();
            system("PAUSE");
            break;
        }
        case 4: {
            modifyP();
            system("PAUSE");
            break;
        }
        case 5:
        {
            deleteP();
            system("PAUSE");
            break;
        }
        case 6: {
            saveBin();
            break;
        }
        case 7: {
            loadBin();
            break;
        }
        case 8: {
            return 0;
        }
        default: cout << "No ingreso una opcion disponible" << endl;
            break;
        }
    } while (op!=8);
    return 0;
}
void inP() {
    product* nuevo = new product();
    cout << "Ingrese el nombre del producto: ";
    cin >> nuevo->name;
    if (first == NULL) {
        first = nuevo;
        first->next = NULL;
        first->previous = NULL;
        last = first;
    }
    else {
        last->next = nuevo;
        nuevo->next = NULL;
        nuevo->previous = last;
        last = nuevo;
    }
    cout << "Producto agregado correctamente a la lista" << endl;
}
void outP() {
    product* current = new product();
    current = first;
    if (first != NULL) {
        while (current != NULL) {
            cout << "\n" << current->name;
            current = current->next;
        }
    }
    else {
        cout << "No hay productos en la lista" << endl;
        cout << "\n";
    }
    cout << "\n" << endl;
}
void searchP() {
    product* current = new product();
    current = first;
    bool found = false;
    string searchP;
    cout << "Ingrese el producto a buscar: ";
    cin >> searchP;
    if (first != NULL) {
        while (current != NULL && found != true) {
            if (current->name == searchP) {
                cout << "\n Producto ( " << searchP << " ) Encontrado \n" << endl;
                found = true;
            }
            current = current->next;
        }
        if (!found) {
            cout << "\n Producto no encontrado \n" << endl;
        }
    }
    else {
        cout << "\n La lista de productos esta vacia \n" << endl;
    }
}
void modifyP() {
    product* current = new product();
    current = first;
    bool found = false;
    string searchP;
    cout << "Ingrese el producto a modificar: ";
    cin >> searchP;
    if (first != NULL) {
        while (current != NULL && found != true) {
            if (current->name == searchP) {
                cout << "\n Producto ( " << searchP << " ) Encontrado \n" << endl;
                cout << "\n Ingrese el nuevo nombre del Producto: ";
                cin >> current->name;
                cout << "\n Producto Modificado Correctamente \n" << endl;
                found = true;
            }
            current = current->next;
        }
        if (!found) {
            cout << "\n Producto no encontrado \n" << endl;
        }
    }
    else {
        cout << "\n La lista de productos esta vacia \n" << endl;
    }
}
void deleteP() {
    product* current = new product();
    current = first;
    product* prev = new product();
    prev = NULL;
    bool found = false;
    string searchP;
    cout << "Ingrese el producto a eliminar: ";
    cin >> searchP;
    if (first != NULL) {
        while (current != NULL && found != true) {
            if (current->name == searchP) {
                cout << "\n Producto ( " << searchP << " ) Encontrado \n" << endl;
                if (current == first) {
                    if (first->next == NULL) {
                        first = NULL;
                    }
                    else {
                        first = first->next;
                        first->previous = NULL;
                    }
                }
                else if (current == last) {
                    prev->next = NULL;
                    last = prev;
                }
                else {
                    prev->next = current->next;
                    current->next->previous = prev;
                
                }
                cout << "\n Producto Eliminado" << endl;
                found = true;
            }
            prev = current;
            current = current->next;
        }
        if (!found) {
            cout << "\n Producto no encontrado \n" << endl;
        }
    }
    else {
        cout << "\n La lista de productos esta vacia \n" << endl;
    }
}
void saveBin() {
    ofstream os(registryName, ios::out | ios::binary);
    if (os.is_open()) {
        product* indice = first;
        while (indice != NULL) {
            os.write((char*)indice, sizeof(product));
            indice = indice->next;
        }
        os.close();
    }
}
void loadBin() {
    ifstream is(registryName, ios::in | ios::binary);
    product* reader;
    
    if (is.is_open()) {
        is.seekg(0, ios::end);
        int size = is.tellg();
        is.seekg(0, ios::beg);
        while (is.tellg() < size) {
            reader = new product;
            is.read((char*)reader, sizeof(product));
            reader->next = NULL;
            cout << reader->name << endl;
            
            if (first == NULL) {
                first = reader;
            }
            else {
                product* indice = first;
                while (indice->next != NULL) {
                    indice = indice->next;
                }
                indice->next = reader;
            }
        }
        is.close();
    }
}