zoukankan      html  css  js  c++  java
  • nodeschool.io 3

    ~~ MY FIRST I/O! ~~

    Write a program that uses a single synchronous filesystem operation to
    read a file and print the number of newlines it contains to the
    console (stdout), similar to running `cat file | wc -l`.

    The full path to the file to read will be provided as the first
    command-line argument.

    ----------------------------------------------------------------------
    HINTS:

    To perform a filesystem operation you are going to need the `fs`
    module from the Node core library. To load this kind of module, or any
    other "global" module, use the following incantation:

    var fs = require('fs')

    Now you have the full `fs` module available in a variable named `fs`.

    All synchronous (or blocking) filesystem methods in the `fs` module
    end with 'Sync'. To read a file, you'll need to use
    `fs.readFileSync('/path/to/file')`. This method will return a
    `Buffer` object containing the complete contents of the file.

    Documentation on the `fs` module can be found by pointing your browser
    here:
    C:UsersdzhangAppDataRoaming pm ode_moduleslearnyounode ode_apidocfs.h
    tml

    `Buffer` objects are Node's way of efficiently representing arbitrary
    arrays of data, whether it be ascii, binary or some other format.
    `Buffer` objects can be converted to strings by simply calling the
    `toString()` method on them. e.g. `var str = buf.toString()`.

    Documentation on `Buffer`s can be found by pointing your browser here:
    C:UsersdzhangAppDataRoaming pm ode_moduleslearnyounode ode_apidocuff
    er.html

    If you're looking for an easy way to count the number of newlines in a
    string, recall that a JavaScript `String` can be `.split()` into an
    array of substrings and that ' '. Using this method you'll end up
    with an array that has one more element than the number of newlines.

    var fs = require('fs');
    
    var arr = fs.readFileSync(process.argv[2],'utf-8').split("
    ");
    
    console.log(arr.length-1);
  • 相关阅读:
    3星|《中国古城墙》:重要的古城墙的资料汇集
    bindingSource具体使用案例
    WPF第三方控件盘点
    FluentValidation具体使用案例
    Visual Studio 版本管理从TFS迁移到SVN
    Image.FromStream与Image.FromFile使用区别
    判断图片的格式的方法
    WCF测试小程序
    使用AutoMapper 处理DTO数据对象的转换
    获取mac地址和IP地址方式
  • 原文地址:https://www.cnblogs.com/della/p/3458122.html
Copyright © 2011-2022 走看看