Skip to content

Instantly share code, notes, and snippets.

@alberic89
Created August 1, 2023 14:16
Show Gist options
  • Save alberic89/68f86c6e3a86f3e2b1e0f13fe5127bea to your computer and use it in GitHub Desktop.
Save alberic89/68f86c6e3a86f3e2b1e0f13fe5127bea to your computer and use it in GitHub Desktop.
Test for stdin use in Zig
const std = @import("std");
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
const stdin = std.io.getStdIn();
defer stdin.close();
const reader = stdin.reader();
const max_input = 1024;
try stdout.print("What is your name? ", .{});
var input_buffer: [max_input]u8 = undefined;
var input_str = (try reader.readUntilDelimiterOrEof(input_buffer[0..], '\n')) orelse {
// No input, probably CTRL-d (EOF). Print a newline and exit!
try stdout.print("\n", .{});
return;
};
try stdout.print("Hello {s}!\n", .{input_str});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment