Am I missing something or does node.js's standard file I/O module lack analogs of the usual file random access methods?
seek()
/fseek()
tell()
/ftell()
How does one read random fixed-size records from large files in node without these?
Am I missing something or does node.js's standard file I/O module lack analogs of the usual file random access methods?
seek()
/ fseek()
tell()
/ ftell()
How does one read random fixed-size records from large files in node without these?
tell
is not, but it is pretty rare to not already know the position you are at in a file, or to not have a way to keep track yourself.
seek
is exposed indirectly via the position
argument of fs.read
and fs.write
. When given, the argument will seek to that location before performing its operation, and if null
, it will use whatever previous position it had.
position
parameter because in my reading of the docs I managed to conflate it with the offset
parameter, which is the offset from the start of the buffer you want to read data into. –
Harrie fs.read
due to the nature of Buffer
and the need to convert such to encoded text such as UTF-8. The obvious ways result in broken characters since UTF-8 includes multibytes characters. ReadableStream
handles multibyte characters, but does not allow one to seek freely. I'm not sure there's any library which lets you combine random access and line reading. –
Harrie node doesn't have these built in, the closest you can get is to use fs.createReadStream
with a start
parameter to start reading from an offset, (pass in an existing fd
to avoid re-opening the file).
http://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options
createReadStream()
again? The path
is mandatory while the fd
is just optional and the docs are not clear. –
Harrie null
for path, even though it says it's required? otherwise i'd just pass in the same path. –
Vaccinia path
is ignorned no matter what it is if you also supply the optional fd
. This doesn't seem to be guaranteed explicitly in the documentation or stated so in the source. Also I can find nothing authoritative by searching the forums. I am loathe to rely on a behaviour that's not documented even if backed up by common sense. I might find a way to ask officially. Thank you yiding. –
Harrie I suppose that createReadStream creates new file descriptor over and over. I prefer sync version:
function FileBuffer(path) {
const fd = fs.openSync(path, 'r');
function slice(start, end) {
const chunkSize = end - start;
const buffer = new Buffer(chunkSize);
fs.readSync(fd, buffer, 0, chunkSize, start);
return buffer;
}
function close() {
fs.close(fd);
}
return {
slice,
close
}
}
Use this:
fs.open(path, flags[, mode], callback)
Then this:
fs.read(fd, buffer, offset, length, position, callback)
Read this for details:
https://nodejs.org/api/fs.html#fs_fs_read_fd_buffer_offset_length_position_callback
© 2022 - 2024 — McMap. All rights reserved.
tell
are mostly when I'm reading text in platform-agnostic code line-by-line which could have lines ending in either\n
,\r
(no longer common), or\r\n
. It's certainly still possible to track position withouttell
though. – Harrie