0

I am trying to compare the string data in each file by storing each line of data in a linked list. I am trying to use the search method given to display the non-repeated records that are in file2 but not in file1. These files are provided as command line arguments. Im not sure what i am doing wrong to compare and display the non repeated records.

MAIN

int main(int argc, char *argv[]) {


// FIRST FILE BEING CREATED AND FILLED
struct Node *ptr = NULL;

FILE *fp;
char str[80], fname[50];

if ((fp= fopen(argv[1], "r")) == NULL) {
    fprintf(stderr, "%s", "cannot open file");
    exit(1);
}

int i =0;
while(!feof(fp)) {
    fgets(str,79,fp);
    if(!feof(fp)) { 
        insert(ptr,strdup(str), "12/19/03", "Chris", "Squire", "$83.15", true);
        //printf("%s",str); 
    }
}
fclose(fp);
print();

struct Node **point = (struct Node**)malloc(sizeof(struct Node*));
point = &ptr;

// SECOND FILE BEING CREATED AND FILLED
struct Node *ptr2 = NULL;

FILE *fp2;
char str2[80], fname2[50];

if ((fp2= fopen(argv[2], "r")) == NULL) {
    fprintf(stderr, "%s", "cannot open file");
    exit(1);
}
i =0;
while(!feof(fp2)) {
    fgets(str2,79,fp2);
    if(!feof(fp2)) {
        if(search(strdup(str2), point)) {
            printf("THE SECOND LIST CONTAINS %s FROM THE FIRST LIST", str2);
            insert(ptr2, strdup(str2),"12/19/03", "Chris", "Squire", "$83.15", true);
        //printf("%s",str2); 
        }
    }
}
fclose(fp2);
print();
}

INSERT FUNCTION IN NODE.C

struct Node* insert(struct Node *ptr, char data[], char tdate[], char fname[], char lname[], char amount[],         bool addtoend)
{
    if(NULL == head)
    {
        return (makelist(data, tdate, fname, lname, amount));
    }

    //if(addtoend)
        //printf("\n Adding node to end of list with value [%s]\n",data);
    //else
        //printf("\n Adding node to beginning of list with value [%s]\n",data);

    ptr = (struct Node*)malloc(sizeof(struct Node));
    if(NULL == ptr)
    {
        printf("\n Node creation failed \n");
        return NULL;
    }
    ptr->data = data;
    ptr->transdate = tdate;
    ptr->firstname = fname;
    ptr->lastname = lname;
    ptr->tamount = amount;
    ptr->next = NULL;

    if(addtoend)
    {
        curr->next = ptr;
        curr = ptr;
    }
    else
    {
        ptr->next = head;
        head = ptr;
    }
    return ptr;
}

SEARCH FUNCTION IN NODE.C

bool search(char data[], struct Node **prev)
{
    struct Node *ptr = head;
    struct Node *tmp = NULL;
    bool found = false;

    printf("\n Searching the list for value [%s] \n",data);

    while(ptr != NULL)
    {
        if(ptr->data == data)
        {
            found = true;
            break;
        }
        else
        {
            tmp = ptr;
            ptr = ptr->next;
        }
    }

    if(true == found)
    {
        if(prev)
            *prev = tmp;
        return true;
    }
    else
    {
        return false;
    }
}
Michael Cera
  • 59
  • 1
  • 1
  • 4
  • 1
    I have not looked much at your code, but there is a logistical problem when there is a line mismatch. Do you search file 1 for the line which matches the next line in file 2, or the other way round? Suppose there are 2 extra or missing lines? And BTW please see [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Weather Vane Dec 06 '16 at 20:06
  • 1
    How are you running the program? How is the behavior you are seeing different than the behavior you are expecting? Can you reduce this to an [MVCE](https://stackoverflow.com/help/mcve)? – Tim Dec 06 '16 at 20:08

0 Answers0