Reading a file to get its content in a String
is a simple operation that is often used in a project. Although this is not complex to do, sooner or later you often have to implement such operation.
Easy reading and writing files with Ada Utility Library
By Stephane Carrez2020-08-09 20:49:00
The Ada Utility Library provides several simple operations that simplify the reading and writing of files through a single procedure call. These operations are not new since I've implemented most of them 10 years ago!!!
Reading a file line by line by using Ada.Text_IO
is quite easy but annoying due to the fact that you have to open the file, iterate over the content getting one line at a time and then closing the file. To simplify this process, the Util.Files package of Ada Utility Library provides a simple Read_File
procedure that uses a procedure as parameter and that procedure will be called for each line that is read.
with Util.Files;
procedure Read_Line (Line : in String) is ...;
Util.Files.Read_File (Path => "file.txt",
Process => Read_Line'Access);
Another Read_File
procedure allows to read the file and get its content in a vector of strings so that once the file is read the vector contains each line. Yet, another Read_File
procedure reads the file content in an Unbounded_String
. You will use that later form as follows:
with Util.Files;
with Ada.Strings.Unbounded;
Content : Ada.Strings.Unbounded.Unbounded_String;
Util.Files.Read_File (Path => "file.txt",
Into => Content);
Very often it is also necessary to write some content in a file. Again, this is easy to do but a simple Write_File
procedure that takes the file path and the content to write is easier to use in several cases:
with Util.Files;
Util.Files.Write_File (Path => "file.txt",
Content => "something");
The Ada Utility Library contains other useful operations and features that have helped me in implementing various projects such as Ada Web Application and Ada Keystore. Have a look at the Ada Utility Library Programmer's Guide!
Tags
- Facelet
- NetBSD
- framework
- Mysql
- generator
- files
- application
- gcc
- ReadyNAS
- Security
- binutils
- ELF
- JSF
- Java
- bacula
- Tutorial
- Apache
- COFF
- collaboration
- planning
- project
- upgrade
- AWA
- C
- EL
- J2EE
- UML
- php
- symfony
- Ethernet
- Ada
- FreeBSD
- Go
- KVM
- MDE
- Proxy
- STM32
- Servlet
- backup
- lvm
- multiprocessing
- web
- Bean
- Jenkins
- release
- OAuth
- ProjectBar
- REST
- Rewrite
- Sqlite
- Storage
- USB
- Ubuntu
- bison
- cache
- crash
- Linux
- firefox
- performance
- interview
Add a comment
To add a comment, you must be connected. Login