เขาทำ progress ใน terminal กันยังไงหว่า?

เคยเรียกคำสั่งอะไรใน terminal แล้วมันก็ขึ้นเลข 0%, 10%, 20%, … 100% ใน terminal ไหม (มันอัพเดทข้อความในบรรทัดเดิมได้เรื่อยๆ… มันทำยังไงหว่า?)

อ่ะนี่ ตัวอักษรสำคัญเลขคือ \r (carriage return)

#include<stdio.h>
void delay()
{
   for(int i=0;i<1000000;i++) for(int j=0;j<10;j++);
}
int main()
{
   for(int p=0;p<=100;p++)
   {
       delay();
       printf("\rPercent: %d ", p);
       fflush(stdout);
   }
   printf("\n");
   return 0;
}

งืมๆ… มาพ่นโค้ด แล้วก็ไป ฮ่าๆ

Comments (2)

1% 2% 3% ... 200%June 4th, 2011 at 10:46 am

Wow! This trick is cool. Instead of having thousands of characters printed out, I can have just a few lines on my screen.

Note that this still works well if you redirect output from screen to a file!

[Reply]

neizodNovember 20th, 2011 at 1:44 pm

อ๋อ.. \r นี่เอง ;)

[Reply]

Leave a comment

Your comment