ab ovo
This commit is contained in:
56
examples/simple_file_test.zig
Normal file
56
examples/simple_file_test.zig
Normal file
@@ -0,0 +1,56 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub fn main() !void {
|
||||
const allocator = std.heap.page_allocator;
|
||||
|
||||
// Create test file
|
||||
const test_file_path = "./tests/test_data/simple_test.txt";
|
||||
const content = "Hello World from simple test!\n";
|
||||
|
||||
std.debug.print("Creating file: {s}\n", .{test_file_path});
|
||||
{
|
||||
const f = try std.fs.cwd().createFile(test_file_path, .{});
|
||||
defer f.close();
|
||||
try f.writeAll(content);
|
||||
}
|
||||
std.debug.print("Created file with {} bytes\n", .{content.len});
|
||||
|
||||
// Now read it back via cwd
|
||||
std.debug.print("\nReading via cwd.openFile...\n", .{});
|
||||
{
|
||||
const f = try std.fs.cwd().openFile(test_file_path, .{});
|
||||
defer f.close();
|
||||
const end_pos = try f.getEndPos();
|
||||
std.debug.print(" end_pos: {}\n", .{end_pos});
|
||||
|
||||
const buf = try allocator.alloc(u8, 100);
|
||||
defer allocator.free(buf);
|
||||
const bytes_read = try f.readAll(buf);
|
||||
std.debug.print(" bytes_read: {}\n", .{bytes_read});
|
||||
std.debug.print(" content: '{s}'\n", .{buf[0..bytes_read]});
|
||||
}
|
||||
|
||||
// Get absolute path
|
||||
var path_buf: [std.fs.max_path_bytes]u8 = undefined;
|
||||
const abs_path = try std.fs.cwd().realpath(test_file_path, &path_buf);
|
||||
std.debug.print("\nAbsolute path: {s}\n", .{abs_path});
|
||||
|
||||
// Read via absolute path
|
||||
std.debug.print("\nReading via openFileAbsolute...\n", .{});
|
||||
{
|
||||
const f = try std.fs.openFileAbsolute(abs_path, .{});
|
||||
defer f.close();
|
||||
const end_pos = try f.getEndPos();
|
||||
std.debug.print(" end_pos: {}\n", .{end_pos});
|
||||
|
||||
const buf = try allocator.alloc(u8, 100);
|
||||
defer allocator.free(buf);
|
||||
const bytes_read = try f.readAll(buf);
|
||||
std.debug.print(" bytes_read: {}\n", .{bytes_read});
|
||||
std.debug.print(" content: '{s}'\n", .{buf[0..bytes_read]});
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
try std.fs.cwd().deleteFile(test_file_path);
|
||||
std.debug.print("\nTest completed successfully!\n", .{});
|
||||
}
|
||||
Reference in New Issue
Block a user