Reading files and modifying them

Hello,

I'm trying to work out the best/most efficient way to scan/iterate over a directory and read all its (text) files. The text files will be save files for a project I'm working on and they will contain various pieces of information.

Is there a way to read these files and store them as Strings, or arrays, etc. and then be able to perform operations (such as comparing one save file to the other)?

I'd appreciate any help at all. I seem to get stuck in various muddles when trying to design this. Thanks in advance.

What type of file formats?

  1. For common txt case, see std::fs::File.
  2. In case of JSON for example, checkout serde_json (more general serde).
  3. For CSV.
  4. For other types, search in crates.io

They will be generic .txt files with just plain text contained.

I am able to read a file as a string without any problem but I would like to be able to iterate over files in a directory and store them all (as strings, or an array, or something). That's largely where my problem is.

Thank you for your links.

Are the files known to be ASCII, in which case you could read them as [u8], or do you need UTF-8 to process them because they might contain non-ASCII characters?

use std::fs::read_dir to iterate over directory.
Then std::fs::File::open, opened file implements std::io::Read trait where you can use methods read_to_end or read_to_string.
If you need to traverse directories recursively there is good crate walkdir

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.