File size: Difference between revisions

19,660 bytes added ,  5 months ago
m
→‎{{header|Wren}}: Changed to Wren S/H
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(20 intermediate revisions by 11 users not shown)
Line 104:
INPUT.TXT: 10 bytes
\INPUT.TXT: 8 bytes</pre>
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program filelen64.s */
 
/************************************/
/* Constantes */
/************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
 
.equ FSTAT, 0x50
 
/************************************/
/* structure de type stat : file infos */
/************************************/
.struct 0
Stat_dev_t: /* ID of device containing file */
.struct Stat_dev_t + 8
Stat_ino_t: /* inode */
.struct Stat_ino_t + 8
Stat_mode_t: /* File type and mode */
.struct Stat_mode_t + 4
Stat_nlink_t: /* Number of hard links */
.struct Stat_nlink_t + 4
Stat_uid_t: /* User ID of owner */
.struct Stat_uid_t + 4
Stat_gid_t: /* Group ID of owner */
.struct Stat_gid_t + 4
Stat_rdev_t: /* Device ID (if special file) */
.struct Stat_rdev_t + 8
Stat_size_deb: /* la taille est sur 8 octets si gros fichiers */
.struct Stat_size_deb + 8
Stat_size_t: /* Total size, in bytes */
.struct Stat_size_t + 8
Stat_blksize_t: /* Block size for filesystem I/O */
.struct Stat_blksize_t + 8
Stat_blkcnt_t: /* Number of 512B blocks allocated */
.struct Stat_blkcnt_t + 8
Stat_atime: /* date et heure fichier */
.struct Stat_atime + 8
Stat_mtime: /* date et heure modif fichier */
.struct Stat_atime + 8
Stat_ctime: /* date et heure creation fichier */
.struct Stat_atime + 8
Stat_End:
 
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessResult: .asciz " File size = "
szCarriageReturn: .asciz "\n"
szMessErrOpen: .asciz "Error open file\n"
szMessErrStat: .asciz "Error stats file\n"
szFileName: .asciz "input.txt"
szFileName1: .asciz "../../../input.txt"
 
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
sBuffer: .skip Stat_End
/*********************************/
/* code section */
/*********************************/
.text
.global main
main:
ldr x0,qAdrszFileName // file name
bl filesize
cmp x0,#0
blt 100f
ldr x1,qAdrsZoneConv
bl conversion10 // call décimal conversion
mov x0,#4
ldr x1,qAdrszFileName
ldr x2,qAdrszMessResult
ldr x3,qAdrsZoneConv // insert conversion in message
ldr x4,qAdrszCarriageReturn
stp x4,x4,[sp,-16]! // save registers
bl displayStrings // display message
add sp,sp,#16 // 1 parameter on stack
ldr x0,qAdrszFileName1 // file name
bl filesize
cmp x0,#0
blt 100f
ldr x1,qAdrsZoneConv
bl conversion10 // call décimal conversion
ldr x0,qAdrsZoneConv
mov x0,#4
ldr x1,qAdrszFileName1
ldr x2,qAdrszMessResult
ldr x3,qAdrsZoneConv // insert conversion in message
ldr x4,qAdrszCarriageReturn
stp x4,x4,[sp,-16]! // save registers
bl displayStrings // display message
add sp,sp,#16 // 1 parameter on stack
100: // standard end of the program
mov x0, #0 // return code
mov x8,EXIT
svc #0 // perform the system call
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrsZoneConv: .quad sZoneConv
qAdrszMessResult: .quad szMessResult
qAdrszFileName: .quad szFileName
qAdrszFileName1: .quad szFileName1
qAdrsBuffer: .quad sBuffer
qAdrszMessErrOpen: .quad szMessErrOpen
qAdrszMessErrStat: .quad szMessErrStat
/***************************************************/
/* display multi strings */
/***************************************************/
/* x0 contains number strings address */
filesize: // INFO: filesize
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
stp x4,x5,[sp,-16]! // save registers
stp x6,x7,[sp,-16]! // save registers
stp x8,x9,[sp,-16]! // save registers
mov x1,x0
mov x0,#AT_FDCWD
mov x2,#O_RDWR // flags
mov x3,#0 // mode
mov x8,#OPEN
svc 0
cmp x0,#0 // error ?
ble 99f
mov x8,x0 // Fd save
ldr x1,qAdrsBuffer // buffer address
mov x8,#FSTAT
svc 0
cmp x0,#0
blt 98f
ldr x1,qAdrsBuffer // buffer address
ldr x4,[x1,#Stat_size_t] // file size
mov x0,x8
mov x8,CLOSE
mov x0,x4 // return size
b 100f
98:
ldr x0,qAdrszMessErrStat
bl affichageMess
mov x0,#-1
b 100f
99:
ldr x0,qAdrszMessErrOpen
bl affichageMess
mov x0,#-1
100:
ldp x8,x9,[sp],16 // restaur registers
ldp x6,x7,[sp],16 // restaur registers
ldp x4,x5,[sp],16 // restaur registers
ldp x2,x3,[sp],16 // restaur registers
ldp x1,lr,[sp],16 // restaur registers
ret
/***************************************************/
/* display multi strings */
/***************************************************/
/* x0 contains number strings address */
/* x1 address string1 */
/* x2 address string2 */
/* x3 address string3 */
/* other address on the stack */
/* thinck to add number other address * 4 to add to the stack */
displayStrings: // INFO: displayStrings
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
stp x4,x5,[sp,-16]! // save registers
add fp,sp,#48 // save paraméters address (6 registers saved * 4 bytes)
mov x4,x0 // save strings number
cmp x4,#0 // 0 string -> end
ble 100f
mov x0,x1 // string 1
bl affichageMess
cmp x4,#1 // number > 1
ble 100f
mov x0,x2
bl affichageMess
cmp x4,#2
ble 100f
mov x0,x3
bl affichageMess
cmp x4,#3
ble 100f
mov x3,#3
sub x2,x4,#4
1: // loop extract address string on stack
ldr x0,[fp,x2,lsl #3]
bl affichageMess
subs x2,x2,#1
bge 1b
100:
ldp x4,x5,[sp],16 // restaur registers
ldp x2,x3,[sp],16 // restaur registers
ldp x1,lr,[sp],16 // restaur registers
ret
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
{{out}}
<pre>
~/.../rosetta/asm3 $ filelen64
input.txt File size = 13
../../../input.txt File size = 6
</pre>
 
=={{header|Action!}}==
Line 216 ⟶ 429:
It is probably much easier to use some an operating system library. This library is not part of
the standard ALGOL 68 language definition.
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang ARM Assembly>
/* ARM assembly Raspberry PI or android with termux */
/* program filelen.s */
 
/* REMARK 1 : this program use routines in a include file
see task Include a file language arm assembly
for the routine affichageMess conversion10
see at end of this program the instruction include */
/* for constantes see task include a file in arm assembly */
/************************************/
/* Constantes */
/************************************/
.include "../constantes.inc"
 
.equ OPEN, 5
.equ NEWFSTAT, 0xc5
 
.equ O_RDWR, 0x0002 @ open for reading and writing
 
/************************************/
/* structure de type stat : file infos */
/************************************/
.struct 0
Stat_dev_t: /* ID of device containing file */
.struct Stat_dev_t + 8
Stat_ino_t: /* inode */
.struct Stat_ino_t + 8
Stat_mode_t: /* File type and mode */
.struct Stat_mode_t + 4
Stat_nlink_t: /* Number of hard links */
.struct Stat_nlink_t + 4
Stat_uid_t: /* User ID of owner */
.struct Stat_uid_t + 4
Stat_gid_t: /* Group ID of owner */
.struct Stat_gid_t + 4
Stat_rdev_t: /* Device ID (if special file) */
.struct Stat_rdev_t + 8
Stat_size_deb: /* la taille est sur 8 octets si gros fichiers */
.struct Stat_size_deb + 8
Stat_size_t: /* Total size, in bytes */
.struct Stat_size_t + 4
Stat_blksize_t: /* Block size for filesystem I/O */
.struct Stat_blksize_t + 4
Stat_blkcnt_t: /* Number of 512B blocks allocated */
.struct Stat_blkcnt_t + 4
Stat_atime: /* date et heure fichier */
.struct Stat_atime + 8
Stat_mtime: /* date et heure modif fichier */
.struct Stat_atime + 8
Stat_ctime: /* date et heure creation fichier */
.struct Stat_atime + 8
Stat_End:
 
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessResult: .asciz " File size = "
szCarriageReturn: .asciz "\n"
szMessErrOpen: .asciz "Error open file\n"
szMessErrStat: .asciz "Error stats file\n"
szFileName: .asciz "input.txt"
szFileName1: .asciz "../../../input.txt"
.align 2
 
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
sBuffer: .skip Stat_End
/*********************************/
/* code section */
/*********************************/
.text
.global main
main:
ldr r0,iAdrszFileName @ file name
bl filesize
cmp r0,#0
blt 100f
ldr r1,iAdrsZoneConv
bl conversion10 @ call décimal conversion
mov r0,#4
ldr r1,iAdrszFileName
ldr r2,iAdrszMessResult
ldr r3,iAdrsZoneConv @ insert conversion in message
ldr r4,iAdrszCarriageReturn
push {r4}
bl displayStrings @ display message
add sp,#4 @ 1 parameter on stack
ldr r0,iAdrszFileName1 @ file name
bl filesize
cmp r0,#0
blt 100f
ldr r1,iAdrsZoneConv
bl conversion10 @ call décimal conversion
mov r0,#4
ldr r1,iAdrszFileName1
ldr r2,iAdrszMessResult
ldr r3,iAdrsZoneConv @ insert conversion in message
ldr r4,iAdrszCarriageReturn
push {r4}
bl displayStrings @ display message
add sp,#4 @ 1 parameter on stack
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
iAdrszCarriageReturn: .int szCarriageReturn
iAdrsZoneConv: .int sZoneConv
iAdrszMessResult: .int szMessResult
iAdrszFileName: .int szFileName
iAdrszFileName1: .int szFileName1
iAdrsBuffer: .int sBuffer
iAdrszMessErrOpen: .int szMessErrOpen
iAdrszMessErrStat: .int szMessErrStat
/***************************************************/
/* display multi strings */
/***************************************************/
/* r0 contains number strings address */
filesize: @ INFO: filesize
push {r1-r8,fp,lr} @ save des registres
mov r1,#O_RDWR @ flags
mov r2,#0 @ mode
mov r7,#OPEN
svc 0
cmp r0,#0 @ error ?
ble 99f
mov r8,r0 @ Fd save
ldr r1,iAdrsBuffer @ buffer address
mov r7,#NEWFSTAT
svc 0
cmp r0,#0
blt 98f
ldr r0,iAdrsBuffer
ldr r1,iAdrsBuffer @ buffer address
ldr r4,[r1,#Stat_size_t] @ file size
mov r0,r8
mov r7,#CLOSE
mov r0,r4 @ return size
b 100f
98:
ldr r0,iAdrszMessErrStat
bl affichageMess
mov r0,#-1
b 100f
99:
ldr r0,iAdrszMessErrOpen
bl affichageMess
mov r0,#-1
100:
pop {r1-r8,fp,pc}
/***************************************************/
/* display multi strings */
/***************************************************/
/* r0 contains number strings address */
/* r1 address string1 */
/* r2 address string2 */
/* r3 address string3 */
/* other address on the stack */
/* thinck to add number other address * 4 to add to the stack */
displayStrings: @ INFO: displayStrings
push {r1-r4,fp,lr} @ save des registres
add fp,sp,#24 @ save paraméters address (6 registers saved * 4 bytes)
mov r4,r0 @ save strings number
cmp r4,#0 @ 0 string -> end
ble 100f
mov r0,r1 @ string 1
bl affichageMess
cmp r4,#1 @ number > 1
ble 100f
mov r0,r2
bl affichageMess
cmp r4,#2
ble 100f
mov r0,r3
bl affichageMess
cmp r4,#3
ble 100f
mov r3,#3
sub r2,r4,#4
1: @ loop extract address string on stack
ldr r0,[fp,r2,lsl #2]
bl affichageMess
subs r2,#1
bge 1b
100:
pop {r1-r4,fp,pc}
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
{{out}}
<pre>
input.txt File size = 8
../../../input.txt File size = 3
</pre>
 
=={{header|Arturo}}==
Line 293 ⟶ 710:
End</syntaxhighlight>
 
=={{header|BaConBASIC}}==
==={{header|BaCon}}===
<syntaxhighlight lang="freebasic">' file size
' Return the entire message, FILELEN returns a NUMBER
Line 798 ⟶ 1,216:
<syntaxhighlight lang="frink">println[newJava["java.io.File", "input.txt"].length[]]
println[newJava["java.io.File", "/input.txt"].length[]]</syntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn DoIt
CFURLRef desktopURL = fn FileManagerURLForDirectory( NSDesktopDirectory, NSUserDomainMask )
CFURLRef url = fn URLByAppendingPathComponent( desktopURL, @"test_file.txt" )
CFDictionaryRef attributes = fn FileManagerAttributesOfItemAtURL( url )
printf @"%@", fn DictionaryObjectForKey( attributes, NSFileSize )
end fn
 
fn DoIt
 
HandleEvents
</syntaxhighlight>
 
=={{header|Gambas}}==
Line 867 ⟶ 1,299:
fsize 'input.txt';'/input.txt'</syntaxhighlight>
 
=={{header|JavaJakt}}==
<syntaxhighlight lang="javajakt">import java.io.File;
fn file_size(filename: String) throws -> i64 {
 
mut result = 0
public class FileSize
mut file = File::open_for_reading(filename)
{
mut buffer = [0u8; 1024] // Size of buffer is arbitrary
public static void main ( String[] args )
while true {
let read_bytes = file.read(buffer)
System.out.println("input.txt : " + new File("input.txt").length() + " bytes");
if read_bytes == 0 {
System.out.println("/input.txt : " + new File("/input.txt").length() + " bytes");
break
}
result += read_bytes as! i64
}
return result
}
 
 
fn main() {
println("{}", file_size(filename: "input.txt"))
println("{}", file_size(filename: "/input.txt"))
}
</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.io.File;
</syntaxhighlight>
<syntaxhighlight lang="java">
public static void main(String[] args) {
File fileA = new File("file.txt");
System.out.printf("%,d B%n", fileA.length());
File fileB = new File("/file.txt");
System.out.printf("%,d B%n", fileB.length());
}
</syntaxhighlight>
<pre>
108 B
108 B
</pre>
 
=={{header|JavaScript}}==
Line 901 ⟶ 1,360:
alert(event);
}</syntaxhighlight>
 
=={{header|Joy}}==
<syntaxhighlight lang="joy">DEFINE filesize == "r" fopen 0 2 fseek pop ftell swap fclose.
 
"input.txt" filesize.
"/input.txt" filesize.</syntaxhighlight>
 
=={{header|jq}}==
Line 906 ⟶ 1,371:
 
jq -Rs length /input.txt</syntaxhighlight>
 
The -R option causes the file to be read as text, and the -s option causes it to be read as a single string.
 
Line 927 ⟶ 1,391:
println("Length of $path is ${File(path).length()} bytes")
}</syntaxhighlight>
 
=={{header|Lang}}==
{{libheader|lang-io-module}}
<syntaxhighlight lang="lang">
# Load the IO module
# Replace "<pathToIO.lm>" with the location where the io.lm Lang module was installed to without "<" and ">"
ln.loadModule(<pathToIO.lm>)
 
$file1 = [[io]]::fp.openFile(input.txt)
fn.println([[io]]::fp.getSize($file1))
[[io]]::fp.closeFile($file1) # Remember to close files
 
$file2 = [[io]]::fp.openFile(/input.txt)
fn.println([[io]]::fp.getSize($file2))
[[io]]::fp.closeFile($file2)
</syntaxhighlight>
 
=={{header|Lasso}}==
Line 998 ⟶ 1,478:
return filesize
end</syntaxhighlight>
 
=={{header|M2000 Interpreter}}==
Version 12 can convert numbers to string if a string exist in expression. Functions or arithmetic expressions must be in parenthesis (see filelen()). Older versions has to use ; (for print only):
<syntaxhighlight lang="m2000 interpreter">
print filename+" has size ";filelen(filename);" bytes"
</syntaxhighlight>
 
<syntaxhighlight lang="m2000 interpreter">
Module ShowFileSize(filename as string) {
if exist(filename) then
print filename+" has size "+(filelen(filename))+" bytes"
else
print filename+ " not exist"
end if
}
ShowFileSize "checkthis.txt"
ShowFileSize "c:\ok.txt"
</syntaxhighlight>
 
=={{header|Maple}}==
Line 1,139 ⟶ 1,637:
printFileSize "/input.txt" ;;</syntaxhighlight>
 
For files greater than Pervasives.max_int, one can use the module [httphttps://camlocaml.inria.fr/pub/docs/manual-ocamlorg/librefapi/PervasivesStdlib.LargeFile.html PervasivesStdlib.LargeFile]:
<syntaxhighlight lang="ocaml">let printLargeFileSize filename =
let ic = open_in filename in
Line 1,151 ⟶ 1,649:
Printf.printf "%d\n" (stat "/input.txt").st_size ;;</syntaxhighlight>
 
The module Unix has also a [httphttps://camlocaml.inria.fr/pub/docs/manual-ocamlorg/librefapi/Unix.LargeFile.html LargeFile sub-module].
 
=={{header|Oforth}}==
Line 1,493 ⟶ 1,991:
 
}</syntaxhighlight>
 
=={{header|S-BASIC}}==
The CP/M operating system -- S-BASIC's native environment --
reports file size as the number of 128-byte records. CP/M also
has no notion of a "root" directory, user area 0 on drive A (the
default on boot-up) being the closest analog. Although S-BASIC has a
built-in SIZE function, it returns the number of blocks
(allocation groups) occupied by the file -- which
varies with the disk format -- and even then
gives the wrong answer if a directory entry controls more than
one 16K logical extent.
<syntaxhighlight = "BASIC">
rem Set the logged drive ('A' to 'P')
procedure setdrive (drive = char)
var hl, de, bc, a_psw = integer
rem -- make sure drive letter is upper case!
if drive >= 'a' then drive = drive - 32
hl = 0
de = drive - 65
bc = 0EH
a_psw = 0
call (5H,hl,de,bc,a_psw)
end
 
rem Set the CP/M user area (0 to 15)
procedure setuser (user = integer)
var hl, bc, a_psw = integer
hl = 0
bc = 20H
a_psw = 0
call (5H,hl,user,bc,a_psw)
end
 
comment
Return size of named file as number of 128-byte records;
assumes file name is upper case. If the file does not
exist, the size will be reported as 0.
end
function fsize(filename = string:20) = integer
var hl, de, bc, a_psw, p = integer
based fname = string:20
based sz = integer
dim byte workfcb(36)
location array de = workfcb
base fname at de
base sz at de + 33
fname = fcb$(filename)
rem See if drive was specified and set FCB accordingly
p = instr(1,filename,":")
if p = 0 then
workfcb(0) = 0
else
workfcb(0) = asc(mid(filename,p-1,1)) - 64
bc = 23H rem BDOS filesize function
call (5,hl,de,bc,a_psw) rem result stored in sz
end = sz
 
rem Exercise the function
 
var filename = string:20
filename = "INPUT.TXT"
rem First check current drive and user
print filename;" occupies";fsize(filename)*128;" bytes"
rem Then check startup directory (A0:)
setdrive 'A'
setuser 0
print "A0:INPUT.TXT occupies";fsize(filename)*128;" bytes"
 
end
</syntaxhighlight>
{{out}}
Although both instances of INPUT.TXT consist of a single line
("The quick brown fox jumps over the lazy red dog") they will
each be reported as occupying a 128-byte record.
<pre>
INPUT.TXT occupies 128 bytes
A0:INPUT.TXT occupies 128 bytes
</pre>
 
 
=={{header|Scala}}==
Line 1,689 ⟶ 2,266:
size1=$(zstat +size input.txt)
size2=$(zstat +size /input.txt)</syntaxhighlight>
 
===Sh builtins only===
 
Tested on Alpine Busybox v1.35.0, passes shellcheck 0.8.0.
 
<syntaxhighlight lang="sh">
#!/bin/sh
unset PATH # No cheating!
 
countbytes(){
size=0
 
# Read the lines in the file
while read -r;do
size=$((size+${#REPLY}+1)) # +1 to account for the newline
done < "$1"
size=$((size+${#REPLY})) # Account for partial lines
 
echo "$size $1"
}
 
countbytes input.txt
countbytes /input.txt
</syntaxhighlight>
 
=={{header|Ursa}}==
Line 1,757 ⟶ 2,358:
Dim root As New IO.FileInfo("\input.txt")
Console.WriteLine(root.Length)</syntaxhighlight>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="Go">
import os
 
fn main() {
paths := ["input.txt", "./input.txt", "non_existing_file.txt"]
for path in paths {
if os.is_file(path) == true {println("The size of '${path}' is ${os.file_size(path)} bytes")}
else {println("Not found: ${path}")}
}
}
</syntaxhighlight>
 
{{out}}
<pre>
The size of 'input.txt' is 22 bytes
The size of './input.txt' is 22 bytes
Not found: non_existing_file.txt
</pre>
 
=={{header|Wren}}==
Line 1,762 ⟶ 2,383:
 
To check the size of a file in the root, just change "input.txt" to "/input.txt" in the following script.
<syntaxhighlight lang="ecmascriptwren">import "io" for File
 
var name = "input.txt"
Line 1,873 ⟶ 2,494:
 
{{omit from|Befunge}} <!-- No filesystem support -->
{{omit from|EasyLang}}
{{omit from|HTML}}
{{omit from|PARI/GP}}
9,476

edits