-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from pedropark99/12-file-op
Add new chapter about File and I/O operations
- Loading branch information
Showing
55 changed files
with
3,650 additions
and
466 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
./lldb/ | ||
lldb | ||
|
||
foo.txt | ||
|
||
*.o | ||
*.a | ||
|
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
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const std = @import("std"); | ||
|
||
pub fn main() !void { | ||
const cwd = std.fs.cwd(); | ||
const file = try cwd.openFile("foo.txt", .{ .mode = .write_only }); | ||
defer file.close(); | ||
try file.seekFromEnd(0); | ||
var fw = file.writer(); | ||
_ = try fw.writeAll("Some random text to write\n"); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const std = @import("std"); | ||
const stdout = std.io.getStdOut().writer(); | ||
pub fn main() !void { | ||
var file = try std.fs.cwd().openFile("ZigExamples/file-io/lorem.txt", .{}); | ||
defer file.close(); | ||
var buffered = std.io.bufferedReader(file.reader()); | ||
var reader = buffered.reader(); | ||
|
||
var buffer: [1000]u8 = undefined; | ||
@memset(buffer[0..], 0); | ||
|
||
_ = try reader.readUntilDelimiterOrEof(buffer[0..], '\n'); | ||
try stdout.print("{s}\n", .{buffer}); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const std = @import("std"); | ||
|
||
pub fn main() !void { | ||
const cwd = std.fs.cwd(); | ||
try cwd.copyFile("foo.txt", cwd, "ZigExamples/file-io/foo.txt", .{}); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const std = @import("std"); | ||
pub fn main() !void { | ||
const cwd = std.fs.cwd(); | ||
const file = try cwd.createFile("foo.txt", .{}); | ||
file.close(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const std = @import("std"); | ||
const stdout = std.io.getStdOut().writer(); | ||
pub fn main() !void { | ||
const cwd = std.fs.cwd(); | ||
const file = try cwd.createFile("foo.txt", .{ .read = true }); | ||
defer file.close(); | ||
|
||
var fw = file.writer(); | ||
_ = try fw.writeAll("We are going to read this line\n"); | ||
|
||
var buffer: [300]u8 = undefined; | ||
@memset(buffer[0..], 0); | ||
try file.seekTo(0); | ||
var fr = file.reader(); | ||
_ = try fr.readAll(buffer[0..]); | ||
try stdout.print("{s}\n", .{buffer}); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const std = @import("std"); | ||
pub fn main() !void { | ||
const cwd = std.fs.cwd(); | ||
const file = try cwd.createFile("foo.txt", .{}); | ||
defer file.close(); | ||
// Do things with the file ... | ||
var fw = file.writer(); | ||
_ = try fw.writeAll("Writing this line to the file\n"); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const std = @import("std"); | ||
|
||
pub fn main() !void { | ||
const cwd = std.fs.cwd(); | ||
try cwd.makeDir("src"); | ||
try cwd.deleteDir("src"); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const std = @import("std"); | ||
|
||
pub fn main() !void { | ||
const cwd = std.fs.cwd(); | ||
try cwd.deleteFile("foo.txt"); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const std = @import("std"); | ||
const stdout = std.io.getStdOut().writer(); | ||
|
||
pub fn main() !void { | ||
const cwd = std.fs.cwd(); | ||
const dir = try cwd.openDir("ZigExamples/file-io/", .{ .iterate = true }); | ||
var it = dir.iterate(); | ||
while (try it.next()) |entry| { | ||
try stdout.print("File name: {s}\n", .{entry.name}); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tincidunt erat sed nulla ornare, nec aliquet ex laoreet. Ut nec rhoncus nunc. Integer magna metus, ultrices eleifend porttitor ut, finibus ut tortor. Maecenas sapien justo, finibus tincidunt dictum ac, semper et lectus. Vivamus molestie egestas orci ac viverra. Pellentesque nec arcu facilisis, euismod eros eu, sodales nisl. Ut egestas sagittis arcu, in accumsan sapien rhoncus sit amet. Aenean neque lectus, imperdiet ac lobortis a, ullamcorper sed massa. Nullam porttitor porttitor erat nec dapibus. Ut vel dui nec nulla vulputate molestie eget non nunc. Ut commodo luctus ipsum, in finibus libero feugiat eget. Etiam vel ante at urna tincidunt posuere sit amet ut felis. Maecenas finibus suscipit tristique. Donec viverra non sapien id suscipit. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const std = @import("std"); | ||
|
||
pub fn main() !void { | ||
const cwd = std.fs.cwd(); | ||
try cwd.makeDir("src"); | ||
try cwd.makePath("src/decoders/jpg/"); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const std = @import("std"); | ||
const stdout = std.io.getStdOut().writer(); | ||
const stdin = std.io.getStdIn().reader(); | ||
pub fn main() !void { | ||
try stdout.writeAll("Type your name\n"); | ||
var buffer: [20]u8 = undefined; | ||
@memset(buffer[0..], 0); | ||
_ = try stdin.readUntilDelimiterOrEof(buffer[0..], '\n'); | ||
try stdout.print("Your name is: {s}\n", .{buffer}); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const std = @import("std"); | ||
const stdout = std.io.getStdOut().writer(); | ||
pub fn main() !void { | ||
try stdout.print("Hello World!\n", .{}); | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
Oops, something went wrong.