Please please please help

This is a discussion on Please please please help within the C Programming forums, part of the General Programming Boards category; I am trying to compare two files and find the unique lines. The lines can be of different length, and ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    31

    Please please please help

    I am trying to compare two files and find the unique lines. The lines can be of different length, and the files can be of different length. I have to use Doubled Linked List. And I cant use unix commands liek diff

    It is under the unix platform.

    I cant seem to find why it isnt working. The check function is supposed to mycheck each line from file 1 with all lines of file 2. The check variable is suposed to see if all the words of the line/linked list pNode3 is same or not. If it is found to be same, then yes ===1. In that case, we know that the line is not unique. if yes!=1, then lline is unique and I just print it by calliing display3


    But it isnt working. Please please help. I am desparate

    Code:
    #include "diffhead.h"
    #define NullChar '\0'
    
    /*structure of the double linked list*/
    struct NODE2 
    {
        struct NODE2 *pNext;
        struct NODE2 *pPrev;
        char* nData;
    }*pHead, *pTail, *pNode,*pHead2, *pTail2, *pNode2,*pHead3,*pTail3,*pNode3;
    
    struct COUNT
    {
        int lineNo; /*line no*/
        int words;  /*total word count*/
    }file, file2;
    
    /*PROTOTYPES */
    void midFunct(char *ch1);
    void adddll2(char *ch2);
    void addll3(char *ch2);
    void display(struct NODE2 *pNode, struct NODE2 *pNode2);
    int strcomp(const char *src1,const char *src2);
    void blankDiff(char * a , char * b);
    int getword(char *word, int lim, FILE * instream);
    int myCheck(struct NODE2* pNode2,struct NODE2* pNode3,char *newlinetest);
    void display3(struct NODE2 *p
    Node3);
    void removeMe(struct NODE2* pNode3);
    	       
    	       
    /*man page for mdiff*/
    void help()
    {
        printf("Usage is: mdiff [option] filespec1 filespec2\n");
        printf("Options:\n");
        printf(" -help\t \n");
        printf("  -b\t \n");
        printf("  -c\t.\n");
        printf("  -i\t \n");
        printf("  -q\t \n");
        printf("  -v No diff\t \n");
        printf("filespec2 can be a drive or directory name.\n");
    }
    
    
    
    int main(int argc, char **argv)
    {
        
        int opt;
        /* options -> the inputs that user has */
        extern int getopt(int argc, char *argv[], char *optionS);
        while ((opt = getopt(argc, argv, "bciqv")) != EOF) 
    	{
    	    switch (opt) 
    		{
    		case '?':
    		    printf("Invalid command line option\n");
    		    help();
    		    return(1);
    		case 'b':
    		    blankDiff(argv[2], argv[3]);
    		    break;
    		case 'c':
    		    break;
    		case 'i':
    		    break;
    		case 'q':
    		    break;
    		case 'v':
    		    break;
    		}
    	}
        return EXIT_SUCCESS;
    }
    
    
    
    void midFunct(char *ch1)
    {
        
        pNode = malloc (sizeof (struct NODE2));
        pNode->nData = ch1;
    
        
        if (pHead == NULL) {
    	pHead = pNode;
    	pNode->pPrev = NULL;
        }
        else {
    	pTail->pNext = pNode;
    	pNode->pPrev = pTail;
        }
        pTail = pNode;
        pNode->pNext = NULL;
    }
    
    
    
    void adddll2(char *ch2)
    {
        pNode2 = malloc (sizeof (struct NODE2));
        pNode2->nData = ch2;
        if (pHead2 == NULL) {
    	pHead2 = pNode2;
    	pNode2->pPrev  = NULL;
        }
        else {
    	pTail2->pNext = pNode2;
    	pNode2->pPrev = pTail2;
        }
        
        pTail2  = pNode2;
        pNode2->pNext = NULL;
    
    
    }
    
    
    void display3(struct NODE2 * pNode3)
    {
        pNode3=pHead3;
        while(pNode3)
            {
                printf("%s",pNode3->nData);
                pNode3=pNode3->pNext;
            }
    }
    
    /*test stub only*/
    /*test function to display link lists*/
    void display(struct NODE2 * pNode, struct NODE2 *pNode2)
        /* struct NODE *pNode3)*/
    {
        pNode=pHead;
        while(pNode)
    	{
    	    printf("<%s>",pNode->nData);
    	    pNode=pNode->pNext;
    	}
        pNode2=pHead2;
        while(pNode2)
    	{
    	    printf("[%s]",pNode2->nData);
    	    pNode2=pNode2->pNext;
    	}
    }
    
    int strcomp ( const char *_src1, const char *_src2 ) 
    {
        while ( *_src1 == *_src2++ ) 
    	if ( *_src1++ == '\0' ) 
    	    return 0;
        /* while ( *_src1 == *_src2++ ) */
        
        if ( *_src1 == '\0' )   
    	return -1;
        
        if ( *--_src2 == '\0' ) 
    	return 1;
        
       return ( ( ( unsigned char ) *_src1 ) - ( ( unsigned char ) *_src2 ) );
    }
    /* int strcomp ( const char *_src1, const char *_src2 ) */
    
    void adddll3(char *ch3)
    {
        pNode3 = malloc (sizeof (struct NODE2));
        pNode3->nData = ch3;
        if (pHead3 == NULL) {
            pHead3 = pNode3;
            pNode3->pPrev  = NULL;
        }
        else {
            pTail3->pNext = pNode3;
            pNode3->pPrev = pTail3;
        }
        pTail3  = pNode3;
        pNode3->pNext = NULL;
        
    }
    
    
    /* getword:  get next word or character from input */
    int getword(char *word, int lim, FILE * instream)
    {
        int c;
        char *w = word;
        while ((isspace(c = fgetc(instream))) && (c !='\n'))
            ;
        
        if (c != EOF)
            *w++ = c;
        
        if (!isalpha(c) ) {
            *w = NullChar;
            return c;
        }
        
        for ( ; --lim > 0; w++)
            if (!isalnum(*w = fgetc(instream))) {
                ungetc(*w,instream);
                break;
            }
        
        *w = NullChar;
        if (c == EOF)
            return EOF;
        return word[0];
    }
    
     
    void blankDiff(char* a, char* b)
    {
        char *array1;
        char *array2;
        FILE *instream;
        FILE *instream2;
        instream=fopen(a,"r");
        instream2=fopen(b,"r");
        char *c, *d;
        
        if(instream==NULL){
    	c=strcpy("\\",a);
    	instream=fopen(c,"r");
        }
        if(instream2==NULL){
    	d=strcpy("\\",b);
    	instream2=fopen(d,"r");
    	}
        a=c;
        b=d;
    
        if((instream == NULL) || (instream2 == NULL))
    	{
    	    printf ("File Open Error!");
    	    exit(99);
    	}
        int d2, k2;
        file.words=file2.words=0;
        file.lineNo=file2.lineNo=0;
    
        while((d2 != -1) || (k2 != -1))
    	{
    	    array1=malloc(100*sizeof(array1));
    	    array2=malloc(100*sizeof(array2));
    	    d2=getword(array1,MAXWORDS,instream);
    	    k2=getword(array2,MAXWORDS,instream2);
    	    midFunct(array1);
    	    adddll2(array2);
    	    file.words = file.words +1;
    	    file2.words=file2.words+1;
    	    if(d2 == '\n' )
    		    file.lineNo=file.lineNo +1;
    	    if(k2 == '\n')
    		    file2.lineNo=file.lineNo +1;
    	}
        
        char *array3=malloc(100*sizeof(array3));
        
        char *newlinetest=malloc(100*sizeof(char));
        strcpy(newlinetest,"\n");
    
        char *spacetest=malloc(100*sizeof(char));
        strcpy(spacetest,"");
    
        pNode=pHead;
        pNode2=pHead2;
        pNode3=pHead3;
    
        register int temptest=1;
        int value=0;
        
        while(pNode!=NULL)
    	{
    	    
    	    while(pNode3!=pHead3){
    		if(pNode3 !=NULL){
    		    pNode3=pNode3->pPrev;
    		    removeMe(pNode3->pNext);
    		}
    		
    	    }
    	    
    	    while(temptest != 0)
    		{
    		    temptest=strcomp(pNode->nData,newlinetest);
    		    array3=pNode->nData;
    		    adddll3(array3);
    		    pNode=pNode->pNext;
    		    if(pNode==NULL)
    			break;
    		}
    	    
    
    	    value=myCheck(pNode2,pNode3,newlinetest);
    	    if(value==1)
    		display3(pNode3);
    	    
    	    temptest=1;
    	}
    
    }
    void removeMe(struct NODE2* pNode3)
    {
        if(pNode3==pHead3)
    	return;
        
        
        if(pNode3->pPrev == NULL)
    	pHead3=pNode3->pNext;
        else
    	pNode3->pPrev->pNext=pNode3->pNext;
    
        if(pNode3->pNext == NULL)
    	pTail3=pNode3->pPrev;
        else
    	pNode3->pNext->pPrev=pNode3->pPrev;
        free(pNode3);
    }
    
    
    int myCheck(struct NODE2* pNode2,struct NODE2 *pNode3,char *newlinetest)
    {
        int compare,yes,check,length;
        yes=0;
        
        if(file2.lineNo > file.lineNo)
    	length=file.lineNo;
        else 
    	length=file2.lineNo;
        
        
        pNode3=pHead3;
        pNode2=pHead2;
        
        check=0;
    
        while(pNode2!=NULL)
    	{
    	    
    	    if(pNode3==NULL)
    		break;
    
    	    
    	    length--;
    	    
    
    
    	    
    	    compare=strcomp(pNode2->nData,pNode3->nData);
    
    	    if(compare !=0)
    		check++;
    
    
    
    	    if((strcomp(pNode3->nData,newlinetest)!=0)&& (strcomp(pNode2->nData,newlinetest) ==0)){
    		
    		    while(strcomp(pNode3->nData,newlinetest)!=0)
    			pNode3=pNode3->pNext;
    	    }
    	    
    	    
    	    if((strcomp(pNode3->nData,newlinetest)==0) && (strcomp(pNode2->nData,newlinetest)!=0)){
    		
    		    while(strcomp(pNode2->nData,newlinetest)!=0)
    			pNode2=pNode2->pNext;
    	    }
    	    
    
    
    	    if(strcomp(pNode2->nData,newlinetest)==0){
    
    		
    		if(check==1){
    		    yes=1;
    		}
    		check=0;
    		pNode3=pHead3;
    	    }
    
    	    if(strcomp(pNode2->nData,newlinetest)==0)
    		check=0;
    	    
    		
    	    pNode2=pNode2->pNext;
    	    pNode3=pNode3->pNext;
    	    
    	}
        
        return yes;
        
        
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    All the words, or identical lines:

    Line1: foo bar baz
    Line2: baz bar foo

    Are these lines identical or not? What about these?

    Line1: Foo bar baz
    line2: foo bar baz

    If it is in fact identical lines, why not do something like:
    Code:
    int unique( Node *n1, Node*n2 )
    {
        Node *n;
        for( n = n2; n; n = n->next )
        {
            if( !strcmp( n1->data, n->data ) )
                return 0; /* return if we hit an identical line */
        }
        return 1; /* if you reach here, the line wasn't found */
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    31
    Line1: Foo bar baz
    line2: foo bar baz
    are not identical...

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21