Skip to content

Latest commit

 

History

History
44 lines (32 loc) · 1.15 KB

File metadata and controls

44 lines (32 loc) · 1.15 KB

fread(filePath, length)

This function reads a specified number of bytes from an open file pointer. On success, it returns the data as a string; on failure, it returns false. It is frequently used to read raw binary data or fixed-length chunks from files.

Alias: fread()

Parameters:

Example

const fs = require('fsmate');

const file = 'example.txt';

// With Promises:
// Read 50 character from 'file'
fs.fread(file, 50)
.then(data => {
  console.log(data);
}).catch(err => {
  console.log(err);
});

// With async/await:
async function fread(filePath, length) {
  try {
    const data = await fs.fread(filePath, length);
    console.log(data);
  } catch(err) {
    console.log(err);
  }
}

// Read full content
fread(file, fs.filesizeSync(file));

For the synchronous version, see: freadSync