Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
WASM
Zig
Javascript
GIMPLE
Ygen
zig source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
zig 0.10.0
zig 0.11.0
zig 0.12.0
zig 0.12.1
zig 0.13.0
zig 0.2.0
zig 0.3.0
zig 0.4.0
zig 0.5.0
zig 0.6.0
zig 0.7.0
zig 0.7.1
zig 0.8.0
zig 0.9.0
zig trunk
Options
Source code
const std = @import("std"); const File = std.fs.File; const warn = std.debug.warn; const FileOpenError = error { AccessDenied, FileNotFound, }; const AllocationError = error { OutOfMemory, }; pub fn main() anyerror!void { const stdout = try std.io.getStdOut(); warn("Please enter a new title to add in the library:\n"); var buffer: [std.mem.page_size]u8 = undefined; const title = std.io.readLineSlice(buffer[0..]) catch |err| switch (err) { error.OutOfMemory => { try stdout.write("That title is too long!\n"); return err; }, else => return err, }; if (title.len >= 3) { warn("'{}' has been successfully added to the library.\n", title); } else { warn("That title is too short.\n"); } const file = File.openWrite("books.txt") catch |err| switch (err) { error.AccessDenied => { try stdout.write("You do not have access to this file. Please check your permissions.\n"); return err; }, error.FileNotFound => { try stdout.write("The file is not found. Please check your path's access.\n"); return err; }, else => return err, }; defer file.close(); // MUST write into the file in case of success. file.write(buffer[0..title]) catch |err| { warn("Unable to write to the destination file: {}.\n", @errorName(err)); return err; }; }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
About the author
Statistics
Changelog
Version tree