0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Rust でテキストファイルのレコードを更新

Last updated at Posted at 2020-07-11

こちらで作成したテキストファイルのレコードを更新します。
Rust でテキストファイルの作成

プロジェクトの作成

cargo new text_update
$ cargo new text_update
     Created binary (application) `text_update` package

作成されたフォルダー構造

$ tree text_update/
text_update/
├── Cargo.toml
└── src
    └── main.rs

コードの作成

cd text_update
mv src/main.rs  src/text_update.rs

フォルダー構造

$ tree
.
├── Cargo.toml
└── src
    └── text_update.rs
Cargo.toml
[package]
name = "text_update"
version = "0.1.0"
edition = "2021"

#

[[bin]]
name = "text_update"
path = "src/text_update.rs"

[dependencies]
chrono = "0.4"
src/text_update.rs
// --------------------------------------------------------------------
/*
    src/text_update.rs

                        Jan/18/2024
*/
// --------------------------------------------------------------------
use std::env;
use std::error;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::collections::HashMap;
use std::io::{Write};

use chrono::{Local, DateTime};
// --------------------------------------------------------------------
fn text_read_proc(fname_in: String) -> HashMap<String,HashMap<String,String>>{
    let mut dict_aa = HashMap::new();
    let file = File::open(fname_in).expect("file not found.");

    for line in BufReader::new(file).lines() {
        let line = line.unwrap();
        let vvv: Vec<&str> = line.trim ().split_terminator('\t').collect();
        let mut unit_aa:HashMap <String,String> = HashMap::new ();
        unit_aa.insert("name".to_string(), vvv[1].to_string());
        unit_aa.insert("population".to_string(), vvv[2].to_string());
        unit_aa.insert("date_mod".to_string(), vvv[3].to_string());

        dict_aa.insert(vvv[0].to_string(), unit_aa);
    }

    dict_aa
}

// --------------------------------------------------------------------
fn text_write_proc (fname_in: String, dict_aa: HashMap<String,HashMap<String,String>>) {
    let mut file = File::create(fname_in).expect("file not found.");

    for (key,value) in dict_aa.iter() {
        let str_out = key.to_string () + "\t" + &value["name"]
            + "\t" + &value["population"]
            + "\t" + &value["date_mod"] + "\n";
        print!("{}", str_out);
        file.write (String::from(str_out).as_bytes()).expect("cannot write.");
    }

}

// --------------------------------------------------------------------
fn main () -> Result<(), Box<dyn error::Error>> {
    eprintln!("*** 開始 ***");

    let args: Vec<_> = env::args().collect();

    let ref fname_in = args[1];
    let key_in = &args[2];
    let population_in = &args[3];

    println! ("{}",fname_in);
    println! ("{}",key_in);
    println! ("{}",population_in);


    let mut dict_aa = text_read_proc(fname_in.to_string());
// ------------------

    let unit_bb = &dict_aa[key_in];

    println! ("{:?}", unit_bb);
    let name_bb = &unit_bb["name"];
    println! ("{}", name_bb);

    let mut unit_cc:HashMap<String, String> = HashMap::new();
    unit_cc.insert("name".to_string(),name_bb.to_string());
    unit_cc.insert("population".to_string(),population_in.to_string());

    let date_mod: DateTime<Local> = Local::now();
    println!("{}", date_mod);

    unit_cc.insert("date_mod".to_string(),date_mod.to_string());

    dict_aa.insert(key_in.to_string(),unit_cc);

// ------------------
    text_write_proc (fname_in.to_string(),dict_aa);

    eprintln!("*** 終了 ***");

    Ok(())
}

// --------------------------------------------------------------------

コンパイル

carogo build

実行

cargo run /var/tmp/plain_text/cities.txt t2382  723

ディスクの使用量

$ du --max-depth=2 -BM
1M	./.git/hooks
1M	./.git/refs
1M	./.git/objects
1M	./.git/info
1M	./.git
1M	./src
46M	./target/debug
46M	./target
46M	.

再生できるファイルの削除

cargo clean

ディスクの使用量

$ du --max-depth=2 -BM
1M	./.git/hooks
1M	./.git/refs
1M	./.git/objects
1M	./.git/info
1M	./.git
1M	./src
1M	.

確認したバージョン

$ cargo --version
cargo 1.75.0
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?