![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2004
Posts: 3
Rep Power: 0
![]() |
Hi I neeed big help. I am writting this program that will find the differenc
e between two text files such that if file lao contains The Way that can be told of is not the eternal Way; The name that can be named is not the eternal name. The Nameless is the origin of Heaven and Earth; The Named is the mother of all things. Therefore let there always be non-being, so we may see their subtlety, And let there always be being, so we may see their outcome. The two are the same, But after they are produced, they have different names. The Nameless is the origin of Heaven and Earth; The named is the mother of all things Therefore let there always be non-being, so we may see their subtlety, And let there always be being, so we may see their outcome. The two are the same, But after they are produced, they have different names. They both may be called deep and profound. Deeper and more profound, The door of all subtleties! FILE1: The Way that can be told of is not the eternal Way; The name that can be named is not the eternal name. I think I am very close... I have problems in my check(...) functions where I will check each line of file2 with all lines of file3 to see if there is a ny unique lines..... But I cant seem to get it working. ANy help please?? Here is what I am doing right now..... please help #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;
/*pNode3 is only used for the temp purpose during complex cursor pattern of checking two files*/
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);
/* getword: get next word or character from input */
int getword(char *word, int lim, FILE * instream);
void check(struct NODE2* pNode2,struct NODE2*pNode3,char *newlinetest);
void display3(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");
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;
while(pNode!=NULL)
{
while(pNode3!=pHead3){
pNode3=pNode3->pPrev;
pNode3->nData="";
}
while(temptest != 0)
{
temptest=strcomp(pNode->nData,newlinetest);
array3=pNode->nData;
adddll3(array3);
pNode=pNode->pNext;
if(pNode==NULL)
break;
}
pNode3=pHead3;
check(pNode2,pNode3,newlinetest);
temptest=1;
}
}
void check(struct NODE2* pNode2,struct NODE2 *pNode3,char *newlinetest)
{
int compareResult;
int noDiffLine=0;
for(pNode2=pHead2,pNode3=pHead3;pNode2!=NULL;pNode2=pNode2->pNext,pNode3=pNode3->pNext)
{
compareResult=strcomp(pNode2->nData,pNode3->nData);
if(compareResult !=0){
noDiffLine++;
printf("%s",pNode3->nData);
}
if((pNode3 !=NULL) && (strcomp(pNode2->nData,newlinetest) ==0))
{
while(strcomp(pNode3->nData,newlinetest)!=0)
pNode3=pNode3->pNext;
}
if((pNode3==NULL) && (strcomp(pNode2->nData,newlinetest)!=0))
{
while(strcomp(pNode2->nData,newlinetest)!=0)
pNode2=pNode2->pNext;
}
if(strcomp(pNode3->nData,newlinetest)==0){
pNode3=pHead3;
}
}
} |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Oct 2004
Posts: 67
Rep Power: 4
![]() |
ok there is too much code to read it and still feel like a normal human being...
But i would suggest, for a line by line comparison to load line from file 1 into one char array and then load line from file 2 into char array 2 and then first do a quick length check. for the two... then if you will use a memcmp for the two buffers you'll have a quick code than to do char by char comparison... just remember that unixoid text files use char(10) for EOL not char(10) & char(13) as MSDOS based OS do. (please correct me if i'm wrong ... i always forget theese values and combinations) .... do a loop that breaks on char(10) or if you will on a ('.' || ';' || '!' || '?' and so on.) if it's a book style text. and then do the comparison.
__________________
coffee is my heroin. |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Oct 2004
Location: Canada
Posts: 82
Rep Power: 4
![]() |
There are 3 unique lines if you look real closely:
The Way that can be told of is not the eternal Way; The name that can be named is not the eternal name. The Named is the mother of all things. Here is some code that does the job. Note: I threw in a goto statement for Mici, who recommended them in another post. You can eliminate it with some state variables if you really wanted to. #include<stdio.h>
#define MAX 20
int match( const char [], const char []);
int main() {
char qPhrase[255];
char sPhrases[MAX][255];
int size;
int i;
FILE *queryFp = fopen("queryPhrases","r");
FILE *sourceFp = fopen("sourcePhrases","r");
FILE *uniqueFp = fopen ("uniquePhrases", "w");
//Get all source phrases
for (size = 0;
size <= MAX - 1 && fgets(sPhrases[size], 255, sourceFp) != NULL;
++size);
//Read a query phrase and compare to each source phrase.
while (fgets(qPhrase, 255, queryFp) != NULL) {
for (i=0; i < size; i++) {
if ( match(qPhrase,sPhrases[i]) ) {
goto BOTTOM;
}
}
fputs(qPhrase, uniqueFp); /*Write any unique phrase to file*/
BOTTOM:
}
fclose(sourceFp);
fclose(queryFp);
fclose(uniqueFp);
return 0;
}
int match (const char q[], const char s[] ){
int i=0;
while ( q[i] != '\0' && s[i] != '\0') {
if ( q[i] != s[i])
return 0;
++i;
}
return 1;
} |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Oct 2004
Posts: 67
Rep Power: 4
![]() |
Thanks for the goto...
Now everyone is able to see how they simplify life... Anyways nice code... One thing though (and please correct me if i'm wrong) ... wouldn't it be simpler if match() went like this: int match(const char q[], const char s[] )
{
if(strcmp(q,s)==0) return 1;
else return 0;
}
__________________
coffee is my heroin. |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Oct 2004
Location: Canada
Posts: 82
Rep Power: 4
![]() |
Yeah, that is better. I forgot about strcmp. I'm rusty with C, but I shouldn't have forgotten strcmp.
|
|
|
|
|
|
#6 |
|
Programmer
Join Date: Oct 2004
Posts: 67
Rep Power: 4
![]() |
Everybody forgets, it's simply a process of releasing system resources for future updates... I'm still running old firmware so I'm in touch with C, on the other hand part's of Qbasic now look like jiberish to me. Not to mention Fortran, Cobol or even Pascal.
To be honest i would be unable to write any kind of app without "Hello Worlds" In the end I don't think it's the knowledge of certain language what count's but the ability to learn and understand one. ![]()
__________________
coffee is my heroin. |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Oct 2004
Posts: 3
Rep Power: 0
![]() |
Thanks all of you... But I have to use "Double Linked List"" and I am not allowed to use array to store the data from the files... Or else it would have been very simple. Also, I am not allowed to use any of the unix system commands like diff etc...
Sorry for the trouble. But I am really stuck. I realize it is really long code. But I just dont see how else I can if I have to use double linked list |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|