Binary Tree and File I/O

This is a discussion on Binary Tree and File I/O within the C Programming forums, part of the General Programming Boards category; Ok im printing a tree in ascending order, on the monitor it does it quite well. But what i wanted ...

  1. #1
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Ok im printing a tree in ascending order, on the monitor it does it quite well. But what i wanted to do was to print the same thing that pops on screen to a text file, it does print it but not in order. For instance if i enter, "jake,bert,zena" on screen its ok like "bert,jake,zena", but when written to a file it comes out as "bert,zena,jake"

    Can anyone point out my crappy logic, or help me out in fixing it? Thanks! There is the code:

    Code:
    void printtree_asc(BST t)
    {
        FILE *dataOutput;
        dataOutput = fopen("fileOutput.txt","a+"); 
        
        if (dataOutput == NULL) {
        printf("OooooPppppsss Out of Memory!");
        sleep(3);
        exit (-1);
        }
    
        if (t!=NULL) {
       	printtree_asc(t->left);
        printf("\n %d \t %s",t->info,t->fname);
        fputs(t->fname,dataOutput);
        fputs("\n",dataOutput);
        printtree_asc(t->right);
        
        }
       fclose(dataOutput);
    }
    Last edited by Matus; 12-01-2008 at 06:33 PM.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by Matus View Post
    Ok im printing a tree in ascending order, on the monitor it does it quite well. But what i wanted to do was to print the same thing that pops on screen to a text file, it does print it but not in order. For instance if i enter, "jake,bert,zena" on screen its ok like "bert,jake,zena", but when written to a file it comes out as "bert,zena,jake"

    Can anyone point out my crappy logic, or help me out in fixing it? Thanks! There is the code:

    Code:
     
    void printtree_asc(BST t)
    {
        FILE *dataOutput;
        dataOutput = fopen("fileOutput.txt","a+");
     
        if (dataOutput == NULL) {
        printf("OooooPppppsss Out of Memory!");
        sleep(3);
        exit (-1);
        }
     
        if (t!=NULL) {
           printtree_asc(t->left);
        printf("\n %d \t %s",t->info,t->fname);
        fputs(t->fname,dataOutput);
        fputs("\n",dataOutput);
        printtree_asc(t->right);
     
        }
       fclose(dataOutput);
    }
    Hmmmm I wouldn't open a file like that. You do realize you can only open a file handle so many times. Personally, I would just pass the FILE as a parameter.

  3. #3
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by master5001 View Post
    Hmmmm I wouldn't open a file like that. You do realize you can only open a file handle so many times. Personally, I would just pass the FILE as a parameter.
    Sorry could u explain Matt, do u mean the a+, i tried using only "w", but only jake would be printed nothing else.
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Example:
    Code:
    /* be sure to include assert.h for the assert() function */
    
    void printtree_asc(BST t, FILE *dataOutput)
    {
        assert(dataOutput);
     
        if (t!=NULL) {
           printtree_asc(t->left, dataOutput);
           printf("\n %d \t %s",t->info,t->fname);
           fputs(t->fname,dataOutput);
           fputs("\n",dataOutput);
           printtree_asc(t->right, dataOutput);
        }
    }

  5. #5
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by master5001 View Post
    Example:
    So in main is where id actually be doing the FILE I/O opening right, and then passing it to the function as one of its arguments?

    Am i understanding correct?
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Correctomundo. The problem with doing it how you had it before, is the file handles may not be opened, closed, nor flushed in the correct order. Which as far as I can see may be why your other stuff wasn't working right. Opening a file is expensive anyway.

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Matus wants to print a tree
    And got some advice from Master-ee
    The File Handle isn't a problem, the fclose() is done too
    We all make mistakes, I don't think that's the issue.

    Now, I wouldn't design a routine like this
    But it's a common mistake for a newbie novice
    Manage the handle in the main line code once
    And then you won't look like a dunce.

    C is a great language to learn
    But these common mistakes you'll soon discern
    In some months to come, you'll answer a post
    And rightfully so, it'll be your time to boast.

    Don't expect me to rhyme every time I reply
    Sometime's my mind's blank, I cannot comply
    But this evening I felt like I might have luck,
    Thank goodness for rhyming web sites, else I'd be stuck
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  8. #8
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by master5001 View Post
    Correctomundo. The problem with doing it how you had it before, is the file handles may not be opened, closed, nor flushed in the correct order. Which as far as I can see may be why your other stuff wasn't working right. Opening a file is expensive anyway.
    Alright Matt, yea that was the problem, think im comprehending what you were saying about the opening, got it thanks man. "Again"

    Owe u big time!
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  9. #9
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by Dino View Post
    Matus wants to print a tree
    And got some advice from Master-ee
    The File Handle isn't a problem, the fclose() is done too
    We all make mistakes, I don't think that's the issue.

    Now, I wouldn't design a routine like this
    But it's a common mistake for a newbie novice
    Manage the handle in the main line code once
    And then you won't look like a dunce.

    C is a great language to learn
    But these common mistakes you'll soon discern
    In some months to come, you'll answer a post
    And rightfully so, it'll be your time to boast.

    Don't expect me to rhyme every time I reply
    Sometime's my mind's blank, I cannot comply
    But this evening I felt like I might have luck,
    Thank goodness for rhyming web sites, else I'd be stuck
    Haha, another good one Dino. Yea thanks for the advice, hopefully im getting there. Another best quote of the month you gota say!
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You could do it your way, you just have to open your file at the inner most point of the recursion. But even then I do not recommend that since again, opening a file is expensive.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Here is the other way I speak of, just so you don't live life always kind of wondering.

    Example:
    Code:
    void printtree_asc(BST t)
    {
        FILE *dataOutput;
     
        if (t!=NULL) {
           printtree_asc(t->left);
    
           dataOutput = fopen("fileOutput.txt","a+");
     
           if (dataOutput == NULL) {
              printf("OooooPppppsss Out of Memory!");
              sleep(3);
              exit (-1);
           }
    
           printf("\n %d \t %s",t->info,t->fname);
           fputs(t->fname,dataOutput);
           fputs("\n",dataOutput);
           fclose(dataOutput);
    
           printtree_asc(t->right);
        }
    }

  12. #12
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by master5001 View Post
    You could do it your way, you just have to open your file at the inner most point of the recursion. But even then I do not recommend that since again, opening a file is expensive.
    Alright thanks, now I see. I'll take note of the File opening operations. Thanks. Owe u
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Nah its cool. Just use your knowledge wisely and we are even

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
        printf("OooooPppppsss Out of Memory!");
    isn't the right error message when you can't open a file, right?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by matsp View Post
    Code:
        printf("OooooPppppsss Out of Memory!");
    isn't the right error message when you can't open a file, right?

    --
    Mats
    Ya i know, i changed that part too, with the standard error stuff, as i'v quite actually seen on other posts before.
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. please help with binary tree, urgent.
    By slickestting in forum C Programming
    Replies: 2
    Last Post: 07-22-2007, 07:55 PM
  3. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 08:09 PM
  4. read records fron file into a binary tree
    By Kirsten in forum C Programming
    Replies: 1
    Last Post: 04-23-2002, 02:48 PM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM

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