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

    ~~ MY FIRST ASYNC I/O! ~~

    Write a program that uses a single asynchronous 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:

    The solution to this problem is almost the same as the previous
    problem except you must now do it the Node.js way: asynchronous.

    Instead of `fs.readFileSync()` you will want to use `fs.readFile()`
    and instead of using the return value of this method you need to
    collect the value from a callback function that you pass in as the
    second argument.

    Remember that idiomatic Node.js callbacks normally have the signature:

    function (err, data) { ... }

    so you can check if an error occurred by checking whether the first
    argument is truthy. If there is no error, you should have your
    `Buffer` object as the second argument. As with `readFileSync()`,
    you can supply 'utf8' as the second argument and put the callback as
    the third argument and you will get a `String` instead of a `Buffer`.

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

    var fs = require('fs');
    fs.readFile(process.argv[2],"utf-8",function(err,data){
        if(err) throw err;
        console.log(data.split("
    ").length-1);
    });
  • 相关阅读:
    CentOS 8配置神奇的自动挂载光驱文件夹misc
    Linux预定义通配符及实例
    history历史记录增加显示时间和用户
    CentOS 8 命令行更改系统语言
    使用screen实现多终端同步显示
    在MobaXtrem使用CentOS 上的图形工具
    CentOS中id前10的用户
    添加硬盘后不重启使系统识别新硬盘
    bash默认启用的内部命令
    九、分页查询
  • 原文地址:https://www.cnblogs.com/della/p/3458183.html
Copyright © 2011-2022 走看看