-
File I/O Issue
howdy boys and girls:
ok i'm reading in a file for a binary tree where the left subtree of a node is in parentheses following the node key and the right subtree follows by a comma:
ie: 1(2,3(10),4(5(8),6(9),7)
would result in 1 at root 2 at left and 4 at right .. this is just the first branch..
my function pulls in this file using ifstream scanned into a char array, i'd use integer but got loads of errors...
Code:
char str[82];
ifstream b_file("./BinaryTree.dat");
b_file>>str;
cout<<str<<endl;
for(int i=0;i<strlen(str);i++)
{
if(str[i] == '(')
{i++; cout<<"Insert Left Subtree"<<endl;}
else if(str[i] == ',')
{i++; cout<<"Insert Right Subtree"<<endl;}
else if(str[i] == ' ')
{i++; cout<<"Ignoring Blank Space"<<endl;}
else{cout<<strcat(str[i--],str[++i])<<endl;}
cout<<str[i]<<endl;
}
b_file.close();
my problem i learned was handling two and then will be 3 digit numbers... i guess i should cat them together but how? or, how would i go about using an int array? then if so how should i attack my problem of sorting to various branches?
thanks for your time
-
You should probably use int's. It'll be easier -
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main ( void )
{
ifstream in("BinaryTree.dat");
if(!in)
{
cout << "Error opening file\n";
return 1;
}
int number;
in >> number;
cout << "root: " << number << '\n';
char child;
while(in >> child)
{
switch(child)
{
case '(':in >> number;
cout << "Insert Left Subtree: " <<
number <<'\n';break;
case ')':if(in >> child);else break;//read following ',' fall through
case ',':in >> number;
cout << "Insert Right Subtree: " << number <<
'\n';break;
default:cout << "Unknown char\n";break;
}
}
return 0;
}
-
thanks for the response i like that method a ton better but am confused by one thing:
Code:
case ')':if(in >> child);else break;//read following ',' fall through
i cant tell what if(in >> child); is testing for!
from what i can tell, this line doesn't do anything?? please can you explain a little clearer for me?
thanks
-
Sorry, it's probably a bit clearer if some whitespace is added -
case ')':
if(in >> child)
;
else break;
//read following ',' fall through
It's checking for EOF before falling through (as in your example every ')' was followed by a ',' except the final one ). in>>child will fail it EOF is reached so the break statement exits.
-
ah yes that is definately clearer. however as i looked back on it my example was flawed!
binarytree.dat should look like
1(2,3(10)),4(5(8),6(9),7)
after the 10 two parenthesis.. causing this output to reach only ten, then if i modify the binarytree.dat to loose the double parentheses after the ten, it will work, however i need them as it is the entire left branch of the tree!
i tried adding a continue on that confusing line and it will iterate to the end but looses about half the values and i get the "unknown char" message. i also added a break and got the same result, i am currently modifing it now and appreciate you quick and helpful responses.
thanks!
-
here is some more info
when making the line look like
Code:
case ')':if(in >> child) continue;else break;//read following ',' fall through
it will iterate through the file, but those unkown chars appear for 6 and 7, both of which follow commas. so i'm guessing once i continue i ruin the flow of the switch. tryin to think of a workaround to handle situations with '))' ..
-
Everytime you read one ')', the code currently reads the next character anyway to remove the ',' from the buffer (presuming EOF hasn't been reached). You could check this character before allowing the case statement to fall through and if it's not ',' extract another character.
-
thats what i'm trying right now!
thanks again
-
thanks Sorensen, the line now works and reads as follows
Code:
case ')':if(in >> child){while(child != ',') in >>child;}else break;
now i'm on my way to actually build the tree with the class i have.
thanks again