File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11use std:: fs:: File ;
22use std:: io:: Write ;
3+ use std:: io:: Read ;
34
45fn main ( ) -> Result < ( ) , std:: io:: Error > {
56 let string_name_file = "exampleFile.txt" ;
@@ -11,21 +12,14 @@ fn main() -> Result<(), std::io::Error> {
1112 // Write string
1213 file. write_all ( string_data_file. as_bytes ( ) ) ?;
1314
15+ // Read data from file
16+ let mut content = String :: new ( ) ;
17+ let mut input_file = File :: open ( string_name_file) ?;
18+ input_file. read_to_string ( & mut content) ?;
19+
1420 // Print message
1521 println ! ( "File {} has been written succesfully" , string_name_file) ;
16- Ok ( ( ) )
17- }
22+ println ! ( "File content: {}" , content) ;
1823
19- /*fn main() -> Result<(), std::io::Error> {
20-
21- let file_path = "example.txt"; // файл для записи
22- let text = String::from("Hello METANIT.COM!"); // текст для записи
23- let mut output_file = File::create(file_path)?; // создаем файл
24- output_file.write_all(text.as_bytes())?; // записываем в файл текст
25-
26- let mut content = String::new(); // строка для считывания файла
27- let mut input_file = File::open(file_path)?; // открываем файл
28- input_file.read_to_string(&mut content)?; // считываем содержимое файла в строку
29- println!("File content: {}", content); // выводим считанный текст на консоль
3024 Ok ( ( ) )
31- }*/
25+ }
You can’t perform that action at this time.
0 commit comments