Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

แสดงรายการไฟล์ที่แก้ไข เก่า และใหม่ที่สุดบน Linux ด้วย C++

ใช้คำสั่ง Linux ls -lt (เรียงตามเวลาล่าสุดก่อน) และ ls -ltr (เรียงตามเวลาสุดท้ายก่อน) ผ่านฟังก์ชัน system() ใน C++

โค้ดตัวอย่าง

#include <iostream>
#include <cstdlib>
using namespace std;

int main() {
    cout << "Files List (Newest first):\n";
    system("ls -lt");
    cout << "\n\nFiles List (Oldest first):\n";
    system("ls -ltr");
}

ผลลัพธ์ตัวอย่าง

Files List (Newest first)
total 32
-rwxr-xr-x 1 user user 8984 May 11 15:19 a.out
-rw-r--r-- 1 user user  424 May 11 15:19 linux_mod_list.cpp
...

Files List (Oldest first)
total 32
-rw-r--r-- 1 user user 1204 May 4 16:24 1325.test.cpp
-rw-r--r-- 1 user user  557 May 4 16:34 trim.cpp
...

หมายเหตุ: วิธีนี้ขึ้นอยู่กับระบบปฏิบัติการ Linux และ shell command