Question

I'm having problem to load data from a .txt files. The problem is some times the code running perfectly but most of the time I just got an run time error. The funny thing is that I haven't made any changes to the code and there is some times the error and some times no error at all!

void CustomersDB::LoadCustomersDATA()
{
    string temp;
    char *str = new char[temp.length() + 1];
    char *p;
    int i, Temp;

    ifstream CustomerFile("CustomersData.txt");
    while (getline(CustomerFile, temp))
    {
        strcpy(str, temp.c_str());
        p = strtok(str, ",");
        string IDNumber = p;
        p = strtok(NULL, ",");
        int Type = atoi(p);
        p = strtok(NULL, ",");
        string FirstName = p;
        p = strtok(NULL, ",");
        string LastName = p;
        p = strtok(NULL, ",");
        string PhoneNumber = p;
        p = strtok(NULL, ",");
        string DateOfBirth = p;
        p = strtok(NULL, ",");
        string Address = p;

        if (Type == 1)
        {
            CustomersDB::getInstance()->AddNewCustomer(new     RegularCustomers(IDNumber, Type, FirstName, LastName, PhoneNumber, DateOfBirth, Address));
            i = FindCustomerIndex(IDNumber);
            p = strtok(NULL, ",");
            Temp = atoi(p);
            while (p != NULL)
            {
                CustomersDB::getInstance()->Client[i]->addAccountsToAllowed(Temp);
                p = strtok(NULL, ",");
            }
        }
        else if (Type == 2)
        {
            CustomersDB::getInstance()->AddNewCustomer(new GoldCustomers(IDNumber, Type, FirstName, LastName, PhoneNumber, DateOfBirth, Address));
            i = FindCustomerIndex(IDNumber);
            p = strtok(NULL, ",");
            Temp = atoi(p);
            while (p != NULL)
            {
                CustomersDB::getInstance()->Client[i]->addAccountsToAllowed(Temp);
                p = strtok(NULL, ",");
            }
        }
        else if (Type == 3)
        {
            CustomersDB::getInstance()->AddNewCustomer(new PlatinumCustomers(IDNumber, Type, FirstName, LastName, PhoneNumber, DateOfBirth, Address));
            i = FindCustomerIndex(IDNumber);
            p = strtok(NULL, ",");
            Temp = atoi(p);
            while (p != NULL)
            {
                CustomersDB::getInstance()->Client[i]->addAccountsToAllowed(Temp);
                p = strtok(NULL, ",");
            }
        }
    }

    CustomerFile.close();
}

The .txt files contain the information separated by comas.

303047898,1,Josh,Hardwell,050-8103580,7/7/89,NY,0

Hope you will find the solution fast because I have tried everything and I'm lost!! Thank you!

Was it helpful?

Solution

You are creating a char array of size 1 here:

string temp; // temp.length() == 0
char *str = new char[temp.length() + 1];

Then you use it as if it had a larger size here:

strcpy(str, temp.c_str()); // temp.length() > 1 after getline
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top