Почему не выводит размеры файлов?
Код | #include <algorithm> #include <dirent.h> #include <iostream> #include <stdio.h> #include <string> #include <string.h> #include <sys/stat.h> #include <sys/types.h> #include <vector> bool cmp(std::pair<std::string, int> a, std::pair<std::string, int> b) { return a. second < b. second; } int main(int argc, char* argv[]) { DIR* d = opendir("."); if (d == NULL) { perror("Ошибка открытия текущего каталога"); } struct dirent* de; std::vector<std::pair<std::string, int>> files; // Сюда будут записаны имена и размеры файлов while (de = readdir(d)) { if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) { continue; // . и.. выводить не нужно } struct stat st; if (stat(de->d_name, &st) == -1) { std::cout << "Ошибка stat(" << de->d_name << "): " << strerror(errno) << std::endl; } else { if (S_ISDIR(st. st_mode)) { std::cout << de->d_name << std::endl; } else { files. push_back(std::pair<std::string, int>(de->d_name, st. st_size)); } } } closedir(d); std::sort(files. begin(), files. end(), cmp); for (int i = 0; i < files. size(); i++) { std::cout << files[i].first << std::endl; } return 0;
|
|