Ok, I feel like such an idiot with C++...gimme C# or Java any day of the week.
I have a tree program that (works) creates a tree, inorder traverses it, and then prints out messages before deleting the tree (and printing out more messages). That all works.
What doesn't work is the interface from the file to the tree. Uh, let me explain that. When I started this program, I just asked the user to input the tree from the console, easy peasy, right? Well, I actually need to take the data from a file that looks like this...
{ ROOT } //this is one tree
{ ROOT { FRED } } //this is one tree
{ ROOT { FRED { FRED_JUNIOR1 } { FRED_JUNIOR2} } }//this is one tree
{ ROOT
{ FRED
{ FRED_JUNIOR1}
{ FRED_JUNIOR2}
}
{ BARNEY
{ BARNEY_JUNIOR1}
{ BARNEY_JUNIOR2}
}
}// and this is the final tree
Since C++ is uhhh primative, I can't just use a file delimiter and a string tokenizer to strip out the whitespace and add to the tree properly.
My question is simple, how do I take a line and add just the word to the tree while using the open and closed brackets to indicate whether or not it is a new tree?
Here is my code... 3 4313 Ganon11 3,652
Recognized Expert Specialist
{ ROOT } //this is one tree
{ ROOT { FRED } } //this is one tree
{ ROOT { FRED { FRED_JUNIOR1 } { FRED_JUNIOR2} } }//this is one tree
{ ROOT
{ FRED
{ FRED_JUNIOR1}
{ FRED_JUNIOR2}
}
{ BARNEY
{ BARNEY_JUNIOR1}
{ BARNEY_JUNIOR2}
}
}// and this is the final tree
When inputting, you can keep a running count of how many open brackets you have by reading character by character. If you find an open bracket '{', add one - if you find a closed bracket '}', subtract one. Every time your total hits 0, you will have finished one tree.
Ok, so I tried to do what you said... - while (! textFile.eof())
-
{
-
getline (textFile, line);
-
if (line.find("{"))
-
{
-
oCount ++;
-
//cout << oCount << endl;
-
-
if (line.find("}"))
-
{
-
oCount ++;
-
//cout << oCount << endl;
-
}
-
-
//add the new string to the tree
-
strcpy(string, line.c_str());
-
add_to_tree(string);
And this is what I get:
HELLO{ ROOT }
HELLO{ ROOT { FRED } }
HELLO{ ROOT { FRED { FRED_JUNIOR1 } { FRED_JUNIOR2} } }
HELLO{ ROOT
HELLO{ FRED
HELLO { FRED_JUNIOR1}
HELLO { BARNEY_JUNIOR1}
HELLO
HELLO { BARNEY_JUNIOR2}
HELLO { FRED_JUNIOR2}
HELLO{ BARNEY
HELLO}
HELLO}
HELLO}
GOODBYE}
GOODBYE}
GOODBYE}
GOODBYE{ BARNEY
GOODBYE { FRED_JUNIOR2}
GOODBYE { BARNEY_JUNIOR2}
GOODBYE
GOODBYE { BARNEY_JUNIOR1}
GOODBYE { FRED_JUNIOR1}
GOODBYE{ FRED
GOODBYE{ ROOT
GOODBYE{ ROOT { FRED { FRED_JUNIOR1 } { FRED_JUNIOR2} } }
GOODBYE{ ROOT { FRED } }
GOODBYE{ ROOT }
which is not right at all....
Please help!
further along...no closer to the end...
code now - if (textFile.is_open())
-
{
-
while (! textFile.eof())
-
{
-
getline (textFile, line, ' ');
-
-
-
target = line.find("{",0);///found open brace
-
if(target != string::npos)
-
{
-
oCount ++;
-
line.erase(target, 1);
-
}
-
target1 = line.find("\t",0); //found tab
-
if(target1 != string::npos)
-
{
-
-
line.erase(target1, 1);
-
}
-
-
target3 = line.find(" ",0); //found tab
-
if(target1 != string::npos)
-
-
{
-
line.erase(target1, 1);
-
}
-
target2 = line.find("}",0);//found closed
-
if(target2 != string::npos)
-
{
-
-
oCount --;
-
line.erase(target2, 1); }
-
//add the new string to the tree
-
if (line.length() > 3)
-
{
-
strcpy(string, line.c_str());
-
add_to_tree(string);
-
}
So now this is the output
HELLO ROOT
HELLO FRED
HELLO BARNEY
HELLO BARNEY_JUNIOR1
HELLO BARNEY_JUNIOR2
}
}
HELLO FRED
HELLO FRED_JUNIOR1
HELLO FRED
HELLO FRED_JUNIOR2
HELLO FRED_JUNIOR1
HELLO FRED_JUNIOR2
}
HELLO ROOT
HELLO ROOT
HELLO ROOT
GOODBYE ROOT
GOODBYE ROOT
GOODBYE ROOT
GOODBYE FRED_JUNIOR2
}
GOODBYE FRED_JUNIOR1
GOODBYE FRED_JUNIOR2
GOODBYE FRED
GOODBYE FRED_JUNIOR1
GOODBYE FRED
GOODBYE BARNEY_JUNIO
}
}
GOODBYE BARNEY_JUNIO
GOODBYE BARNEY
GOODBYE FRED
GOODBYE ROOT
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Kylotan |
last post by:
I have a text file where the fields are delimited in various different
ways. For example, strings are terminated with a tilde, numbers are
terminated with whitespace, and some identifiers are terminated with a
newline. This means I can't effectively use split() except on a small
scale. For most of the file I can just call one of several functions I
wrote that read in just as much data as is required from the input
string, and return the...
|
by: spike |
last post by:
Im writing a program to search for a string in a binary file.
And it works. The problem is: It is sooo slow! how can i make it faster?
It takes 27 seconds just to search a 5 meg file.
I guess it has something to do with the strequal() function...
Btw, thanks to all of you who answered last time!
code:
-------------------------------------------------------------------------
#include <stdio.h>
|
by: Profetas |
last post by:
I have the following code that detects a
<c>
and
</c>
#include <stdio.h>
main(int argc, char *argv)
{
FILE* fp;
char data;
|
by: Brian Henry |
last post by:
first question... I have a flat file which unfortinuatly has columns
seperated by nulls instead of spaces (a higher up company created it this
way for us) is there anyway to do a readline with this and not have it
affected by the null? because it is right now causes truncated data at wierd
places... but as soon as i manually with a hex editor change char(00) to
char(20) in the files it reads prerfectly... which leads me to my 2nd...
|
by: j7.henry |
last post by:
I am trying to pull specific data that is in a comma delimited file
into a web page. So if my comma delimited file looks like:
Name,Address,Zip
Fred,123 Elm,66666
Mike,23 Jump,11111
I would like to be able to read this data and put each row into a
variable? so I could display the values in a web page where I want.
| |
by: Jerry John |
last post by:
I am working in ASP.NET with C#. I have a text file which contains datas
with delimiters. For example:-
MSH|^~\$|DISCHARGE|CLAY COUNTY MEMORIAL|||200502110939|
I also have an XML file created with predefined tags. Some of the tags
contain child element. I need to pass the data from the text file i.e the
value within the delimiters should be passed to the corresponding tags within
the XML file. I have done this through hard code. But i...
|
by: Timofmars |
last post by:
I'm try to Unload DB2 data from a table into a record sequential file
on NT. I can an unload on Mainframe, but it doesn't seem to be an
option in NT.
In NT, all I can do is export/import. I can do a Load too, but not an
Unload. I just want the only the data from the table in a record
sequential file. Export seems only to give options to have a delimited
line sequential file or a record sequential file where the data is
preceeded by...
|
by: Bill |
last post by:
Hello,
I'm trying to output buffer content to a file. I either get an access
violation error, or crazy looking output in the file depending on
which method I use to write the file. Can anyone help out a newbie?
#include <stdio.h>
#include <ctype.h>
#include <string.h>
|
by: Killer42 |
last post by:
The Input #1 statement simply reads in one line from a text file (in this case you INI file) and places the values from it into one or more variables. So what you are reading in this statement is "#Please do not delete this file". My guess is that would all be placed in the first variable, a$.
The information held in your file is much more complex than can be handled by a single Input # statement. Probably your best bet is to keep looping and...
|
by: gopiganguly |
last post by:
Hi everyone,
There is a small problem encountered while creating a package in sql
server 2005.
Actually i am using a flat file which has 820 rows and 2 columns which
are seperated by line feed(for ROW) and tab(for COLUMN).after
importing i found that ther are only 800 rows imported into the table.
Ather verifying the input file i found out that there are some null
values in the second column so there is no line feed for those ...
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |