/****** C:\testc> C:\testc> path C:\Dev-Cpp\bin;%path% C:\testc> gcc linenum.c -o linenum.exe C:\testc> linenum < linenum.c 01 //linenum.c --- by tsaiwn@csie.nctu.edu.tw 02 // tcc linenum.c 或是 gcc linenum.c -o linenum 03 // linenum < inputfile.name 04 // linenum < inputFile.name > outputFile.name 05 #include 06 int main( ) { 07 static char buf[999]; // 用 static 就不會佔用 Stack 的空間 08 int n = 0; // 一開始放 0 這樣加上 1 就是 Line 1 09 fgets(buf, sizeof(buf), stdin); // 先讀入一列然後進入 Loop 10 while(! feof(stdin) ) { // while檔案stdin (鍵盤) 尚未結束 11 ++n; 12 printf("%5.2d %s", n, buf); 13 buf[0] = 0; // clear the buffer -- 先把上次讀到的清除 ! 14 fgets(buf, sizeof(buf), stdin); // 在 Loop 內最後再讀入一列 15 } // while // 注意下列很重要喔, 因輸入檔最後一列可能沒 new Line 16 if(buf[0] != 0) printf("%5.4d %s", ++n, buf); // 注意 ++n 17 printf("\n"); 18 if(*buf != 0)fprintf(stderr, "Waring: no newline at end of file\n"); 19 return 0; 20 } // main 注意要把 Line numbers 去掉 C:\testc> C:\testc> dir linen* 磁碟區 C 中的磁碟沒有標籤。 磁碟區序號: F021-1832 C:\testc 的目錄 2010/09/17 上午 02:48 928 linenum.c 2010/09/14 下午 04:58 15,856 linenum.exe 2010/11/29 上午 12:23 1,612 linenum.txt 3 個檔案 17,834 位元組 0 個目錄 68,932,206,592 位元組可用 C:\testc> **************************************/