Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Общие вопросы > Вывод размеров файлов в порядке возрастания


Автор: Miracleone 23.6.2019, 12:00
Почему не выводит размеры файлов?
Код

#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;

Автор: xvr 24.6.2019, 13:37
Цитата(Miracleone @  23.6.2019,  12:00 Найти цитируемый пост)
Почему не выводит размеры файлов?

Видимо потому, что размер вы не выводите, только имя (строка 100)

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)