This is rust language. Inside of file text is following, this is a test this onl
ID: 2247684 • Letter: T
Question
This is rust language.
Inside of file text is following,
this is a test
this only a test
The goal of a program will be to print the nth character of a given file.
This will give us with handling program arguments, working with strings and slices, working with files, and converting strings to integers.
This code can only check the 4th character, but I want to check any nth character with input.
Your program will take two command line arguments. Your program can be compiled and run with the following commands:
The first program argument is a filename, and the second argument is just a number. You program will read the file and print the nnth character in the file (indexed by 0), where nn is the second argument (5, in this case).
Here is code,
use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
fn main() {
// Create a path to the desired file
let path = Path::new("input_file.txt");
let display = path.display();
// Open the path in read-only mode, returns `io::Result`
let mut file = match File::open(&path) {
// The `description` method of `io::Error` returns a string that describes the error
Err(why) => panic!("couldn't open {}: {}", display,
why.description()),
Ok(file) => file,
};
// Read the file contents into a string, returns `io::Result`
let mut s = String::new();
match file.read_to_string(&mut s) {
Err(why) => panic!("couldn't read {}: {}", display,
why.description()),
Ok(_) => print!("{} contains: {}", display, s),
}
let mut count = 0; //Initialized
let nthcharacter = 4; //Check the 4th character
for c in s.chars() {
count += 1;
if count==nthcharacter {
println!(" {}th character is: {}", nthcharacter, c);
}
}
}
Explanation / Answer
Solution :
As your program taking command line arguments, you need to write a code to access command line arguments and get the 2nd argument from it-
Same I have coded for you :
So modified program is below :
Program to read nth character from a file :
use std::error::Error;
use std::fs::File;
use std::env; // For accessing command line arguments
use std::io::prelude::*;
use std::path::Path;
fn main() {
// Create a path to the desired file
let path = Path::new("input_file.txt");
let display = path.display();
// Open the path in read-only mode, returns `io::Result`
let mut file = match File::open(&path) {
// The `description` method of `io::Error` returns a string that describes the error
Err(why) => panic!("couldn't open {}: {}", display,
why.description()),
Ok(file) => file,
};
// Read the file contents into a string, returns `io::Result`
let mut s = String::new();
match file.read_to_string(&mut s) {
Err(why) => panic!("couldn't read {}: {}", display,
why.description()),
Ok(_) => print!("{} contains: {}", display, s),
}
let mut count = 0; //Initialized
// Added code to read command line arguments
let args: Vec<_> = env::args().collect();
let nthcharacter = args[1] // Reads 2nd command line argument which is nth character
for c in s.chars() {
count += 1;
if count==nthcharacter {
println!(" {}th character is: {}", nthcharacter, c);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.