How to resolve the algorithm Read a specific line from a file step by step in the Rust programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Read a specific line from a file step by step in the Rust programming language

Table of Contents

Problem Statement

Some languages have special semantics for obtaining a known line number from a file.

Demonstrate how to obtain the contents of a specific line within a file. For the purpose of this task demonstrate how the contents of the seventh line of a file can be obtained,   and store it in a variable or in memory   (for potential future use within the program if the code were to become embedded). If the file does not contain seven lines,   or the seventh line is empty,   or too big to be retrieved,   output an appropriate message. If no special semantics are available for obtaining the required line,   it is permissible to read line by line. Note that empty lines are considered and should still be counted. Also note that for functional languages or languages without variables or storage,   it is permissible to output the extracted data to standard output.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Read a specific line from a file step by step in the Rust programming language

Source code in the rust programming language

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")
}


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"));
}


  

You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the Pascal programming language
You may also check:How to resolve the algorithm Web scraping step by step in the Lasso programming language
You may also check:How to resolve the algorithm Define a primitive data type step by step in the Lua programming language
You may also check:How to resolve the algorithm Hello world/Graphical step by step in the Stata programming language
You may also check:How to resolve the algorithm String append step by step in the BASIC programming language