Code modification and errors
I have the following code and It needs some modification sin it to compile.
The cmpwrd.c and worder.c are the files and worder.c compiles and works fine when compeiled individually. but when i do this
gcc cmpwrd.c worder.c
I am getting an error that multiple mains. so I need to remove one from worder.c.
I have to turn off the TEST_SCAFFOLD which is running forcibly.
Can any one please post me these modification.
Any help is appreciated.
Quote:
Code:
cmpwords.c
#include "worder.h"
#include <stdlib.h>
#include <math.h>
struct gabble {
char* wd;
int count;
int touched;
};
struct blurry {
char* fn;
struct gabble wds[40000];
int firstFree;
int totalCount;
} a,b;
void rdfile(char* fn, struct blurry* b){
char* wp;
int i;
b->fn = fn;
b->firstFree = 0;
if (init_file(fn)){fprintf(stderr,"\n?giving up\n"); exit(1);}
while (nextC != EOF){
wp = nextword();
b->totalCount++;
for (i=0; i<b->firstFree; i++){
if (strcmp(wp,b->wds[i].wd)==0){
b->wds[i].count++;
break;
}
}
if (i == b->firstFree){
b->firstFree++;
b->wds[i].wd = wp;
b->wds[i].count = 1;
b->wds[i].touched = 0;
}
}
}
void cmpdist(struct blurry* a, struct blurry* b){
int i,j;
double sum = 0;
for (i =0; i<a->firstFree; i++){
double x = a->wds[i].count/(double)a->totalCount;
for (j=0; j<b->firstFree; j++){
if (strcmp(a->wds[i].wd,b->wds[j].wd)==0){
double y = b->wds[j].count/(double)b->totalCount;
sum += (x-y)*(x-y);
a->wds[i].touched = b->wds[i].touched = 1;
}
}
if (a->wds[i].touched ==0) {sum += x*x; }
}
for (j=0; j<b->firstFree; j++){
if (b->wds[j].touched ==0) {
double y = b->wds[j].count/(double)b->totalCount;
sum += y*y;
}
}
printf("distance = %g\n", sqrt(sum));
}
int main(int argc, char** argv){
if (argc != 3){
fprintf(stderr, "Usage: cmpwrds file1 file2\nCompare distances between word distributions in files.\n");
return (1);
}
rdfile(argv[1], &a);
rdfile(argv[2], &b);
cmpdist(&a,&b);
}
Worder.c
/*
* A function nextword() is defined reading an English word. Used here
* for reading an English text from standard input and writing
* the individual words on separate lines on standard output.
* The main program acts as a test scaffold for the functions, which could
* be used alone (if the main program were replaced.) Compiling
* this file with -DTEST_SCAFFOLD should do just that.
*/
#ifndef TEST_SCAFFOLD
#define TEST_SCAFFOLD 1
#endif
#define EXTERN
#include "worder.h"
FILE* finput; /* file input pointer */
#if TEST_SCAFFOLD
char* nextword(); /* forward declaration */
main(){
init_file("CON");
while (nextC != EOF) {
char* t = nextword();
puts(t);
/* putchar('\n'); puts adds new line*/
}
}
#endif /* TEST_SCAFFOLD */
/*
* The nextword() function returns the next English word or punctuation
* from standard input. It doesn't handle any abbreviations.
*/
char* nextword(){
char* retval;
/* set up pointer to buffer to accumulate word */
char* p = retval = currentPointer;
/* skip white space */
while (nextC == ' ' || nextC == '\n' || nextC == '\r' || nextC == '\t'){
nextC = fgetc(finput);
}
/* check for EOF */
if (nextC == EOF) { return "*ENDOFFILE*"; }
/* accumulate letters until punctuation */
while (nextC != EOF && nextC != ' ' && nextC != '\t' && nextC != ','
&& nextC != ';' && nextC != ':' && nextC != '.' && nextC != '?'
&& nextC != '\n' && nextC != '!' && nextC != '"'){
*p++ = nextC;
nextC = fgetc(finput);
}
/* check for a blank word */
if (p == retval){
/* first char of word was punctuation */
*p++ = nextC;
nextC = fgetc(finput);
}
/* add trailing null character for C strings */
*p = 0;
currentPointer = p+1;
/* return pointer to buffer */
return retval;
} /* nextword() */
int init_file(char* fn){
finput = fopen(fn, "r");
if (finput == 0){
fprintf(stderr, "could not open file %s\n", fn);
return (1);
}
nextC= fgetc(finput); /* initialize global */
return 0;
}
Worder.h
/*
* A function nextword() is defined reading an English word. Used here
* for reading an English text from standard input and writing
* the individual words on separate lines on standard output.
*/
#include <stdio.h>
#ifndef EXTERN
#define EXTERN extern
EXTERN char wordspace[100000000]; /* 100 MB buffer used to build words in. */
EXTERN char* currentPointer;
#else
EXTERN char wordspace[100000000]; /* 100 MB buffer used to build words in. */
EXTERN char* currentPointer = wordspace; /* buffer so far */
#endif
/* most values passed / maintained in globals */
EXTERN char nextC; /* next character. Global, so that delimiter of word can be start of next word */
/*
* The nextword() function returns the next English word or punctuation
* from standard input. It doesn't handle any abbreviations.
*/
char* nextword();
/*
* the init_file() function sets up the input file and the nextC variable.
*/
int init_file(char* fn);
Regards,
G.