I/O - Multiple Files

This is a discussion on I/O - Multiple Files within the C Programming forums, part of the General Programming Boards category; Ok, the goal is to make a Binary Search Tree program, which I have done, but one of the stipulations ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    33

    I/O - Multiple Files

    Ok, the goal is to make a Binary Search Tree program, which I have done, but one of the stipulations is that ALL I/O must be handled in a 3rd file named io.c

    So that makes

    main.c
    bst.c - Represents tree
    bst.h - Header
    io.c - Handles input/output
    io.h - Header

    I have main and bst working, and If I put the print function in bst.c, it works just as it should.

    My question is, what do I need to include in my io.c file

    Also, my io.h file may be missing something too.

    Just a heads up before I post the code. Yes, there will be more functions in these files, this is simply a test.

    io.c:
    Code:
    #include <stdio.h>
    #include "bst.h"
    #include "io.h"
    
    void inorder(Tree T){
       if(T!=NULL){
    
            inorder(T->Left);
            printf("&#37;d ",T->data);
            inorder(T->Right);
       }
    }
    io.h:
    Code:
    #ifndef IO_H
    #define IO_H
    
    void inorder(Tree T);
    
    #endif
    Like I said, if that function is in my bst.c file, the program works fine.

    If I try to compile it in io.c however, all three lines give the following errors in DevC++:

    dereferencing pointer to incomplete type

    Hope that makes sense, thanks in advance.

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    How and where do you define the Tree type?

    In your 'inorder' function you treat the variable T as if it were a pointer (using the -> operator), but according to the parameter list it should be of type Tree (which typically wouldn't be a pointer unless you typedef'd it to be)

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    33
    Code:
    struct Node {
           int data;
           Tree  Left;
           Tree  Right;
           int Height;
           };
    And typedef'd it

    typedef struct Node *Tree;

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    Ok i'm confused now, that works totally fine for me.

    Are you making sure to do the typedef BEFORE you define the struct?

    Code:
    typedef struct Node *Tree;
    struct Node {
       int data;
       Tree  Left;
       Tree  Right;
       int Height;
    };
    ?

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    33
    Ha, I am an idiot, that was the problem. I had it in 2 places and deleted the wrong one.

    Thanks

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    Its ok, I'm an idiot too.

    You see, to me, there are 2 kinds of people: idiots, and despicable idiots. The only difference between them is that an idiot knows they're an idiot, the latter actually believe they're smart.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    33
    Found another problem, though not related to files, might as well not make another thread.

    This function:

    Code:
    void inputloop(Tree T){
         int x=1;
         int num;
         char choice;
         while(x==1){
              printf("Enter (i)nsert, (s)earch, inorder (t)raversal, or (q)uit: ");
              choice=getchar();
              switch(choice){
              case 'i':
                   printf("Enter a number to insert: ");
                   scanf("%d",&num);
                   T = Insert(num, T);
                   break;
    
              case 's':
                   printf("Enter a number to search for: ");
                   scanf("%d",&num);
                   if(Find(num, T)==NULL) printf("%d is not in the tree.\n",num);
                   else printf("%d is in the tree.\n",num);
                   break;
              default:
                   break;
              }
         }
    }
    Which handles user input for some reason it is printing Enter (i)nsert, (s)earch, inorder (t)raversal, or (q)uit: twice.

    For example, this is what a run looks like:

    Enter (i)nsert, (s)earch, inorder (t)raversal, or (q)uit: i
    Enter a number to insert: 4
    Enter (i)nsert, (s)earch, inorder (t)raversal, or (q)uit: Enter (i)nsert, (s)earch, inorder (t)raversal, or (q)uit:

    No idea in hell why it is printing twice, probably something really easy as well.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    I'm getting tired so my heads getting a little fuzzy. Unfortunately I can't really analyze your code that well.

    However, remember that getchar() and scanf() are going to leave the '\n' line-terminator in the input buffer. With a quick glance at your code it appears that you might not be accounting for this.

    Scanf is smart enough to skip over leading whitespace, so if a line terminator is left in the buffer, scanf() will skip over it. However, some of the other input functions aren't as smart. For instance, if you call getchar() and there's still a '\n' sitting in the buffer, it will actually return the '\n' without skipping over it.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    33
    Yeah, I just added another getchar() to pick it up and that fixed it.

    Got the whole project done now, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-25-2008, 06:35 AM
  2. Replies: 1
    Last Post: 05-01-2003, 03:52 PM
  3. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 05:14 PM
  4. opening multiple files sequentially
    By moonwalker in forum C Programming
    Replies: 5
    Last Post: 08-20-2002, 10:57 PM
  5. Multiple .c files
    By GaPe in forum Windows Programming
    Replies: 3
    Last Post: 03-26-2002, 08:27 AM

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