Php and c implement reading the file instance code by line,
Php and c implement reading files by line
Preface
A bad call actually gave two notifications. Okay, give yourself a chance and give the other party a chance to answer questions. Massive Data Processing often involves hash partitioning each row of the original file into a sub-file. How can we read the file by row? Here we record the implementation of php and c.
A very watery article, just record common methods to prevent interview embarrassment
Php code:
<? Php/*** read files by row * @ 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 implementation code:
# 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'; // remove the linefeed printf ("% s \ n", buf );} return 0 ;}
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!