NO

Author Topic: Need help reading a list of data and sorting using a binary tree.  (Read 4587 times)

NoobJoshua

  • Guest
Yes this is for an intro programming class so I'm not asking anyone to solve the problem for me but simply point me in the right direction. I am on the verge of pulling my hair out!  :-[  If it makes anyone feel better I do not plan on taking another programming class nor am I to be a programmer  ;)

So the problem asks to read data from a file and store them in a binary tree to be sorted. It is an index of Shakespeare plays and relevant information.

To read the data and store each field separately I have done this

Code: [Select]
union input{
   char lineIn[47];
   struct pData{
      char date1[9];
      char date2[5];
      char title[26];
      char type[7];
   }playdata;
}use;

and later on in the main function I do this to get each line individually before storing them in their own node:

Code: [Select]
for(i=0; i <47; i++){
            use.lineIn[i] = fgetc(fp);
        }
My problem is that the data doesn't seem to be being read correctly. When I try to test it by printing a desired field(type,title, etc.) it starts out ok but then begins to go nuts.
« Last Edit: April 10, 2014, 12:44:23 AM by NoobJoshua »

CommonTater

  • Guest
Re: Need help reading a list of data and sorting using a binary tree.
« Reply #1 on: April 10, 2014, 02:02:16 AM »
Code: [Select]
union input{
   char lineIn[47];
   struct pData{
      char date1[9];
      char date2[5];
      char title[26];
      char type[7];
   }playdata;
}use;
For starts you need a struct not a union.  The difference is that in a struct each variable is laid out in order in it's own memory space but in a union they all pile on top of each other.  Both have their uses... but in this case a struct is called for.
 
To build a linked list you need one more variable... a pointer to the next struct in the list.  This lets you find your way along so you can access the list sequentially.
 
Sorting then becomes easy... just change the pointers.
 
Quote
and later on in the main function I do this to get each line individually before storing them in their own node:

Code: [Select]
for(i=0; i <47; i++){
            use.lineIn[i] = fgetc(fp);
        }
My problem is that the data doesn't seem to be being read correctly. When I try to test it by printing a desired field(type,title, etc.) it starts out ok but then begins to go nuts.

fgetc only records one character...
http://pubs.opengroup.org/onlinepubs/009695299/functions/fgetc.html
 
This might help... if not search for "Linked List Tutorial"...
http://eternallyconfuzzled.com/tuts/datastructures/jsw_tut_linklist.aspx
 
« Last Edit: April 10, 2014, 02:10:47 AM by CommonTater »

NoobJoshua

  • Guest
Re: Need help reading a list of data and sorting using a binary tree.
« Reply #2 on: April 10, 2014, 02:13:51 AM »
That union is only used to gather the data. There is a separate. structure for the actual node in the link.
« Last Edit: April 10, 2014, 02:15:28 AM by NoobJoshua »

CommonTater

  • Guest
Re: Need help reading a list of data and sorting using a binary tree.
« Reply #3 on: April 10, 2014, 02:44:49 AM »
That union is only used to gather the data. There is a separate. structure for the actual node in the link.

With a more conventional mechanism you could read from text files (assuming it is text) directly into the node struct... probably with a lot fewer complications.
 
Look at fscanf() in the Pelles Help file... Once you know the order of the data you just call it in a loop, for each variable in the struct.  Life gets easier... add a new node, fill it from fscanf()... repeat until done.
 
eg.  fscanf(MyFile, " %s", list->name);
 
 

NoobJoshua

  • Guest
Re: Need help reading a list of data and sorting using a binary tree.
« Reply #4 on: April 10, 2014, 03:04:33 AM »
Not quite sure what you mean. Say if I have 4 values in my structure, all type char strings which form one line from the data text file. How would fscanf be used to capture each value from a line?

There are not white spaces between every value.
« Last Edit: April 10, 2014, 03:09:09 AM by NoobJoshua »

CommonTater

  • Guest
Re: Need help reading a list of data and sorting using a binary tree.
« Reply #5 on: April 10, 2014, 04:01:27 AM »
Not quite sure what you mean. Say if I have 4 values in my structure, all type char strings which form one line from the data text file. How would fscanf be used to capture each value from a line?

There are not white spaces between every value.

That depends if there are delimiters in the lines or not...
 
e.g. "The quick, brown fox jumped, over the lazy dog, in the field"
 
could be read by...
 
fscanf(file, " %s,  %s,  %s,  %s", data->str1, data->str2, data->str3, data->str4);
 
When there is no delimiter you can tell it how many characters to read...
 
e.g. fscanf(file, "%12s" ....
 
You need to actualy look this up in the help file... the info is all there.
 
Simply type fscanf  into the editor, press F1 ... start reading. Pelles help file is probably the best C language reference there is.
 
 
 
 
« Last Edit: April 10, 2014, 04:03:55 AM by CommonTater »