#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <cctype>
#include <cstdio>
#include <string>
#include <list>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
using std::string;
using std::ostream;
using std::list;
using std::endl;
off_t tell(int fd) {
return lseek(fd, 0, SEEK_END);
}
void dir_traverse(const std::string& path, std::ostream& out) {
list<string> child_directories;
DIR*dirp = opendir(path.data());
struct dirent*dir_entry = readdir(dirp);
while(dir_entry !=NULL){
unsigned char d_type = dir_entry->d_type==DT_DIR?'D' : 'F';
if(d_type == 'D'){
if(dir_entry->d_name[0]!= '.') {
child_directories.push_back(dir_entry->d_name);
out<<'\t'<<d_type<<":"<<dir_entry->d_name<<endl;
}
}
if(d_type == 'F'){
int fd= open(dir_entry->d_name, O_RDONLY);
if(fd =-1){
out<<"file did not open"<<'\t'<<errno<<endl;
}
int size= tell(fd);
out<<'\t'<<d_type<<":"<<dir_entry->d_name<<endl;
close(fd);
//open file
//read file
//compare two files
//print name of file and path if two are equal otherwise do nothing
}
dir_entry= readdir(dirp);
}
list<string>::iterator it = child_directories.begin();
while(it != child_directories.end()) {
dir_traverse(path + "/" + *it, out);
it++;
}
closedir(dirp);
}
int main() {
dir_traverse("./homework", std::cout);
}
答案 0 :(得分:3)
连接它们:
open((path + "/" + dir_entry->d_name).c_str(), ...)