Facebook Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
Please elalaborate more!!!!
dir * just print contents (dir/files) in current directory...
if it is the case then .... its too easy ...
use this link:
int getdir (string dir, vector<string> &files)
{
DIR *dp;
struct dirent *dirp;
if((dp = opendir(dir.c_str())) == NULL) {
cout << "Error(" << errno << ") opening " << dir << endl;
return errno;
}
while ((dirp = readdir(dp)) != NULL) {
files.push_back(string(dirp->d_name));
}
closedir(dp);
return 0;
}
#include <dirent.h>
#include <string>
#include <string.h>
#include <iostream>
using namespace std;
int main(){
DIR *parentdir;
string dirname = "/home/prajyoti/Documents/Placement/careercup/directi";
struct dirent *fileentry ;
if((parentdir = opendir(dirname.c_str()))){
while((fileentry = readdir(parentdir)) != NULL){
if(strcmp(fileentry->d_name, ".") != 0 && strcmp(fileentry->d_name, "..")!= 0){
cout << fileentry -> d_name << endl;
}
}
}
return 0;
}
Hmm, alot is missing from the above 'solutions': where's the volume name, serial number, dir name, dir or file, date / time created, num files / dir's and their byte sizes!?!?!?
I guess you need to discover exactly what dir * does / returns and not make assumptions about what you think it returns!
can you elaborate more !!
- TheDewarist June 05, 2012