1

I have coded a function to read in a csv file but half way through the parsing the program crashes giving me errors in strcat.The errors are at the third field which is phone.I can't spot the error I have made in this read function.Anyone have an idea where I'm going of track here?

struct contact *readFile( struct contact *ptrList)
{
    struct contact *head, *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...");
        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 = strtok(NULL, " , ");
        ph = strtok(NULL, " , ");
        co = strtok(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);

This is how I defined contact:

struct contact {
                char sName[CONTACT_MAX+1];
                char fName[CONTACT_MAX+1];
                char phone[CONTACT_MAX+1];
                char company[CONTACT_MAX+1];
                struct contact *prev;
                struct contact *next;
                };
Valeri Atamaniouk
  • 5,125
  • 2
  • 16
  • 18

1 Answers1

1

Here I've tried to summarize:

sn = strtok(oneLine, " , ");
fn = strtok(NULL, " , ");
ph = strtok(NULL, " , ");
co = strtok(NULL, " , ");

You are relying heavily on correct format, which might not be the case.

sn = strtok(oneLine, " , ");
fn = sn ? strtok(NULL, " , ") : NULL;
ph = fn ? strtok(NULL, " , ") : NULL;
co = ph ? strtok(NULL, " , ") : NULL;

if (!co) continue; // bad string

As BLUEPIXY noted:

printf("\nCouldn't open %s...", "test.csv");

Allocations can look simpler in C:

head = malloc(sizeof(*head));

fgets can fail:

if (fgets(oneLine, CONTACT_MAX, fptr) == NULL) break; // error, do something...

And un-initialized head variable (thanks to Kninnug):

struct contact *head = NULL; // otherise it contains garbage
Valeri Atamaniouk
  • 5,125
  • 2
  • 16
  • 18