Basically I am creating a basic shell program for a class project. I am stuck on iterating through the files/folders within a directory.
My error is in the function DisplayDirectoryContents()
class Directory
{
private:
map<string, pair<list<Folder>, list<File> > > directoryContents;
string directoryPath;
public:
list<Folder> getFolders() {return directoryContents[directoryPath].first;}
list<File> getFiles() {return directoryContents[directoryPath].second;}
string getDirectoryPath() {return directoryPath;}
Directory()
{
directoryPath = "root/";
File *file = new File("Test");
directoryContents[directoryPath].second.push_back(*file);
}
void DisplayDirectoryContents()
{
// Get files and folders from directory
list<File> files = this->getFiles();
for(int i = 0; i < files.size(); i++)
{
cout << files->getFileName(); << Error here
}
}
};
I've tried setting this function up a few different ways but I can't seem to get the right kind of code to make it work.
I would think this would work but it's throwing me errors everytime I try and mess with the DisplayDirectoryContents() function. Does anyone have any tips that I can try to help fix this? Thanks
class File
{
string fileName;
string fileTime;
public:
string getFileTime(){return fileTime;}
string getFileName(){return fileName;}
void setFileTime(){time_t now = time(NULL);
fileTime = ctime(&now);}
void setFileName(string newFileName){fileName = newFileName;}
File(){}
File(string fName)
{
fileName = fName;
time_t now = time(NULL);
fileTime = ctime(&now);
}
void MakeFile(string fName);
void RemoveFile(string fName);
};
If you are able to use C++11 or higher, you can use a range for
loop to iterate over the contents of the list.
void DisplayDirectoryContents()
{
// Get files and folders from directory
list<File> files = this->getFiles();
// Iterate over each item in the list.
// Use a reference to avoid copying of the items.
for ( File& file : files )
{
cout << file.getFileName();
}
}