Question

The error I'm getting navigates to the strcat.asm file and sets a breakpoint at main loop entrance.The readFile method I created is breaking just before the strings are tokenized at this strlen loop:

while( !feof(fptr) )
    {
        fgets(oneLine, CONTACT_MAX, fptr); // process the next line to be tokenized
        if (oneLine[strlen(oneLine) - 1] == '\n')
        {

            oneLine[strlen(oneLine) - 1] = '\0';
        }
        sn = strtok(oneLine, " , ");
        fn = sn ? strtok(NULL, " , ") : NULL;
        ph = fn ? strtok(NULL, " , ") : NULL;
        co = ph ? strtok(NULL, " , ") : NULL;

Anyone have any ideas where I'm going wrong here?

My readFile() is as follows:

struct contact *readFile( struct contact *ptrList)
{
    struct contact *head = NULL;
    struct contact *newContact;
    FILE *fptr;
    char oneLine[CONTACT_MAX];
    char *sn, *fn, *ph, *co;
    head = ptrList;



    //open test.csv to be read
    fptr = fopen("test.csv", "r");

    if( fptr == NULL )
    {
        printf("\nCouldn't open %s...", "test.csv");
        return(ptrList);
    }
    fgets(oneLine, CONTACT_MAX, fptr);

    while( !feof(fptr) )
    {
        fgets(oneLine, CONTACT_MAX, fptr); // process the next line to be tokenized
        if (oneLine[strlen(oneLine) - 1] == '\n')
        {
            oneLine[strlen(oneLine) - 1] = '\0';
        }
        sn = strtok(oneLine, " , ");
        fn = sn ? strtok(NULL, " , ") : NULL;
        ph = fn ? strtok(NULL, " , ") : NULL;
        co = ph ? strtok(NULL, " , ") : NULL;

        if ( head == NULL )
        {
            head = (struct contact *)malloc(sizeof(struct contact));
            ptrList = head;
                strcpy(head->fName,fn);
                strcpy(head->sName,sn);
                strcpy(head->phone,ph);
                strcpy(head->company,co);

            head->prev = NULL;
            head->next = NULL;

        }
        else
        {
            newContact = (struct contact *)malloc(sizeof(struct contact));
            head->next = newContact;
            newContact->prev = head;
            newContact->next = NULL;

            strcpy(newContact->fName, fn);
            strcpy(newContact->sName, sn);
            strcpy(newContact->phone, ph);
            strcpy(newContact->company, co);

            head = newContact;
        } // end of (ptrList == NULL)

    } // end of while( !feof(fptr))
    fclose(fptr);
    return(ptrList);
}

The format of the file is like this:

Surname,FirstName,Number,Company

the Builder,Bob,1234567,Bob's

Unknown,Scoop,8765645,Bob's

Was it helpful?

Solution

head = (struct contact *)malloc(sizeof(struct contact));
            ptrList = head;
                strcpy(head->fName,fn);
                strcpy(head->sName,sn);
                strcpy(head->phone,ph);
                strcpy(head->company,co);

You are derefferencing head without head's members have been initialized, thats undefined behavior and probably causing your error because strcpy is expecting a \0 terminated array. But since you haven't initialized the members of head, you can't know they are terminated or not. And even if they are, the problem si that strcpy() has to read for checking the \0 and as far it isn't initialized, strcp() is reading uninitialized memory anyway.

And finaly a standard quote:

J.2 Undefined behavior

1 The behavior is undefined in the following circumstances:

[..]

The value of the object allocated by the malloc function is used (7.20.3.3).

So a solution would be memset() head to 0 so the memblock will be everywhere terminated, as long you don't override it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top