//linen.c --- by tsaiwn@csie.nctu.edu.tw // gcc -o linen.exe linen.c 或是 tcc linen.c // linen < inputfile.name // linen < inputFile.name > outputFile.name #include int main( ) { static char buf[999]; // 用 static 就不會佔用 Stack 的空間 int n = 0; // 一開始放 0 這樣加上 1 就是 Line 1 fgets(buf, sizeof(buf), stdin); // 先讀入一列然後進入 Loop while(! feof(stdin) ) { // while檔案stdin (鍵盤) 尚未結束 ++n; printf("%5d %s", n, buf); buf[0] = 0; // clear the buffer -- 先把上次讀到的清除 ! fgets(buf, sizeof(buf), stdin); // 在 Loop 內最後再讀入一列 } // while // 注意下列很重要喔, 因輸入檔最後一列可能沒 new Line if(buf[0] != 0) printf("%5d %s", ++n, buf); // 注意 ++n printf("\n"); if(buf[0]) fprintf(stderr, "Waring: no newline at end of file\n"); return 0; } // main