Skip to content

Latest commit

 

History

History
39 lines (32 loc) · 1.1 KB

files.md

File metadata and controls

39 lines (32 loc) · 1.1 KB
title
Files

A fast way to count number of lines

Some approaches to manipulating text file data

  • readAll() to read all the file into a single string variable. Fast but not practical for enormous files. Use splitLines iterator to do line-based manipulations
  • readLine() to read from file a line at a time
  • CsvParse() with readRow() to perform line-based manipulation for rows of text that need to be broken into "fields".
import parsecsv
  • memfiles and lines() iterator to iterate over memory mapped portions of the file (see example above).

Ensuring the file is closed

The defer() statement can be used instead of try: finally: blocks

  import strutils
  var   
    lineCnt = 0
    f = open("test_readfile2.nim")
  defer: close(f)
  for line in f.readAll().splitLines():
    inc lineCnt
  echo lineCnt