How to read a local text file?

前端 未结 20 2252
北恋
北恋 2020-11-21 05:28

I’m trying to write a simple text file reader by creating a function that takes in the file’s path and converts each line of text into a char array, but it’s not working.

20条回答
  •  旧巷少年郎
    2020-11-21 05:52

    I know, I am late at this party. Let me show you what I have got.

    This is a simple reading of text file

    var path = "C:\\path\\filename.txt"
    var fs = require('fs')
    fs.readFile(path , 'utf8', function(err, data) {
      if (err) throw err;
      console.log('OK: ' + filename);
      console.log(data)
    });
    

    I hope this helps.

提交回复
热议问题