Read a specific line from a file: Difference between revisions
Content added Content deleted
(Added Elixir implementation) |
(Added PHP example) |
||
Line 1,319: | Line 1,319: | ||
?gets(fn) -- (shows -1 if past eof) |
?gets(fn) -- (shows -1 if past eof) |
||
close(fn)</lang> |
close(fn)</lang> |
||
=={{header|PHP}}== |
|||
<lang php><?php |
|||
$DOCROOT = $_SERVER['DOCUMENT_ROOT']; |
|||
function fileLine ($lineNum, $file) { |
|||
$count = 0; |
|||
while (!feof($file)) { |
|||
$count++; |
|||
$line = fgets($file); |
|||
if ($count == $lineNum) return $line; |
|||
} |
|||
die("Requested file has fewer than ".$lineNum." lines!"); |
|||
} |
|||
@ $fp = fopen("$DOCROOT/exercises/words.txt", 'r'); |
|||
if (!$fp) die("Input file not found!"); |
|||
echo fileLine(7, $fp); |
|||
?></lang> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |