Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading line by line from STDIN

Tags:

php

stdin

pipe

I want to do something like this:

$ [mysql query that produces many lines] | php parse_STDIN.php 

In parse_STDIN.php file I want to be able to parse my data line by line from stdin.

like image 313
sobi3ch Avatar asked Aug 15 '12 11:08

sobi3ch


People also ask

How do you read from stdin in C?

The built-in function in c programming is getline() which is used for reading the lines from the stdin. But we can also use other functions like getchar() and scanf() for reading the lines.

How do you scan a line by line in Python?

Use fileinput. input() reads through all the lines in the input file names specified in command-line arguments. If no argument is specified, it will read the standard input provided.


1 Answers

use STDIN constant as file handler.

while($f = fgets(STDIN)){     echo "line: $f"; } 

Note: fgets on STDIN reads the \n character.

like image 107
Shiplu Mokaddim Avatar answered Sep 20 '22 02:09

Shiplu Mokaddim