473,549 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help with reading a text file and storing the values into a tree

34 New Member
Hi guys,
I have this coding that I wanted to try out.
Basically this is meant to be done in Java as practice for the Topic trees in data structures and algorithms.
I have recently learned C++ on my own so I wanted to test my skill in C++ by tryin to solve this problem using C++.

so yea here it goes...
i have a text file as follows...
Expand|Select|Wrap|Line Numbers
  1. H,E,L
  2. E,B,F
  3. B,A,C
  4. A,null,null
  5. c,null,D
  6. D,null,null
  7. F,null,G
  8. G,null,null
  9. L,J,M
  10. J,I,K
  11. I,null,null
  12. K,null,null
  13. M,null,null
  14.  
i want to write the code that would read in the text file and store the 1st value gotten as a root, the 2nd value as the left value of the root and 3rd value as the right value of the root..
i have already given a shot at this but i aint that successful with it..

the following are the 2 other files... Tree.h and TreeNode.h
this one is TreeNode.h
Expand|Select|Wrap|Line Numbers
  1. #pragma once
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <deque>
  6. #include <queue>
  7. using namespace std;
  8.  
  9. template <class T>
  10. class TreeNode
  11. {
  12.     public:
  13.         TreeNode<T>(T newItem);
  14.         ~TreeNode<T>(void);
  15.         void setItem(T newItem);
  16.         void setLeft(TreeNode newLeft);
  17.         void setRight(TreeNode newRight);
  18.         T getItem();
  19.         T getLeft();
  20.         T getRight();
  21.  
  22.     private:
  23.         T item;
  24.         TreeNode left;
  25.         TreeNode right;
  26. };
  27.  
  28.  
  29. template <class T>
  30. TreeNode<T>::TreeNode(T newItem)
  31. {
  32.     item = newItem;
  33.     left = null;
  34.     right = null;
  35. }
  36.  
  37. template <class T>
  38. TreeNode<T>::~TreeNode(void)
  39. {
  40. }
  41.  
  42.  
  43. template <class T>
  44. void TreeNode<T>::setItem(T newItem) 
  45. {
  46.     // set methods
  47.     item = newItem;
  48. }
  49.  
  50. template <class T>
  51. void TreeNode<T>::setLeft(TreeNode newLeft) 
  52. {
  53.     left = newLeft;
  54. }
  55.  
  56. template <class T>
  57. void TreeNode<T>::setRight(TreeNode newRight) 
  58. {
  59.     right = newRight;
  60. }
  61.  
  62. template <class T>
  63. T TreeNode<T>::getItem() 
  64. {
  65.     // get methods
  66.     return item;
  67. }
  68.  
  69. template <class T>
  70. T TreeNode<T>::getLeft() 
  71. {
  72.     return left;
  73. }
  74.  
  75. template <class T>
  76. T TreeNode<T>::getRight() 
  77. {
  78.     return right;
  79. }
  80.  
this one is Tree.h
Expand|Select|Wrap|Line Numbers
  1. #pragma once
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <deque>
  6. #include <queue>
  7. using namespace std;
  8.  
  9. template <class T>
  10. class Tree
  11. {
  12.     public:
  13.         Tree<T>(T rootItem);
  14.         ~Tree<T>(void);
  15.         bool isEmpty();
  16.         T getRoot();
  17.  
  18.     private:
  19.         TreeNode root;
  20. };
  21.  
  22.  
  23. template <class T>
  24. Tree<T>::Tree(T rootItem)
  25. {
  26.     root = new TreeNode(rootItem);
  27. }
  28.  
  29. template <class T>
  30. Tree<T>::~Tree(void)
  31. {
  32. }
  33.  
  34.  
  35. template <class T>
  36. bool Tree<T>::isEmpty() 
  37. {
  38.     // check emtpy
  39.     return root == null;
  40. }
  41.  
  42. template <class T>
  43. T Tree<T>::getRoot() 
  44. {
  45.     // get tree root
  46.     return root;
  47. }
  48.  

the following code is from my main method to try and read the file... but it isnt correct..
Expand|Select|Wrap|Line Numbers
  1. typedef queue<TreeNode> TreeLinkedList;
  2. TreeLinkedList myQueue;
  3.  
  4. template <class T>
  5. T readOneObject(string string1)
  6. {
  7.     char * cstr, *p;
  8.     int counter;
  9.     string str (string1);
  10.     string data1, data2, data3;
  11.  
  12.     cstr = new char [str.size()+1];
  13.     strcpy (cstr, str.c_str());
  14.  
  15.     // cstr now contains a c-string copy of str
  16.  
  17.     int count = 0;
  18.     p=strtok (cstr,",");
  19.     count++;
  20.     while (p!=NULL)
  21.     {
  22.         p=strtok(NULL,",");
  23.         if( count == 1 )
  24.         {
  25.             data1.append(p);
  26.             root = data1;
  27.         }
  28.         else if( count == 2 )
  29.         {
  30.             data2.append(p);
  31.         }
  32.         else if( count == 3 )
  33.         {
  34.             data3.append(p);
  35.         }
  36.         count++;
  37.         if( count == 3 )
  38.             break;
  39.     }
  40.  
  41.     delete[] cstr; 
  42.     return treeObj;
  43. }
  44.  
  45. void readFile(string filename1)
  46. {
  47.     char oneline[256];
  48.     ifstream infile(filename1.c_str());
  49.     Tree * treeObj = NULL;
  50.  
  51.     while(infile.good())
  52.     {
  53.         infile.getline(oneline, 256);
  54.  
  55.         if(strlen(oneline) == 0)
  56.         {
  57.             continue;
  58.         }
  59.         treeObj = readOneObject(oneline);
  60.  
  61.         //if( treeObj != NULL )
  62.             //myQueue.push(treeObj); //linkedListObj.InsertNode(shapeObj);
  63.     }
  64.     infile.close();    
  65. }
  66.  
