Home >Backend Development >PHP Tutorial >Code for reading files line by line (php, c implementation)

Code for reading files line by line (php, c implementation)

WBOY
WBOYOriginal
2016-07-25 08:55:271053browse
  1. /**
  2. * Read the file line by line
  3. * @param string $filename
  4. * @site bbs.it-home.org
  5. */
  6. function readFileByLine ($filename)
  7. {
  8. $fh = fopen($filename, 'r');
  9. while (! feof($fh)) {
  10. $line = fgets($fh);
  11. echo $line;
  12. }
  13. fclose($fh);
  14. }
  15. // test
  16. $filename = "/home/wzy/test/sort.txt";
  17. readFileByLine($filename);
复制代码

2,c语言按行读取文件

  1. #include
  2. #include
  3. #include
  4. #define LEN 1024
  5. int main(void)
  6. {
  7. char filename[LEN], buf[LEN];
  8. FILE *fp;
  9. int len;
  10. scanf("%s", filename);
  11. fp = fopen(filename, "r");
  12. if (fp == NULL) exit(-1);
  13. while (fgets(buf, LEN, fp) != NULL) {
  14. len = strlen(buf);
  15. buf[len - 1] = '
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn