Read Each Line of Txt File to New Array Element

node.js: read a text file into an array. (Each line an item in the array.)

If you can fit the final data into an array then wouldn't you also be able to fit it in a string and split it, as has been suggested?
In any case if you would like to process the file one line at a time you can also try something like this:

var fs = require('fs');

function readLines(input, func) {
var remaining = '';

input.on('data', function(data) {
remaining += data;
var index = remaining.indexOf('\n');
while (index > -1) {
var line = remaining.substring(0, index);
remaining = remaining.substring(index + 1);
func(line);
index = remaining.indexOf('\n');
}
});

input.on('end', function() {
if (remaining.length > 0) {
func(remaining);
}
});
}

function func(data) {
console.log('Line: ' + data);
}

var input = fs.createReadStream('lines.txt');
readLines(input, func);

EDIT: (in response to comment by phopkins) I think (at least in newer versions) substring does not copy data but creates a special SlicedString object (from a quick glance at the v8 source code). In any case here is a modification that avoids the mentioned substring (tested on a file several megabytes worth of "All work and no play makes Jack a dull boy"):

function readLines(input, func) {
var remaining = '';

input.on('data', function(data) {
remaining += data;
var index = remaining.indexOf('\n');
var last = 0;
while (index > -1) {
var line = remaining.substring(last, index);
last = index + 1;
func(line);
index = remaining.indexOf('\n', last);
}

remaining = remaining.substring(last);
});

input.on('end', function() {
if (remaining.length > 0) {
func(remaining);
}
});
}

Java: How do you read each line of a text file and setting each line as an array element?

I would use an ArrayList... like so...

ArrayList<String> questions = new ArrayList<String>();


/// in your loop...
while((line = reader.readLine()) != null){
questions.add(line);
}

By the way, as jlordo points out in the comment, the way you've structured your method, the only way out of your loop is for line to be null, so, by the time you get to your return statement you're returning null. What are you actually trying to return here? If it's the entire file of lines you need to be adding them to a String as you go.

Read each line of txt file to new array element and open links php

There is a php function called file() that read all the lines of a file into an array, so removing your need to make an array from a newline delimited string read with file_get_contents()

$lines = file('name.txt');

// Loop through our array, and build (n) cURL connections
foreach ($lines as $line_num => $line) {
// Do your cURL stuff in here
}

Reading from txt file each line as Array

You can simply read the file back again, and use Split on the delimiter you have chosen

        var textLines = File.ReadAllLines("");

foreach (var line in textLines)
{
string[] dataArray = line.Split(',');
}

file() fails to read each line in txt file into new array element

The file probably uses line endings other than \n. Turn on auto detection first:

ini_set('auto_detect_line_endings', true);
$lines = file('file.txt');
print_r($lines);

If desired, and you have access you can also set this in the php.ini.

C++ Read txt and put each line into Dynamic Array

Don't use arrays; use std::vector. The std::vector behaves like an array and uses Dynamic Memory:

std::string s;
std::vector<std::string> database;
while (std::getline(input, s))
{
database.push_back(s);
}

Keep it simple. :-)



Related Topics



Leave a reply



Submit