Read Text File

Read Text File

Medium NodeJS File System & Paths 25 views
Explanation Complexity

Problem Statement

Read a file path from stdin and print its contents.

Input Format

stdin: one file path.

Output Format

Print file contents.

Example

./data.txt
(file text)

Constraints

If file missing, print 'NOT FOUND'.

Input / Output Format

Input Format
stdin: one file path.
Output Format
Print file contents.
Constraints
If file missing, print 'NOT FOUND'.

Examples

Input:
./data.txt
Output:
(file text)

Example Solution (Public)

NodeJS
const fs = require('fs');
const p = fs.readFileSync(0, 'utf8').trim();
if (!p) process.exit(0);
try {
  const txt = fs.readFileSync(p, 'utf8');
  process.stdout.write(txt);
} catch (e) {
  console.log('NOT FOUND');
}

Official Solution Code

const fs = require('fs');
const p = fs.readFileSync(0, 'utf8').trim();
if (!p) process.exit(0);
try {
  const txt = fs.readFileSync(p, 'utf8');
  process.stdout.write(txt);
} catch (e) {
  console.log('NOT FOUND');
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.