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条)

报告相同问题?

悬赏问题

  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成