Thread: tree container

  1. #31
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Can you answer my confusion about the files. Perhaps give an example of how they are supposed to look? That information might be available in the code but I don't want to spend a lot of time digging through it to figure that out.

  2. #32
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    This is example of input file (but program should support some different language versions and different versions of dir/s command dependent on version of Windows):

    Code:
     Wolumin w stacji E nie ma etykiety.
     Numer seryjny woluminu: D89D-A886
    
     Katalog: E:\Main
    
    2008-11-02  19:23    <DIR>          .
    2008-11-02  19:23    <DIR>          ..
    2008-11-02  19:21    <DIR>          katalog 1
    2008-11-02  19:21    <DIR>          moj katalog
    2008-11-02  19:23                 0 spis.txt
                   1 plik(˘w)               0 bajt˘w
    
     Katalog: E:\Main\katalog 1
    
    2008-11-02  19:21    <DIR>          .
    2008-11-02  19:21    <DIR>          ..
    2008-11-02  19:21    <DIR>          kat
    2008-11-02  19:21    <DIR>          kat2
    2008-11-02  19:21                10 plik.txt
    2008-11-02  19:21                 0 plik2.txt
                   2 plik(˘w)              10 bajt˘w
    
     Katalog: E:\Main\katalog 1\kat
    
    2008-11-02  19:21    <DIR>          .
    2008-11-02  19:21    <DIR>          ..
                   0 plik(˘w)               0 bajt˘w
    
     Katalog: E:\Main\katalog 1\kat2
    
    2008-11-02  19:21    <DIR>          .
    2008-11-02  19:21    <DIR>          ..
                   0 plik(˘w)               0 bajt˘w
    
     Katalog: E:\Main\moj katalog
    
    2008-11-02  19:21    <DIR>          .
    2008-11-02  19:21    <DIR>          ..
    2008-11-02  19:21                 0 plik22.txt
                   0 plik(˘w)               0 bajt˘w
    
         Razem wymienionych plik˘w:
                   3 plik(˘w)               0 bajt˘w
                  14 katalog(˘w)   1˙212˙960˙768 bajt˘w wolnych
    I thought about using output txt file but after considering it once more I decided not to use any output file.
    Last edited by kawafis44; 12-09-2008 at 01:35 AM.

  3. #33
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I would use some sort of recursive function. That way, the current parent will be available inside the function, and any new directories you find will be added to that parent. I think this would solve the problem you have with your second question.

    You could also just have a variable that remembers the current parent. Finding the correct parent I guess would be the hard part after you've processed the full block.

    One question I have in glancing at your code is about when you create the root. It looks like you are creating a root for every block. This is not a good idea. You need one root for the entire tree. In your example the root should be for E:\.

  4. #34
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    > It looks like you are creating a root for every block. This is not a good idea. You need one root for the entire tree. In your example the root should be for E:\.


    But I've got this line:
    Code:
    //Directory *root = new Directory(myRoot); //it creates root
    Directory root(myRoot, whereInFile); //it creates object root
    in this block of code:
    Code:
    //-----------------------------------------------------------------------
    //first iteration - it checks version of dir/s and reads name of main dir
    //-----------------------------------------------------------------------

    I'm not so sure what to write here:
    Code:
    case 4:
       if (line[0] != ' ')
       { //case 4a - main body of the block
          actualName = line.substr(36); //name of new file or directory
          if (line[21] == '<') //it is directory
          {
             // ????
          }
       }

    At first I thought about writing something like this:
    Code:
    //Directory *sub1   = new Directory("sub1", whereInFile);
    //root.children.push_back(sub1); (or "root.children.push_back(sub1, whereInFile);" ?)
    and
    Code:
    //Directory *sub11  = new Directory("sub1.1");
    //sub1->children.push_back(sub11);
    and I think that's not good way to do it.


    It should be better to create it in two stages. 1) create object, 2) insert object into tree (in right place). So I created "Directory root(myRoot, whereInFile);" in line 104 and now I can create other object in line 150:
    Code:
    Directory new_directory(actualName, whereInFile); //this is the only one new line added to the code from message #30
    ActualName is created in line 147:
    Code:
    actualName = line.substr(36); //name of new file or directory
    and whereInFile should work as well. It is declared in line 64:
    Code:
    long int whereInFile = 0; //what number of line in the file is the actual one
    and increased every time in line 183
    Code:
    //every iteration it should increase whereInFile by one
    whereInFile++;
    At first time it should be actualName=="katalog 1" and whereInFile==1.


    And my problem is that this line:
    Code:
    Directory new_directory(actualName, whereInFile);
    will be creating new_directory every time. I need NEW, OTHER object, not the same object called new_directory every time.


    And the other problem is with inserting object into tree. I've got in line 29:
    Code:
    vector<Directory*> children; //children directories
    but it is inside the class. I guess I need something outside the class so that I can store table of objects or something like this.

    Added: 15 Dec 2008

    I see it may be too much to analyse my previous post. So here is my problem written with no unnecessary code. It looks like I've got problem with the same thing as at the beginning, about one month ago - with this tree structure (creating new branches).

    Code:
    #include <iostream>
    #include <vector>
    #include <fstream>
    using namespace std;
    
    class Directory
    {
     private:
        Directory::Directory(const Directory& dir) {}
        void Directory::operator=(const Directory& dir) {}
     public:
        string name;                 //name of the directory
        vector<Directory*> children; //children directories
        long int lineInFile;         //What is the number of line in the file
                                     //where list of files in this directory begins
                                     //(don't mistake it with whereInFile)
    
        Directory(string name_ = "No Name", long int lineInFile_ = -1) :
           name(name_), lineInFile(lineInFile_) { }; //default constructor
           //lineInFile == -1 means error
        ~Directory()
        {
           //delete children;
           for (int i = 0; i<children.size(); i++)
           { delete children[i]; }
        };
    };
    
    int nesting = 0;
    
    void print(Directory *dir) { //my print function needs to take a pointer...
       for (int i = 0; i < nesting; ++i) cout << ' ';
       cout << dir->name << endl; //...so I've got this...
       ++nesting;
       for_each(dir->children.begin(), dir->children.end(), print); //...and this.
       --nesting;
    }
    
    int main()
    {
       //myRoot
       //---firstSub
       //------firstSubA
       //------firstSubB
       //---secondSub
       //------secondSubA
       //---thirdSub
        
       Directory root("myRoot", 0); //it creates object root
       string names = ("firstSub", "firstSubA", "firstSubB", "secondSub", "secondSubB", "thirdSub"); //examplary data
       int numbers = (10, 14, 22, 29, 36, 46);                                                       //examplary data
       for (int i=0; i<6; i++)
       {
          //what kind of recursive function should it be ??
          //Directory branch(names[i], numbers[i]);
       }
    
       system("pause"); return 0;
    }
    Regards!

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. Linked List Queue Implementation help
    By Kenogu Labz in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2005, 10:14 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM