I get the parameters from the cmd command line and output them, then I need to find the file with the largest file size in this folder with which the script is running, how can I get the file size?

#include <iostream.h> using namespace std; int main(int argc, char *argv[]) { for(int i=0;i<argc;i++) { cout<<"Argument"<<i<<":"<<argv[i]<<endl; } return 0; } 

    1 answer 1

    Standard library <filesystem> . You can read the contents of a directory using directory_iterator , like a set of directory_entry , which has a member function file_size() that returns the size of the file ...

    And then - just find the maximum.

    The simplest example is

     namespace fs = std::filesystem; int main() { for(auto& p: fs::directory_iterator("..")) { std::cout << p.path() << " has size " << p.file_size() << " bytes" << std::endl; } } 

    Well, and find the maximum and withdraw - this is your own :)

    • for (directory_iterator itr (directory_entry); itr! = directory_iterator (); ++ itr) {} Is this a valid entry? - Alexander
    • Please write sample code - Alexander
    • Well, see a piece in the answer ... - Harry
    • Thank you very much - Alexander