June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -3,28 +3,24 @@ read_line(text &line, text path, integer n)
{
file f;
f_affix(f, path);
f.affix(path);
while (n) {
n -= 1;
f_slip(f);
}
call_n(n, f_slip, f);
f_line(f, line);
f.line(line);
}
integer
main(void)
{
if (2 < argc()) {
text line;
if (1 < argc()) {
text line;
read_line(line, argv(1), 6);
read_line(line, argv(1), 6);
o_text(line);
o_byte('\n');
o_("7th line is:\n", line, "\n");
}
return 0;
0;
}

View file

@ -0,0 +1,7 @@
>> x: pick read/lines %file.txt 7
case [
x = none [print "File has less than seven lines"]
(length? x) = 0 [print "Line 7 is empty"]
(length? x) > 0 [print append "Line seven = " x]
]

View file

@ -0,0 +1,19 @@
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Error;
use std::path::Path;
fn main() {
let path = Path::new("file.txt");
let line_num = 7usize;
let line = get_line_at(&path, line_num - 1);
println!("{}", line.unwrap());
}
fn get_line_at(path: &Path, line_num: usize) -> Result<String, Error> {
let file = File::open(path).expect("File not found or cannot be opened");
let content = BufReader::new(&file);
let mut lines = content.lines();
lines.nth(line_num).expect("No line found at that position")
}

View file

@ -0,0 +1,32 @@
use std::env;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::path::Path;
fn main() {
if env::args().len() <= 1 {
println!("At least a path to a file is needed: No file path given");
return;
} else {
let path = &env::args().nth(1).expect("could not parse the path");
let path = Path::new(&path);
let mut line_num = 1usize;
if let Some(arg) = env::args().nth(2) {
line_num = arg.parse::<usize>().expect("Parsing line number failed");
}
print_line_at(&path, line_num);
}
}
fn print_line_at(path: &Path, line_num: usize) {
if line_num < 1 {
panic!("Line number has to be > 0");
}
let line_num = line_num - 1;
let file = File::open(path).expect("File not found or cannot be opened");
let content = BufReader::new(&file);
let mut lines = content.lines();
let line = lines.nth(line_num).expect("No line found at given position");
println!("{}", line.expect("None line"));
}