Php and c implement reading the file instance code by line,

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.