Search
 
SCRIPT & CODE EXAMPLE
 

C

how to add a number before every line in c language

#include <stdio.h>

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

    int c, last = 0; /* c must be type int, not char to match EOF */
    size_t ln = 1;      /* use size_t for counters */
    /* use filename provided as 1st argument (stdin by default) */
    FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;

    if (!fp) {  /* validate file open for reading */
        perror ("file open failed");
        return 1;
    }
    printf ("%06zu ", ln++);            /* output line 1 number */
    while ((c = getc(fp)) != EOF) {     /* read each character */
        if (last)                       /* test if last set */
            putchar (last);             /* output all characters */
        if (last == '
')               /* test if last is newline */
            printf ("%06zu ", ln++);    /* output next line number */
        last = c;                       /* set last to c */
    }
    putchar (last);                     /* output final character */
    if (last != '
')                   /* check POSIX eof */
        putchar('
');                  /* tidy up with newline */
    if (fp != stdin)                    /* close file if not stdin */
        fclose (fp);

    return 0;
}
Comment

PREVIOUS NEXT
Code Example
C :: how to devowel string in c program 
C :: using tables as arguments in c++/c 
C :: c Modulo 10^9+7 (1000000007) 
C :: captive portal esp8266 
C :: Wait until an animation finishes - Selenium, Java 
C :: iulia vântur 
C :: How to get the number of characters in a string without using strlen function 
C :: listas enlazadas/ linked lists 
C :: inline function in c example 
C :: c++ sum of ascii string 
C :: error: dereferencing pointer to incomplete type 
C :: float 
C :: C what does /= mean 
C :: matrix of string in c 
C :: C Variable Byte Sizes 
Dart :: how to diable flutter for web 
Dart :: textfield border radius flutter 
Dart :: flutter textfield label align top 
Dart :: dart input field overflow when keyboard open 
Dart :: flutter card border radius overflow hidden 
Dart :: how to stop screen rotation in flutter 
Dart :: raisedbutton shape flutter 
Dart :: hive regiter adapter enum 
Dart :: flutter snackbar width 
Dart :: flutter print line char limit 
Dart :: replace string in dart,replace string in dart using regex 
Dart :: flutter disable horizontal 
Dart :: flutter datatypes check 
Dart :: replaceall dart 
Dart :: how to convert int/int to float dart 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =