Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Using single fscanf to read from multiple lines?

According to PHP documentation

mixed fscanf ( resource $handle , string $format [, mixed &$... ] )

Each call to fscanf() reads one line from the file.

Now I am trying to read this output from a file using single fscanf()

These are line separated inputs.

8

4.0

is the best place to learn

using this code

fscanf("%d \n %f \n %[^\\n]");

but the output I am getting is

8

blank

blank

What is the correct code for it to read using single fscanf?

like image 455
phpNoob Avatar asked Jan 22 '26 19:01

phpNoob


1 Answers

It is not possible to read multiple lines from a file with a single fscanf. An alternative is to call fscanf multiple times, one for each line. An alternative which will read all the lines in one go is the file() function.

like image 190
John Fawcett Avatar answered Jan 24 '26 12:01

John Fawcett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!