Need help. What`s wrong?

Read words from textfile (only [a-zA_Z]. Puts them to the binary search
tree. If word already exists in tree has to add to counter in struct.

typedef struct puu_alkio
{
char sana[100];
int lukumaara;
struct puu_alkio *vasen, *oikea;
}puu_alkio, *puu_osoitin;


void add_to_tree(struct puu_alkio *solmu, struct puu_alkio *uusi);
void new_node(struct puu_alkio, FILE *);
int read();
void find(struct puu_alkio *, struct puu_alkio *)


int main(int argc, char *argv[])
{
int valinta, i, laskuri, n;
char merkkijono[30];
FILE *luku_tied, *kirj_tied_1,* kirj_tied_2;


read();

fclose(luku_tied);
}
return 0;
}

/* read from file */
int read( )
{
FILE *luku_tied;
int sana_lkm=0;
char sana[100];
struct puu_alkio;


luku_tied = fopen(argv[1], "r");
if(luku_tied == NULL)
{
perror("Tiedoston avaus ei onnistunut");
exit(1);
}
else
{

while (!feof(luku_tied));
{
sana_lkm=scanf(luku_tied, "%[^a-zA-Z]", sana);
if(sana_lkm)
new_node(struct puu_alkio *uusi, FILE *);
}
}
fclose(luku_tied);
}

int add_to_tree(puu_osoitin *juuri, char sana)
{

puu_osoitin t = *juuri; 
puu_alkio *uusi;

if( (uusi = (puu_osoitin)malloc(sizeof(puu_alkio))) == NULL )
{
perror("Muistinvaraus ei onnistu");
return -1;
}
uusi->sana = sana;
uusi->lukumaara = 1;
uusi->vasen = NULL;
uusi->oikea = NULL;

if(!t) { 
*juuri = uusi;
return 0;
}
else
find(struct puualkio *solmu, struct puualkio *uusi);
return 0;
}

void find(struct puu_alkio *solmu, struct puu_alkio *uusi)
{
struct puu_alkio *juuri, *uusi;

if (strcmp(solmu->sana, uusi->sana) > 0)
{
if (solmu->vasen == NULL)
solmu -> vasen = uusi;
else
add_to_tree(solmu->vasen, uusi);
}
else
{
if (solmu->oikea == NULL)
solmu -> oikea = uusi;
else
add_to_tree(solmu->oikea, uusi);
}
}

void new_node(struct puu_alkio *uusi, FILE *)
{
struct puualkio *juuri, *uusi;

if(uusi = (char* ) malloc(sizeof(struct)) == (char*)NULL)
{
perror ("muistinvaraus ei onnistu");
exit(1);
}
else
{
strcpy(uusi->sana, solmu->sana);
uusi->vasen = NULL;
uusi->oikea = NULL;

if (juuri == NULL)
juuri = uusi;
}
else
find(juuri, uusi);
}

Recommended Answers

All 4 Replies

ok

Need help. Whats wrong?

Read words from textfile (only [a-zA_Z]. Puts them to the binary search
tree. If word already exists in tree has to add to counter in struct

typedef struct puu_alkio
{
char sana[100];
int lukumaara;
struct puu_alkio *vasen, *oikea;
}puu_alkio, *puu_osoitin;
 
 
void add_to_tree(struct puu_alkio *solmu, struct puu_alkio *uusi);
void new_node(struct puu_alkio, FILE *);
int read();
void find(struct puu_alkio *, struct puu_alkio *)
 
 
int main(int argc, char *argv[])
{
                     int valinta, i, laskuri, n;
                     char merkkijono[30];
                     FILE *luku_tied, *kirj_tied_1,* kirj_tied_2;
 
 
                     read();
 
                     fclose(luku_tied);
}
   return 0;
}
 
/* read from file */
int read( )
{
                     FILE *luku_tied;
                     int sana_lkm=0;
                     char sana[100];
                     struct puu_alkio;
 
 
                     luku_tied = fopen(argv[1], "r");
                     if(luku_tied == NULL)
                       {
                         perror("Tiedoston avaus ei onnistunut");
                         exit(1);
                       }
                     else
                     {
 
                       while (!feof(luku_tied));
                         {
                         sana_lkm=scanf(luku_tied, "%[^a-zA-Z]", sana);
                         if(sana_lkm)
                         new_node(struct puu_alkio *uusi, FILE *);
                          }
                      }
   fclose(luku_tied);
}
 
int add_to_tree(puu_osoitin *juuri, char sana)
{
 
 puu_osoitin t = *juuri; 
 puu_alkio *uusi;
 
if( (uusi = (puu_osoitin)malloc(sizeof(puu_alkio))) == NULL )
  {
   perror("Muistinvaraus ei onnistu");
   return -1;
 }
 uusi->sana = sana;
 uusi->lukumaara = 1;
 uusi->vasen = NULL;
 uusi->oikea = NULL;
 
 if(!t) { 
   *juuri = uusi;
   return 0;
 }
 else
find(struct puualkio *solmu, struct puualkio *uusi);
 return 0;
}
 
void find(struct puu_alkio *solmu, struct puu_alkio *uusi)
{
struct puu_alkio *juuri, *uusi;
 
if (strcmp(solmu->sana, uusi->sana) > 0)
                     {
                     if (solmu->vasen == NULL)
                     solmu -> vasen = uusi;
                     else
                     add_to_tree(solmu->vasen, uusi);
                     }
                     else
                     {
                     if (solmu->oikea == NULL)
                     solmu -> oikea = uusi;
                     else
                     add_to_tree(solmu->oikea, uusi);
                     }
}
 
void new_node(struct puu_alkio *uusi, FILE *)
{
                     struct puualkio *juuri, *uusi;
 
                     if(uusi = (char* ) malloc(sizeof(struct)) == (char*)NULL)
                     {
    perror ("muistinvaraus ei onnistu");
    exit(1);
    }
    else
{
                     strcpy(uusi->sana, solmu->sana);
uusi->vasen = NULL;
                     uusi->oikea = NULL;
 
                     if (juuri == NULL)
                     juuri = uusi;
                     }
                     else
                     find(juuri, uusi);
}

Start with a smaller program, and compile more often would be my advice.
Writing 5 lines at a time, pressing "compile" and fixing the problems which you find is the way to learn how to do this yourself. If you get stuck, then we only have a few things to fix and you hopefully learn something new.
Writing 100+ lines and dumping it on a message board and hoping someone else will fix it all for you isn't.

There really are too many syntax errors which should have been picked out long ago.
For example, main has
- one extra }
- no parameters passed to read(), despite read() for example trying to access argv
- calling fclose() on a file handle which wasn't opened in main
- a whole bunch of unused variables.

> while (!feof(luku_tied));
Better watch that ;, this will either do nothing or loop forever.

> if(uusi = (char* ) malloc(sizeof(struct)) == (char*)NULL)
1. Don't cast the return result of malloc in C
2. Read up on operator predecence rules. As written, uusi gets the boolean result of ==
Your other malloc call got that bit right.

Oh, and you might want to work on the indentation a bit as well, some of it is really misleading as to what is going on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.