Skip to content Skip to sidebar Skip to footer

Read Lines From Stdin Line by Line in C Stopping at Carriage Returns

The stdin is the brusque form of the "standard input", in C programming the term "stdin" is used for the inputs which are taken from the keyboard either by the user or from a file. The "stdin" is as well known every bit the pointer because the developers access the information from the users or files and can perform an action on them.

In this write-up, nosotros volition use the congenital-in functions of C programming that tin be used to read the input by the stdin.

How to read a line from stdin in C programming

There are dissimilar born functions used in c programming for reading the inputs from the stdin. The functions used for reading the lines are:

  • getline()
  • getchar()
  • putchar()
  • scanf()

We will explain all these functions in particular.

getline(): This function is used to read the lines from the stdin. To understand this function, permit united states consider the example, we will create a text file with the proper noun myfile2.c and write the following script:

#include <stdio.h>

#include <stdlib.h>

int principal( void )

{

printf ("Please enter a line:\northward") ;

char *line =NILL;

size_t len = 0 ;

  ssize_t lineSize = 0 ;

  lineSize = getline( &line, &len, stdin) ;

printf ("You entered %s, which has %zu chars.\n", line, lineSize - 1 ) ;

free (line) ;

return 0 ;

}

Compile the script of file2.c using the gcc compiler:

$ gcc myfile2.c -o myfile2

Run the code using the command:

In the above output, we can see that a line is taken from the stdin and and then displayed with the count of its characters. The getline() role reads a total sentence from the stdin and allocates some retentiveness on the heap and saves it there. In the getline() we laissez passer the address of the memory where the line should be stored, the address of the length of the line, and the stdin. And then simply brandish the line and its length using the printf() role. Moreover, in the end, we used costless() so that the infinite occupied in retentivity tin be cleared to re-use it side by side time.

getchar(): The getchar() function is used to read the offset character of the stdin and the putchar() is used to display the single grapheme on the screen. The drawback of the getchar() and putchar() is that they tin can read and brandish only i character at a time but we can utilize a loop to display all the characters of stdin. To understand this, write the following code:

#include <stdio.h>

#include <stdlib.h>

int main( ) {

char c;

int i,50;

fprintf (stdout, "Enter the string length: " ) ;

fscanf (stdin, "%d" , &50) ;

fprintf (stdout, "Enter a value :" ) ;

for (i= 0 ; i<=50; i++ )

{

   c= getc (stdin) ;

putc (c,stdout) ;

}

fprintf (stdout, "\n" ) ;

render 0 ;

}

Compile the code using the gcc compiler:

$ gcc myfile4.c -o myfile4

Execute the myfile4:

In the above code, we input a line "How-do-you-do! Information technology's Linuxhint" and the getchar() reads the outset character of the line, and putchar() is used to brandish the line. First, we have asked the user about the length of the cord and then we displayed information technology with the help of a loop.

scanf(): The other widely used method to read the line from the stdin is using the "scanf()" function. The scanf takes the input from the stdin, so scans it and saves it in some variable or array. For example:

#include <stdio.h>

int primary( ) {

char a[ 100 ] ;

fprintf ( "Enter a cord :" ) ;

fscanf (stdin, "%s" , a) ;

fprintf ( stdout, "\nYou entered the following cord: %south " , a) ;

fprintf (stdout,"\n") ;

render 0 ;

}

Using the gcc compiler, compile the program of myfile5.c to debug the errors:

$ gcc myfile5.c -o myfile5

Execute the myfile5:

In the to a higher place script, nosotros simply declared the array "a" with the character data type, with the help of scanf() we took the input from the stdin. We used the "%s" constant which is used to read and print the strings likewise. And so displayed the string stored in array a[] that is "Hullo".

Decision

The stdin is used for taking the input from the keyboard and it can read in different ways. There are different functions used for reading the stdin. In this write-upward, we have used different functions used to read a line. The born part in c programming is getline() which is used for reading the lines from the stdin. But we tin can likewise use other functions similar getchar() and scanf() for reading the lines.

Almost the author

I'm an Engineering graduate and my passion for Information technology has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.

williamsamenes.blogspot.com

Source: https://linuxhint.com/read-lines-stdin-c-programming/

Postar um comentário for "Read Lines From Stdin Line by Line in C Stopping at Carriage Returns"