mirror of
https://github.com/ziglang/zig.git
synced 2026-02-13 04:48:20 +00:00
std.io.readLine function
this provides a better input for guess number example. see #882
This commit is contained in:
parent
2e5115b068
commit
4eb68987d8
@ -9,8 +9,6 @@ pub fn main() !void {
|
||||
var stdout_file_stream = io.FileOutStream.init(&stdout_file);
|
||||
const stdout = &stdout_file_stream.stream;
|
||||
|
||||
var stdin_file = try io.getStdIn();
|
||||
|
||||
try stdout.print("Welcome to the Guess Number Game in Zig.\n");
|
||||
|
||||
var seed_bytes: [@sizeOf(u64)]u8 = undefined;
|
||||
@ -27,12 +25,15 @@ pub fn main() !void {
|
||||
try stdout.print("\nGuess a number between 1 and 100: ");
|
||||
var line_buf : [20]u8 = undefined;
|
||||
|
||||
const line_len = stdin_file.read(line_buf[0..]) catch |err| {
|
||||
try stdout.print("Unable to read from stdin: {}\n", @errorName(err));
|
||||
return err;
|
||||
const line_len = io.readLine(line_buf[0..]) catch |err| switch (err) {
|
||||
error.InputTooLong => {
|
||||
try stdout.print("Input too long.\n");
|
||||
continue;
|
||||
},
|
||||
error.EndOfFile, error.StdInUnavailable => return err,
|
||||
};
|
||||
|
||||
const guess = fmt.parseUnsigned(u8, line_buf[0..line_len - 1], 10) catch {
|
||||
const guess = fmt.parseUnsigned(u8, line_buf[0..line_len], 10) catch {
|
||||
try stdout.print("Invalid number.\n");
|
||||
continue;
|
||||
};
|
||||
|
||||
17
std/io.zig
17
std/io.zig
@ -478,3 +478,20 @@ test "import io tests" {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn readLine(buf: []u8) !usize {
|
||||
var stdin = getStdIn() catch return error.StdInUnavailable;
|
||||
var adapter = FileInStream.init(&stdin);
|
||||
var stream = &adapter.stream;
|
||||
var index: usize = 0;
|
||||
while (true) {
|
||||
const byte = stream.readByte() catch return error.EndOfFile;
|
||||
switch (byte) {
|
||||
'\n' => return index,
|
||||
else => {
|
||||
if (index == buf.len) return error.InputTooLong;
|
||||
buf[index] = byte;
|
||||
index += 1;
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user