Reading and writing bin and xml files
In this example, we will compare reading files in bin and xml format in Engee and MATLAB.
Let's start with binary files (bin). They contain a raw data structure, which makes them difficult to interpret without special tools. Such files consist of a stream of bytes that can represent any data, be it text, images, audio or video. This data is stored in binary form, which requires special processing to extract the necessary information.
The main features of working with binary files are as follows.
- Data format: In binary files, data is represented by a sequence of bytes, where each byte stores one unit of information.
- Data Interpretation: Understanding the contents of a binary file requires knowledge of its internal structure. Without knowledge of the data structure, the program will not be able to interpret the file correctly.
- Data is accessed by reading and writing individual bytes or blocks of bytes. The program must know how the interpreter will process this data.
Let's create a binary file for reading.
Pkg.add(["EzXML"])
open("file.bin", "w") do io
write(io, Int32(12)) # Запишем целое число типа Int32
write(io, Float64(3.14)) # Запишем вещественное число типа Float64
write(io, "Test\n") # Запишем строку
end
Reading a binary file in Engee:
open("file.bin", "r") do io
num_int = read(io, Int32) # Считаем целое число типа Int32
num_float = read(io, Float64) # Считаем вещественное число типа Float64
str = String(read(io)) # Считаем оставшуюся часть файла как строку
println("Целое число: ", num_int)
println("Вещественное число: ", num_float)
print("Прочитанная строка: ", str)
end
Reading a binary file in MATLAB:
using MATLAB
mat"""
fileID = fopen('file.bin', 'rb');
num_int = fread(fileID, 1, 'int32');
num_float = fread(fileID, 1, 'double');
str = fscanf(fileID, '%c');
fclose(fileID);
"""
mat"""fprintf('Прочитанные целые числа: ');
disp(num_int);"""
mat"""fprintf('Прочитанные вещественные числа: ');
disp(num_float);"""
mat"""disp(['Прочитанная строка: ', str]);"""
Although the tests show that the code turned out to be the same type, the differences between the languages are manifested in the fact that the syntax of Engee is simpler and more intuitive. In addition, data types in Engee are easier to understand because there is no concept of "double".
Now let's move on to XML files (xml). They are designed to store structured data in the form of a tree. Each element of the XML file has its own tag, which defines the type of data contained in this element. This makes it easier to work with such files, as programs can analyze the file structure and extract the necessary data.
The main features of working with XML files are as follows.
- Data structure: An XML file is a tree in which each element (tag) contains information about the data type and structure.
- Hierarchical structure: An XML file organizes data in a tree structure, where each element has a parent and descendants.
- Markup: The XML file includes markup that defines how the data is organized. This simplifies data processing, and the program knows how to handle each element.
The easiest way to read such files is to use a lowercase representation. For detailed analysis of such files, Engee has the EzXML library. And MATLAB uses the Java API to work with XML, since there are no built-in tools for this.
Pkg.add("EzXML")
using EzXML
using EzXML
doc = parsexml("""
<primates>
<genus name="Homo">
<species name="sapiens">Human</species>
</genus>
<genus name="Pan">
<species name="paniscus">Bonobo</species>
<species name="troglodytes">Chimpanzee</species>
</genus>
</primates>
""")
# Сохраняем документ в файл
write("file.xml", doc)
Reading an XML file in Engee:
open("file.xml", "r") do io
str = String(read(io))
println(str)
end
Reading an XML file in MATLAB:
mat"""
fileID = fopen('file.xml', 'rb');
disp(fscanf(fileID, '%c'));
"""
Conclusion
In this example, we have studied how binary files and XML documents are written and read in both programming languages. As can be seen from this analysis, the syntactic and functional advantages in this direction are on the side of Engee.