Read Input Grammar From Standard Input C++
The stdin is the short 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 also known as the pointer because the developers access the data from the users or files and tin perform an action on them.
In this write-up, we will utilize the congenital-in functions of C programming that tin be used to read the input past the stdin.
How to read a line from stdin in C programming
There are unlike 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 item.
getline(): This function is used to read the lines from the stdin. To sympathize this function, let us 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 chief( 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 so displayed with the count of its characters. The getline() function reads a full sentence from the stdin and allocates some memory on the heap and saves information technology there. In the getline() we pass the address of the retentivity where the line should be stored, the address of the length of the line, and the stdin. Then simply display the line and its length using the printf() function. Moreover, in the end, nosotros used free() so that the space occupied in retentivity tin can exist cleared to re-use it next fourth dimension.
getchar(): The getchar() role is used to read the first character of the stdin and the putchar() is used to display the single character on the screen. The drawback of the getchar() and putchar() is that they can read and display just i character at a time merely we tin 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 cord 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 lawmaking using the gcc compiler:
$ gcc myfile4.c -o myfile4
Execute the myfile4:
In the in a higher place code, we input a line "Hello! It's Linuxhint" and the getchar() reads the first character of the line, and putchar() is used to display the line. First, we have asked the user most the length of the string 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()" office. The scanf takes the input from the stdin, then scans it and saves it in some variable or array. For instance:
#include <stdio.h>
int main( ) {
char a[ 100 ] ;
fprintf ( "Enter a cord :" ) ;
fscanf (stdin, "%s" , a) ;
fprintf ( stdout, "\nYou entered the following string: %s " , a) ;
fprintf (stdout,"\n") ;
return 0 ;
}
Using the gcc compiler, compile the plan of myfile5.c to debug the errors:
$ gcc myfile5.c -o myfile5
Execute the myfile5:
In the to a higher place script, we 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 impress the strings too. Then displayed the string stored in assortment a[] that is "How-do-you-do".
Conclusion
The stdin is used for taking the input from the keyboard and it can read in unlike ways. There are unlike functions used for reading the stdin. In this write-up, we have used unlike functions used to read a line. The built-in function in c programming is getline() which is used for reading the lines from the stdin. Merely we can besides use other functions similar getchar() and scanf() for reading the lines.
Source: https://linuxhint.com/read-lines-stdin-c-programming/
0 Response to "Read Input Grammar From Standard Input C++"
Post a Comment