File I/O Issue

This is a discussion on File I/O Issue within the C++ Programming forums, part of the General Programming Boards category; howdy boys and girls: ok i'm reading in a file for a binary tree where the left subtree of a ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    63

    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
    SS3X

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    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;
    
    }

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    63
    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
    SS3X

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    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.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    63
    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!
    SS3X

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    63
    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 '))' ..
    SS3X

  7. #7
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    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.

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    63
    thats what i'm trying right now!

    thanks again
    SS3X

  9. #9
    Registered User
    Join Date
    Jan 2002
    Posts
    63
    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
    SS3X

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21