【问题标题】:how to read txt file line by line [duplicate]如何逐行读取txt文件[重复]
【发布时间】:2012-09-05 22:17:47
【问题描述】:

可能重复:
How can I read and parse the contents of this text file?

我想像 C++ 那样读取文本文件

这是文本文件中的示例

(item(name 256) (Index 1)(Image "Wea001")   (specialty  (aspeed 700) (Hit 20)))
(item   (name 257)  (desc 520)           (Index 2)  (Image "Wea002")(specialty(Attack 16 24)))

我想要这样的输出

name : 256
Index : 1
Image : Wea001
Specialty > aspeed : 700 
Specialty > hit : 20

Name : 257
Desc : 520
Index : 2
Image : Wea002
Speciality > Attack : 16 24

这可能吗?

【问题讨论】:

标签: php


【解决方案1】:

直接出php手册:

fgets()

示例 #1 逐行读取文件

<?php
$handle = @fopen("/tmp/inputfile.txt", "r");
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {
        echo $buffer;
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}
?>

【讨论】:

    【解决方案2】:

    RTM

    <?php
    $f = fopen ("fgetstest.php", "r");
    $ln= 0;
    while ($line= fgets ($f)) {
        ++$ln;
        printf ("%2d: ", $ln);
        if ($line===FALSE) print ("FALSE\n");
        else print ($line);
    }
    fclose ($f);
    

    ?>

    【讨论】:

      猜你喜欢
      • 2017-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-02
      • 1970-01-01
      • 2019-04-24
      • 2015-05-19
      相关资源
      最近更新 更多