dongyuyi5680 2011-03-15 15:57
浏览 309
已采纳

手动将fgetc文件指针移动到下一行

Question 1: How can I manually move the fgetc file pointer from its current location to the next line?

I'm reading in data character by character until a specified number of delimiters are counted. Once the delimiter count reaches a certain number, it needs to copy the remainder of the line until a new line (the record delimiter). Then I need to start copying character by character again starting at the next record.

Question 2: Is manually moving the file pointer to the next line the right idea? I would just explode(at " ") but I have to count the pipe delimiters first because " " isn't always the record delimiter.

Here's my code (it puts all the data into the correct record until it reaches the last delimiter '|' in the record. It then puts the rest of the line into the next record because I haven't figured out how to make it correctly look for the ' ' after specified # of | are counted):

$file=fopen("source_data.txt","r") or exit ("File Open Error");
$record_incrementor = 0;
$pipe_counter = 0;

while (!feof($file))
    {
        $char_buffer = fgetc($file);
        $str_buffer[] = $char_buffer;

            if($char_buffer == '|')
            {
                $pipe_counter++;
            }
            if($pipe_counter == 46) //Maybe Change to 46
            {   
                $database[$record_incrementor] = $str_buffer;
                $record_incrementor++;
                $str_buffer = NULL;
                $pipe_counter = 0;
            }


    }

Sample Data:

1378|2009-12-13 11:51:45.783000000|"Pro" |"B13F28"||""|1||""|""|""|||False|||""|""|""|""||""||||||2010-12-15 11:51:51.330000000|108||||||""||||||False|""|""|False|""|||False
1379|2009-12-13 12:23:23.327000000|"TLUG"|"TUG"||""|1||""|""|""|||False|||""|""|""|""||""||||||1943-04-19 00:00:00|||||||""||||||False|""|""|False|""|||False
  • 写回答

3条回答 默认 最新

  • dongwei9365 2011-03-15 16:29
    关注

    I'd say that doing this via file handling functions is a bit clumsy, when it could be done via regular expression quite easily. Just read the entire file into a string using file_get_contents() and doing a regular expression like /^(([^|]*\|){47}([^ ]*))/m with preg_match_all() could find you all the rows (which you can then explode() using | as the delimiter and setting 48 as the limit for number of fields.

    Here is a working example function. The function takes the file name, field delimiter and the number of fields per row as the arguments. The function returns 2 dimensional array where first index is the data row number and the second is the field number.

    function loadPipeData ($file, $delim = '|', $fieldCount = 48)
    {
        $contents = file_get_contents($file);
        $d = preg_quote($delim, '/');
        preg_match_all("/^(([^$d]*$d){" . ($fieldCount - 1) . '}([^
    ]*))/m', $contents, $match);
        $return = array();
    
        foreach ($match[0] as $line)
        {
            $return[] = explode($delim, $line, $fieldCount);
        }
    
        return $return;
    }
    
    var_dump(loadPipeData('source_data.txt'));
    

    (Note: this is a solution to the original problem)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么