mingzhangyang
5/25/2018 - 4:02 PM

Read a huge file line by line

Read a huge file line by line

#!/usr/bin/env python

import sys

def work(path):
    char_counter = 0
    line_counter = 0
    with open(path, 'rb') as f:
        for line in f:
            char_counter += len(line)
            line_counter += 1
    print "Read finished, total lines: %d, total chars: %d" % (line_counter, char_counter)

if __name__ == "__main__":
    p = sys.argv[1]
    work(p)
'use strict';

const readline = require('readline');
const fs = require('fs');

function work(path) {
  let rl = readline.createInterface({
    input: fs.createReadStream(path, 'binary')
  });
  let char_counter = 0;
  let line_counter = 0;
  rl.on('line', line => {
    char_counter += line.length+1;
    line_counter++;
  }).on('close', ()=> {
    console.log('Read finished, total lines: %d, total chars: %d', line_counter, char_counter);
  });
}

if (typeof module !== 'undefined' && module.parent) {

} else {
  // test code go here
  let p = process.argv[2];
  work(p);
}
package main

import (
	"fmt"
	"bufio"
	"os"
	"log"
)

func readline(path string) {
	f, err := os.Open(path)
	if err != nil {
		log.Panic("opening file failed")
	}
	defer f.Close()
	sc := bufio.NewScanner(f)
	var char_counter, line_counter int
	for sc.Scan() {
		line := sc.Text()
		char_counter += len(line)+1
		line_counter++
	}
	if err = sc.Err(); err != nil {
		log.Panic("scanner error")
	}
	fmt.Printf("Read finished. Total lines: %d, total chars: %d\n", line_counter, char_counter)
}

func main() {
	p := os.Args[1]
	readline(p)
}
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(int argc, char *argv[]) {
    int char_counter = 0;
    int line_counter = 0;
    ifstream f;
    string line;
    f.open(argv[1]);
    while (!f.eof()) {
        getline(f, line);
        char_counter += (line.length()+1);
        line_counter++;
    }
    cout << "Read finished, total lines: " << line_counter << ", total chars: " << char_counter << endl;
}