Ανάγνωση συμβολοσειράς από ένα αρχείο κειμένου για να οικοδομήσουμε ένα δυαδικό δέντρο αναζήτησης

ψήφοι
-1

Εδώ είναι ο κωδικός μου μέχρι τώρα

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

#define lineSize 256

struct recordNode {
   char district[256];
   int employees;
   int employers;
   int students;
   int retried;
   int others;
   struct recordNode* left;
   struct recordNode* right;
};

struct stockNode* addRecord(struct recordNode* tree, struct recordNode node) {
   struct recordNode* newnode;
   struct recordNode* searcher;

   /* allocate memory block and assign parameter values to it */
   newnode = (struct recordNode*)malloc(sizeof(struct recordNode));

   newnode->left = NULL;
   newnode->right = NULL;
   /* check if the tree is empty, and in such case, return the newnode as*/
   /* the first node of the tree */
   if (tree == NULL)
     return newnode;
   /* searcher is the pointer to search for the correct location for insertion */
     searcher = tree;
   while (1) {
   /* see if the newnode should go to left branch */
      //if (code < searcher->code) {
      if (strcmp(tree->district, node.district) < 0) {
      /* yes, and if the left branch is empty, this is the insertion location */
         if (searcher->left == NULL) {
            searcher->left = newnode;
            return tree;
         }
      else { /* not yet, keep moving to the next level down */
         searcher = searcher->left;
         continue;
      }
   }
   /* see if the newnode should go to right branch */
      if (strcmp(tree->district, node.district) > 0) {
   /* yes, and if the right branch is empty, this is the insertion location */
         if (searcher->right == NULL) {
            searcher->right = newnode;
            return tree;
         }
      else { /* not yet, keep moving to the next level down */
        searcher = searcher->right;
        continue;
      }
   }
   else {
      free(newnode);
      return NULL; /* an error indication */
   }
   }
}

void getFile () {
   struct recordNode node;
   struct recordNode *tree;

   FILE* fpin;
   FILE* fpout;

   char line_buffer[lineSize]; /* BUFSIZ is defined if you include stdio.h */
   int counter = 0;

   //file validation
   fpin=fopen(testData.txt, r);

   if (fpin == NULL ) exit(0);
        counter = 0;
    while (fgets(line_buffer, sizeof(line_buffer), fpin)) { 
               counter++;
               if (counter != 1) {
               sscanf(line_buffer, %[^','],%d,%d,%d,%d, node.district, &node.employees, &node.students, &node.retried, &node.others);
               tree = addRecord(tree, node); **//ERROR**

               }

    }
        getchar();

}

void main() {

   getFile();
   getchar();

}

Η ακόλουθη γραμμή:

tree = addRecord(tree, node);

Δίνει αυτό το σφάλμα:

// ERROR έργου Project2.exe έθεσε εκτός τάξης EAccessViolation με το μήνυμα «Παραβίαση πρόσβασης στη διεύθυνση 32657E39. Διαβάστε διεύθυνσης 00000001' . Διαδικασία σταμάτησε. Χρησιμοποιήστε το Βήμα ή Εκτέλεση για να συνεχίσετε

Πώς μπορώ να διορθώσω αυτό το πρόβλημα;

Δημοσιεύθηκε 08/06/2011 στις 06:20
πηγή χρήστη
Σε άλλες γλώσσες...                            


1 απαντήσεις

ψήφοι
1

Φροντίστε να ορίσετε * δείκτη δέντρο για να NULL την πρώτη φορά που το δημιουργούν. Σε αντίθετη περίπτωση μπορεί να είναι οποιαδήποτε τιμή.

 void getFile () {
       struct recordNode node;
       struct recordNode *tree=NULL;
       ...
Απαντήθηκε 08/06/2011 στις 06:49
πηγή χρήστη

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more