Tag: readline

NodeJs的Readline值放入数组中

我正在使用NodeJS readline模块从文本文件中读取。 它将每行都返回到控制台,但是我希望它将返回的行添加到数组。 这是我到目前为止的代码,但它不工作。 我认为rd.on是一个for循环,但它看起来不像。 任何帮助,将不胜感激! function getinfomation2(){ var firstfile = []; var secondfile = []; var countvar = 0; readline = require('readline'); var rd = readline.createInterface({ input: fs.createReadStream('../FixtureProfiles/CFIP1.json'), output: process.stdout, terminal: false }); // Add fixture details from file to array to later be called rd.on('line', function(line) { // if fails console.log(line) returns line […]

readline rl.write如何工作?

我试图parsing一个文件,并用“bazbar”行代替“foobar”。 它只是返回一个空文件。 我不知道我在做什么错,文档不是特别有用。 var readline = require('readline'); var fs = require('fs'); var rl = readline.createInterface({ input: fs.createReadStream('test/in.txt'), output: fs.createWriteStream('test/out.txt', { flags: 'r+' }) }); rl.on('line', function (line) { if (line.match(/foobar/)) { rl.write(line.replace(/foo/, 'baz')); } }); 这是tmp回购,如果有人只是拉动和搞乱它: https : //github.com/corysimmons/css-body-components/tree/master/test

在变换stream中使用Node.js readline

我们有一个巨大的文本文件,我们想要使用stream逐行操作。 有没有在转换stream中使用Node.js readline模块的方法? 比如让整个文本用大写字母(逐行处理)?

Readline纠缠或如何捕捉关键事件,并在一个盒子里面画画

所以我正在使用nodejs readline接口进行实践和学习,我遇到了这个问题。 我在这里做的是: 创build一个readline接口 将光标移到顶部并绘制一个框架 将光标再次移动到顶部并在框架内打印一个时间戳(testing以查看我为每个事件重绘) 将光标移动到帧的末尾,所以当我退出时,我没有得到我的input在缓冲区的中间 听keypress并调用内部时间戳相同的绘制function( 3. ) 这个想法是只绘制绝对必要的,理论上来说,让你这样做。 这一切运作良好,因为我只听方向键我使用这个自定义标准输出function,可以忽略所有其他input。 const Writable = require('stream').Writable; const Readline = require('readline'); //size of frame const MINWIDTH = 120; const MINHEIGHT = 40; //custom stdout to suppress output const customStdout = new Writable({ write: function(chunk, encoding, callback) { if( !this.muted ) { process.stdout.write( chunk, encoding ); } […]

Nodejs readline给出了一些错误

我想逐行阅读一个文件,并根据每行的数据,我必须对数据进行分类。 var rd = readline.createInterface({ input: fs.createReadStream('/home/user/Desktop/text.txt'), output: process.stdout, console: false }); 当这行被执行时,文件被读取并打印在terminal中。 但是当我尝试使用readline逐行读取文件时,出现错误。 rd.on('line', (input) => { console.log(input); }); 我收到以下错误。 `Interface { _sawReturnAt: 0, isCompletionEnabled: true, _sawKeyPress: false, _previousKey: { sequence: '\n', name: 'enter', ctrl: false, meta: false, shift: false }, domain: Domain { domain: null, _events: { error: [Function: debugDomainError] }, _eventsCount: 1, […]

逐行读取完成后执行Node.js

这可能会发生在其他asynchronous函数,但我使用readline作为例子,因为这是我遇到的问题。 我刚刚开始使用JS,目前正试图通过stdin( < )从文件中收集input。 下面的代码就是一个例子: const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, terminal: false }); rl.on('line', foo); function foo(line) { // Code to be iterated by line } function summary() { // Code to be executed after rl finishes } 所以问题是,我在哪里把summaryfunction? rl.on是否在完成后执行另一个执行的callback? 澄清:每当rl读取一行并调用summary时, foo被调用一次,这是在rl完成之后

节点js readline继续评估input并返回一个错误

我正在运行在Linuxterminal上运行节点时Node js readline模块的一个奇怪的行为。 该模块不工作,但它也尝试评估用户input,就像它是一个语句,返回一个参考错误。 这只是我做的一个简单的testing,但不能得到它的工作…我已经尝试了很多教程。 下面是我的代码: 'nodeReadlineTeste':function(){ var readline = require('readline'); //without terminal:false I was getting double lettering on terminal input… var rl = readline.createInterface({input:process.stdin,output:process.stdout,terminal:false}); rl.question('What is your name? ',function(resp){getIt(resp)}); //this function is to bypass NODE assyncronous nature… I was getting the log before even answering. function getIt (resp){console.log(resp); rl.close()}; }, //当运行这个方法并响应我的名字时,我得到这个结果: > global.nodeReadlineTeste() What […]

Readline挂在terminal的第一个input

使用节点版本8.1.0。 阅读readline上的文档: https : //nodejs.org/docs/latest-v8.x/api/readline.html 说明密切的方法,它指出: readline.Interface实例应该被认为是“完成”,一旦“closures”事件发射。 所以自然我会打开一个新的界面,就像我在我的例子中所做的那样。 const readline = require('readline'); function question(question, defaultAnswer) { // Create the interface const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); return new Promise((resolve, reject) => { rl.question(question, (answer) => { // Relinquished control over the input and output streams rl.close(); if (answer.length === 0) { resolve(defaultAnswer); […]

整数不保存在一个循环/部分工作的JS

我是新来的JavaScript,目前正在写一个terminal运行的基本文件。 它的大部分工作。 它允许用户添加信用/删除信用和检查他们的信用。 最后一部分是允许他们购买一种产品,并从总体积分中扣除这一数额。 我所遇到的问题是,虽然它显示购买物品时,金额已被删除。 返回菜单并检查剩余的信用额度后返回原始金额。 我已经尝试使用下面的代码,而不是保存信用(甚至在检查可用信用之后): credit -= readlineSync.keyInSelect(products, 'What product would you like?'); 但是因为数组使用用户input的数字,所以从整体信用中删除该数字而不是实际的价格。 因此,例如,如果用户select选项4,则当实际上糖果是£3时,从信用中移除4。 任何可以给予的帮助将不胜感激,因为我花了相当长的时间没有find解决办法。

使用javascript读取多个input行

我正在尝试使用Node的读线库readline来解决一些algorithm问题。 (其实这是课程提出的)。 input的代码是: var readline = require('readline'); var lineNumber = 0; process.stdin.setEncoding('utf8'); var rl = readline.createInterface({ input: process.stdin, terminal: false }); rl.on('line', readLine); function readLine (line) { // Do some work here. } 问题是计算n个数字的总和。 作为input,我们有:1.在第一行中,我们要计算总和的数字的数量。 2.第二行将包含由空格分隔的“n”个数字。 一个input的例子是:5 1 4 8 7 9这种情况下的输出:25 我怎么能用这个库(我可以用Java,C ++等来做到这一点,但我不知道如何使用JavaScript读取多个input行)