hkvega01
hkvega01

Reputation: 37

split the text from file

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

FILE *getFile (char filename[]) {
   struct recordNode* node;

   FILE* fpin;
   FILE* fpout;

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

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

   if (fpin == NULL ) exit(0);
        counter = 0;
    while (fgets(line_buffer, sizeof(line_buffer), fpin)) { /* same as while (feof(in) != 0) */
               counter++;
               if (counter != 0) {
                //Central & Western - Chung Wan,7576,1042,2156,1875,3154 (sample data)
               sscanf(line_buffer, "%s,%d,%d,%d,%d", node->district, node->employees, node->students, node->retried, node->others);
               printf("%s", node->district); **//error**
               }


               getchar();



    }
        getchar();

   return fpout;
}

i got a error when i compile, what's wrong with my code? There is a error message Project Project1.exe raised exception class EAccessViolation with Message .................... (borland C++)

Upvotes: 0

Views: 194

Answers (1)

user2100815
user2100815

Reputation:

Your problem (or at least one of them) is here:

sscanf(line_buffer, "%s,%d,%d,%d,%d", node->district, node->employees, 
                      node->students, node->retried, node->others);

all except the first of those parameters should be pointers:

sscanf(line_buffer, "%s,%d,%d,%d,%d", node->district, & node->employees, 
                      & node->students, & node->retried, & node->others);

Also, this:

filename = "testData.txt";

shouldn't be there at all - the name of the file to read is passed in as a parameter to the function.

And lastly, this:

 struct recordNode* node;

should be:

 struct recordNode node;

and you need to change things like node->district to node.district. This is tiring work - perhaps you could put a bit more effort into chasing these things down before posting here. You will learn much more that way.

Upvotes: 3

Related Questions