I'm trying to print line numbers in the beginning of the lines without using
fgets()
yes, it prints line number well when I input multiple files
but I want to get result like this. Can you guys help me with this?
Now result
1 I'll always remember
2 the day we kiss my lips
3
4 light as a feather
*5 ####@localhost ~ $*
expect result
1 I'll always remember
2 the day we kiss my lips
3
4 light as a feather
*####@localhost ~$*
Here is my code:
#include <stdio.h>
int main(int argc, char *argv[]) {
FILE *fp;
int c, n;
n = 1;
for (int i = 1; i < argc; i++) {
if (argc < 2)
fp = stdin;
else
fp = fopen(argv[i], "r");
c = getc(fp);
printf("%d ", n);
while (c != EOF) {
putc(c, stdout);
if (c == '\n')
n++, printf("%d ", n);
c = getc(fp);
}
fclose(fp);
}
return 0;
}