当前位置:首页 > PHP教程 > PHP常见问题

php与c 实现按行读取文件实例代码

php与c 实现按行读取文件

前言

感觉很糟糕的一场电话一面竟然给了二面通知,好吧,给自己一个机会也给对方一次机会,题外话。海量数据处理经常涉及到hash将原来文件的每一行散列到子文件中,那如何按行读取文件呢,这里记录一下php和c的实现

很水的一篇,只是记录一下常用的方法,防止面试尴尬

php代码:

<?php /** * 按行读取文件 * @param string $filename */ function readfilebyline ($filename) { $fh = fopen($filename, 'r'); while (! feof($fh)) { $line = fgets($fh); echo $line; } fclose($fh); } // test $filename = "/home/wzy/test/sort.txt"; readfilebyline($filename);

c实现代码:

#include <stdio.h> #include <stdlib.h> #include <string.h> #define len 1024 int main(void) { char filename[len], buf[len]; file *fp; int len; scanf("%s", filename); fp = fopen(filename, "r"); if (fp == null) exit(-1); while (fgets(buf, len, fp) != null) { len = strlen(buf); buf[len - 1] = ''; // 去掉换行符 printf("%sn", buf); } return 0; }

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!



【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!