const std = @import("std");
const stdin = std.io.getStdIn().reader();
//const stdout = std.io.getStdOut().writer();
pub fn main() !void {
var gp = std.heap.GeneralPurposeAllocator(.{}){};
//try stdout.writeAll("Enter max 6 characters: ");
//try stdout.print("Enter max 6 characters: ", .{});
std.debug.print("Enter max 6 characters: ", .{});
const input: []const u8 = blk: {
var buf_array = try std.ArrayList(u8).initCapacity(gp.allocator(), 16);
errdefer buf_array.deinit();
while (true) {
try stdin.readUntilDelimiterArrayList(&buf_array, '\n', 1 << 16);
if (buf_array.items.len <= 6) break;
//try stdout.print("Too many characters!\nTry again: ", .{});
std.debug.print("Too many characters!\nTry again: ", .{});
continue;
}
break :blk buf_array.toOwnedSlice();
};
defer gp.allocator().free(input);
//try stdout.print("You entered: {s}\n", .{input});
std.debug.print("You entered: {s}\n", .{input});
}
#(Compile) errors on windows, not on mac
1 messages · Page 1 of 1 (latest)
Gives me following errors:
C:\Users\me\zig-windows-x86_64-0.11.0-dev.462+af4361f57\lib\std\os\windows.zig:1760:20: error: unable to evaluate comptime expression
.x86_64 => asm volatile (
^~~
C:\Users\me\zig-windows-x86_64-0.11.0-dev.462+af4361f57\lib\std\os\windows.zig:1773:15: note: called from here
return teb().ProcessEnvironmentBlock;
~~~^~
C:\Users\me\zig-windows-x86_64-0.11.0-dev.462+af4361f57\lib\std\io.zig:95:30: note: called from here
return os.windows.peb().ProcessParameters.hStdInput;
~~~~~~~~~~~~~~^~
C:\Users\me\zig-windows-x86_64-0.11.0-dev.462+af4361f57\lib\std\io.zig:109:33: note: called from here
.handle = getStdInHandle(),
~~~~~~~~~~~~~~^~
usrIn.zig:11:30: note: called from here
const stdin = std.io.getStdIn().reader();
C:\Users\me\zig-windows-x86_64-0.11.0-dev.462+af4361f57\lib\std\os\windows.zig:1760:20: error: unable to evaluate comptime expression
.x86_64 => asm volatile (
^~~
C:\Users\me\zig-windows-x86_64-0.11.0-dev.462+af4361f57\lib\std\os\windows.zig:1773:15: note: called from here
return teb().ProcessEnvironmentBlock;
~~~^~
C:\Users\me\zig-windows-x86_64-0.11.0-dev.462+af4361f57\lib\std\io.zig:43:30: note: called from here
return os.windows.peb().ProcessParameters.hStdOutput;
~~~~~~~~~~~~~~^~
C:\Users\me\zig-windows-x86_64-0.11.0-dev.462+af4361f57\lib\std\io.zig:57:34: note: called from here
.handle = getStdOutHandle(),
~~~~~~~~~~~~~~~^~
usrIn.zig:12:32: note: called from here
const stdout = std.io.getStdOut().writer();
Is there some permission I need on windows for reading and writing to terminal?
getStdIn does actual work (on some platforms), so you can't store its result in a global (globals have to be comptime-known, it's like C rather than C++). So you just have to put those definitions inside your main function
That's good to know!
Next error I get, which I didn't get on mac is:
error: expected type '[]const u8', found 'error{OutOfMemory}![]u8'
if (buf_array.items.len <= 6) break;
Doesn't change when I allocate more or less.
-
It should be
defer buf_array.deinit()as the docs for toOwnedSlice() mentionits capacity may or may not be cleared and deinit() is still required to clean up its memory. -
The compile error is from toOwnedSlice() returning
Allocator.Error![]const u8while the input block expects only[]const u8. You should unwrap the error withbreak :blk try buf_array.toOwnedSlice()
Right on with break :blk try!
You ment defer buf_array.deinit() in stead of errdefer buf_array.deinit() I take it. Yes, makes sense then.