Created
August 1, 2023 14:16
-
-
Save alberic89/68f86c6e3a86f3e2b1e0f13fe5127bea to your computer and use it in GitHub Desktop.
Test for stdin use in Zig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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