This language bar is your friend. Select your favorite languages!

Idiom #95 Get file size

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

def file_size(path)
  "stat -f%z #{path}"
end
x = File.size(path)
#include <stdio.h>
FILE *f = fopen(path, "rb");
fseek(f, 0, SEEK_END);
int x = ftell(f);
#include <fstream>
std::ifstream f(path, std::ios::in | std::ios::binary);
f.seekg(0, std::ios::end);
size_t x = f.tellg();
import std.file: getSize;
auto x = getSize(path);
import "os"
f, err := os.Open(path)
defer f.Close()
if err != nil {
	return err
}
info, err := f.Stat()
if err != nil {
	return err
}
x := info.Size()
import System.IO
fileSize :: FilePath -> IO Integer
fileSize path = withFile path ReadMode hFileSize
const {readFileSync: read} = require ('fs')
let x = read(path).length
import java.io.File;
long x = new File(path).length();
$x = filesize($path);
uses fileutil;
x := filesize(path);
my $x = -s $path;
import os
x = os.path.getsize(path)
use std::fs;
let x = fs::metadata(path)?.len();

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org