0
Q:

read file rust

document = 'document.txt'
file = open(document, 'r')
# 'r' can be replaced with:
# 'w' to write
# 'a' to append (add to the end)
# 'w+' makes a new file if one does not already exist of that name
# 'a+' is the same as 'w+' but it appends if the file does exist

##go to beginning of document
file.seek(0)

##print all lines in document, except empty lines:
for i in file:
    k = i.strip() 
    print k

##close the file after you are done
file.close()


##this can temporarily open a file:
with open(document) as ur:
    for i in ur:
        k = i.strip() 
        print k
3
//this will get a string with all the text from the file
var fileText = File.ReadAllText(@"path\to\my\file.txt");

//this will get all of the lines of the file as an string[]
var fileLines = File.ReadAllLines(@"path\to\my\file.txt");
1
use std::env;
use std::fs;

fn main() {
    // --snip--
    let args: Vec<String> = env::args().collect();

    let query = &args[1];
    let filename = &args[2];

    println!("Searching for {}", query);
    println!("In file {}", filename);

    let contents = fs::read_to_string(filename)
        .expect("Something went wrong reading the file");

    println!("With text:\n{}", contents);
}
0

New to Communities?

Join the community