every help is appreciated.. please help me to solve this...
thanks in advance...:D
i will check for replies every 30 mins or lesser.. so plz come back and check if u had left any comments or qns for me to answer.. thanks :D
Sep 2 '08 #1
8 2606
arnaudk
424 Contributor
I didn't read your code, but can you be more specific about what problems you are having?
Sep 2 '08 #2
JosAH
11,448 Recognized Expert MVP
You 'left' and 'right' items in your TreeNode class should be pointers (or references)
to a TreeNode; your Java background is shining through ;-) You're also missing
some <T> template tags here and there ...

kind regards,

Jos
Sep 2 '08 #3
slizorn
34 New Member
I didn't read your code, but can you be more specific about what problems you are having?
well basically i want to create a method for reading in my text file and saving it to the respective right, left, root of the tree node

problems faced are that my method is wrong so it aint working.. it/the method for readin in a file isnt correct...
Sep 2 '08 #4
slizorn
34 New Member
You 'left' and 'right' items in your TreeNode class should be pointers (or references)
to a TreeNode; your Java background is shining through ;-) You're also missing
some <T> template tags here and there ...

kind regards,

Jos

haha yea i did do a full course in Java in my 1st yr in uni.. goin to enter 2nd yr this sept.. was hopin to get some practice for a small headstart.. ;)
well gimme a few mins i will edit my code again and post it..
could ya tell me how i shd go abt implementing/writing the code to read in my text file? its definitely wrong but sort of in the right track..
Sep 2 '08 #5
slizorn
34 New Member
You 'left' and 'right' items in your TreeNode class should be pointers (or references)
to a TreeNode; your Java background is shining through ;-) You're also missing
some <T> template tags here and there ...

kind regards,

Jos
by the way
u see anything wrong with the 1st line?
typedef queue<TreeNode> TreeLinkedList;
TreeLinkedList myQueue;

error C3203: 'TreeNode' : unspecialized class template can't be used as a template argument for template parameter '_Ty', expected a real type
Sep 2 '08 #6
JosAH
11,448 Recognized Expert MVP
by the way
u see anything wrong with the 1st line?
typedef queue<TreeNode> TreeLinkedList;
TreeLinkedList myQueue;

error C3203: 'TreeNode' : unspecialized class template can't be used as a template argument for template parameter '_Ty', expected a real type
Is the type TreeNode known at that location? You do have to #include it before
you want to use it; C++ is definitely not Java.

kind regards,

Jos
Sep 2 '08 #7
Laharl
849 Recognized Expert Contributor
You've also declared TreeNode as a template class, thus, your declaration should be
Expand|Select|Wrap|Line Numbers
  1. typedef queue<TreeNode<some type)> > TreeLinkedList;
. The space is necessary, otherwise C++ interprets it as the >> operator and you'll get errors.
Sep 2 '08 #8
weaknessforcats
9,208 Recognized Expert Moderator Expert
I assume this is for a course you are taking otherwise you should be using a map.
Sep 2 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
1872
by: aa | last post by:
How do I read a value written in the first tab of the last line of a text file where values are separated by "\t"?
2
1610
by: Kaustubh | last post by:
Well i have txt file with abt 1000 Sentence i want that each sentence appear on my website each time diff sentence on each visit on website ! i have done this <% set Testfile=Server.CreateObject("Scripting.FileSystemObject") set TfileStream=Testfile.OpenTextFile("C:\Inetpub\wwwroot\Quates.txt") TextFormat=TfileStream.ReadLine %> <p...
1
7038
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the file... Thank you to all of you who can help me with this one...
8
18213
by: Phil Slater | last post by:
I'm trying to process a collection of text files, reading word by word. The program run hangs whenever it encounters a word with an accented letter (like rôle or passé) - ie something that's not a "char" with an ASCII code in 0..127 I've searched the ANSI C++ standard, the internet and various text books, but can't see how to workaround...
31
2169
by: J.S. | last post by:
Let's say I have a text file with parameters like the following embedded in the text: @@Textbox1@@, @@Textbox2@@, etc. Is it possible to replace the parameters in the text file with values entered into textboxes in the Windows Forms UI? Thanks, J.S.
7
7746
by: Drew Berkemeyer | last post by:
Hello, I'm using the following code to read a text file in VB.NET. Dim sr As StreamReader = File.OpenText(strFilePath) Dim input As String = sr.ReadLine() While Not input Is Nothing strReturn += input + vbCrLf input = sr.Read
4
12776
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0 to read text file but could not get the data in correct format. All columns are not coming in dataset and rows are messing up. Suggestions...
3
3160
by: jasvinder singh | last post by:
Respected Sir/madam, Can you help in providing code in 'C' for Reading text file with n number of rows and columns and putting the result in arrays.The sample file is as follows: rim_label = 'aeff5' state 0 = 'eee' state 1 = 'ffffff' . .and so on...
9
1866
by: Amkcoder | last post by:
I am trying to read a file a character at a time, I want to read in the new line character. I am printing out each character to the screen. For some reason it will not print out the new line. I want to do this so the line will break on the console while reading it in a character at a time. I am using fgetc on a file opened for reading in...
1
1282
by: Hamayun Khan | last post by:
Hi All I have text files having queries like below. INSERT INTO tblJobScrap (,,,,,,,,,) VALUES ('http://www.aberdeencity.gov.uk/webapps/images/genericicons/acc-logo.gif','Aberdeen','Scotland','School Crossing Patroller','No fixed closing date','£6.0100 p.h.','£6.37-£6.65...
0
7527
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7459
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7726
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
6052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5377
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5097
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3488
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.