C language how to add line number before each line of the file

ww‮‬w.lautturi.com
C language how to add line number before each line of the file
#include <stdio.h>

int main (int argc, char **argv) {

    int c, last = 0; 
    size_t ln = 1;      
    
    FILE *fp =  fopen ("test.txt", "r") ;

    if (!fp) {  
        perror ("file open failed");
        return 1;
    }
    printf ("%06zu ", ln++);            
    while ((c = getc(fp)) != EOF) {     
        if (last)                       
            putchar (last);             
        if (last == '\n')               
            printf ("%06zu ", ln++);    
        last = c;                       
    }
    putchar (last);                     
    if (last != '\n')                   
        putchar('\n');                  
    if (fp != stdin)                    
        fclose (fp);

    return 0;
}
Created Time:2017-08-22 13:58:23  Author:lautturi