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
PHP Is Confused When Adding and Concatenating
Replace Last Occurrence of a String in a String
How to Sort a Multidimensional Array by One of the Fields of the Inner Array in PHP
In Where Shall I Use Isset() and !Empty()
Remove All Elements from Array That Do Not Start With a Certain String
How to Use MySQLi_Fetch_Array() Twice
Symfony2 Conceptual Issue: General Bundles Vs. Specific Ones
How to Embed a .Png Image into an HTML Page
How to Relax PHP'S Open_Basedir Restriction
What Are the Differences Between Composer Update and Composer Install
Passing an Array as Arguments, Not an Array, in PHP
Why I'M Getting 500 Error When Using File_Get_Contents(), But Works in a Browser
Why Is Pdo Better For Escaping MySQL Queries/Querystrings Than MySQL_Real_Escape_String
What Are PHP Nested Functions For
How to Split a String by Multiple Delimiters in PHP