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.
Printable View
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.
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):
I thought about using output txt file but after considering it once more I decided not to use any output file.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 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:\.
> 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:
in this block of code:Code://Directory *root = new Directory(myRoot); //it creates root
Directory root(myRoot, whereInFile); //it creates object root
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:
andCode://Directory *sub1 = new Directory("sub1", whereInFile);
//root.children.push_back(sub1); (or "root.children.push_back(sub1, whereInFile);" ?)
and I think that's not good way to do it.Code://Directory *sub11 = new Directory("sub1.1");
//sub1->children.push_back(sub11);
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:
ActualName is created in line 147:Code:Directory new_directory(actualName, whereInFile); //this is the only one new line added to the code from message #30
and whereInFile should work as well. It is declared in line 64:Code:actualName = line.substr(36); //name of new file or directory
and increased every time in line 183Code:long int whereInFile = 0; //what number of line in the file is the actual one
At first time it should be actualName=="katalog 1" and whereInFile==1.Code://every iteration it should increase whereInFile by one
whereInFile++;
And my problem is that this line:
will be creating new_directory every time. I need NEW, OTHER object, not the same object called new_directory every time.Code:Directory new_directory(actualName, whereInFile);
And the other problem is with inserting object into tree. I've got in line 29:
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.Code:vector<Directory*> children; //children directories
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).
Regards!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;
}