Logo

Programming-Idioms

  • JS
  • Kotlin
  • Rust
  • C++

Idiom #95 Get file size

Assign to variable x the length (number of bytes) of the local file at path.

#include <fstream>
std::ifstream f(path, std::ios::in | std::ios::binary);
f.seekg(0, std::ios::end);
size_t x = f.tellg();
const {readFileSync: read} = require ('fs')
let x = read(path).length

Only works with NodeJS (server-side JavaScript) because the browser isn't allowed to access your files.
use std::path::Path;
let x = path.metadata()?.len();

path has type &std::path::Path
use std::fs;
use std::io;
use std::os::linux::fs::MetadataExt;
let x = fs::metadata(path)?.st_size();

Populated from stat
use std::fs;
let x = fs::metadata(path)?.len();
with Ada.Directories; use Ada.Directories;
X : constant File_Size := Size (Path);

New implementation...
< >
programming-idioms.org