Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Using pointers to move through a tree

$
0
0
I'm coding a directory program in C++. The idea is that the Folder class contains two arrays, one of subfolders, and the other (currently unimplemented) for files within that folder. I'm starting with a root folder, and reading in a file to build a tree of folders.

// Initialize the directory from file
void initDir()
{
	currentDir = new Folder();
	root = Folder();
	
	int numDirs;
	ifstream dir("directory");
	
	dir >> numDirs;
	
	Folder* db = NULL;
	db = new Folder[numDirs];
	
	if(dir.is_open())
	{
		int counter = 0;
		int id, numSubfolders;
		string name;
		while(dir >> id >> name >> numSubfolders)
		{
			Folder temp = Folder(id, name);
			db[counter] = temp;
			counter++;
			int blank;
			for(int i=0; i<numSubfolders; i++)
			{
				dir >> blank;
			}
		}
		dir.clear() ;
		dir.seekg(0, ios::beg) ;
		dir >> numDirs;
		counter = 0;
		while(dir >> id >> name >> numSubfolders)
		{
			for(int i=0; i<numSubfolders; i++)
			{
				int newID;
				dir >> newID;
				for(int j=0; j<numDirs; j++)
				{
					if(db[j].GetID() == newID)
					{
						cout << "Add " << db[j].GetName() << " to " << db[counter].GetName() << endl;
						db[counter].addSubfolder(db[j]);
						cout << "Added " << db[counter].GetSubfolders()[db[counter].GetFolderSize() - 1].GetName() << endl;
					}
				}
			}
			counter++;
		}
		dir.close();
	}
	else cout << "Unable to load directory" << endl;
	
	root = db[0];
	currentDir = &root;
}



I use currentDir to mark the current folder, and have another function to access subfolders:

// Move to new directory
string newword;
bool didChange = false;
getline(iss, newword, ' ');
for(int i=0; i<currentDir->GetFolderSize(); i++)
{
	if(newword == currentDir->GetSubfolders()[i].GetName())
	{
		Folder newDir = currentDir->GetSubfolders()[i];
		currentDir = &newDir;
		cout << "Contents of " << currentDir->GetName() << ":" << endl;
		bool hasPrinted = false;
		for(int i=0; i<currentDir->GetFolderSize(); i++)
		{
			cout << currentDir->GetSubfolders()[i].GetName() << " (Folder)" << endl;
			hasPrinted = true;
		}
		for(int i=0; i<currentDir->GetFileSize(); i++)
		{
			cout << currentDir->GetContents()[i].GetName() << "(File)" << endl;
			hasPrinted = true;
		}
		if(!hasPrinted)
		{
			cout << currentDir->GetName() << " is empty." << endl;
		}
		didChange = true;
	}
}
if(!didChange)
{
	if(newword == root.GetName())
	{
		currentDir = &root;
	}
	else
	{
		cout << "The directory you have specified does not exist.  Use the command \"dirnew\" to create a new directory." << endl;
	}
}



The code compiles just fine, but when I run it and try to access a subfolder, it either claims the subfolder is empty (which it isn't) or gives me complete gibberish before segfaulting. I used the "Add ____" and "Adding _______" couts to debug the addition of subfolders, which is working just fine.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>