《PHP應用:php與c 實現按行讀取文件實例代碼》要點:
本文介紹了PHP應用:php與c 實現按行讀取文件實例代碼,希望對您有用。如果有疑問,可以聯系我們。
PHP實例php與c 實現按行讀取文件
PHP實例前言
PHP實例感覺很糟糕的一場電話一面竟然給了二面通知,好吧,給自己一個機會也給對方一次機會,題外話.海量數據處理經常涉及到hash將原來文件的每一行散列到子文件中,那如何按行讀取文件呢,這里記錄一下php和c的實現
PHP實例很水的一篇,只是記錄一下常用的方法,防止面試尷尬
PHP實例php代碼:
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);
PHP實例c實現代碼:
PHP實例
#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] = '\0'; // 去掉換行符
printf("%s\n", buf);
}
return 0;
}
PHP實例感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/2039.html