donljt2606 2014-04-04 17:27
浏览 42

在php中生成的大文件的索引问题

I have a problem to retrieve the value of large files (> 7GB). once I find out the solution, mostly using fgets (). examples of such cases: My file:

#CHROM  POS ID  REF ALT
1   8   rs392108184 T   G
4   91  rs122936913 G   T

my code:

<?php
    //(1)
    echo "(1)";
    $data = file_get_contents("data/ncbi/5.vcf");
    $data = explode("
", $data);
    echo '<pre>';
    print_r($data);
    echo '</pre>';

    //(2)
    echo "(2)";
    $handle = @fopen("data/ncbi/5.vcf", "r");
    if ($handle) {
        while (($buffer = fgets($handle, 4096)) !== false) {
            $buffer = explode("
", $buffer);
            echo '<pre>';
        print_r($buffer);
        echo '</pre>';
        }
        fclose($handle);
    }
?>

output:

(1)
Array
(
    [0] => #CHROM   POS ID  REF ALT
    [1] => 1    8   rs392108184 T   G
    [2] => 4    91  rs122936913 G   T
)
(2)
Array
(
    [0] => #CHROM   POS ID  REF ALT
    [1] => 
)
Array
(
    [0] => 1    8   rs392108184 T   G
    [1] => 
)
Array
(
    [0] => 4    91  rs122936913 G   T
    [1] => 
)

if i use (1), output as I want but can not use for large files. whereas if I use (2), can be used for large files, but the output as it is not what I wanted, because iteration is not clear. I want to use large files and the output could be like this.

Array
    (
        [0] => #CHROM   POS ID  REF ALT
        [1] => 1    8   rs392108184 T   G
        [2] => 4    91  rs122936913 G   T
    )

is there any solution?

  • 写回答

1条回答 默认 最新

  • dongqiangteng7319 2014-04-04 17:41
    关注

    If you run out of memory trying to load the whole file into memory, count the lines in your own code:

    $linenum = 0;
    while ($line = fgets($handle)) {
        echo "<pre>($linenum) => $line</pre><br>";
        $linenum++;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100