Write Array which has also strings to file

 

Hello,

I have researched about the possibility to write an array to a file in the forum and the documentation,

and I have tried a number of possibilities.

However, I have the impression that it does not work with an Array, which also contains strings.

In my case, I have an array which is based on a structure, and has around 10 fields, some of them strings.

I just wanted to check if this is a misunderstanding from my side?

is there no possibility to do such a thing?

Thanks a lot in advance!

 
KjLNi: I have researched about the possibility to write an array to a file in the forum and the documentation, and I have tried a number of possibilities. However, I have the impression that it does not work with an Array, which also contains strings. In my case, I have an array which is based on a structure, and has around 10 fields, some of them strings. I just wanted to check if this is a misunderstanding from my side? is there no possibility to do such a thing? Thanks a lot in advance!

It is not built-in, but you can write your own function to write the data to file in binary form, as long as you also store the string length to the file as well.

You will also have to create your function to read it back in by reading the string length first and using that to read the string itself.

You can also use object oriented programming to have a class for each data type with methods for serializing the data, to facilitate reading and writing to a file.

 
Fernando Carreiro #:

It is not built-in, but you can write your own function to write the data to file in binary form, as long as you also store the string length to the file as well.

You will also have to create your function to read it back in by reading the string length first and using that to read the string itself.

You can also use object oriented programming to have a class for each data type with methods for serializing the data, to facilitate reading and writing to a file.

Hello, thanks for your reply.

At least it confirms that I have made my research!

However, being a self-taught programmer, I am afraid that I would not know how to do it.

I have to admit that it seems a bit strange to me that this type of thing is not easier to achieve?

I would assume that I am not the first person with this requirement?

however, thanks a lot for your response!

 
KjLNi #:

Hello, thanks for your reply.

At least it confirms that I have made my research!

However, being a self-taught programmer, I am afraid that I would not know how to do it.

I have to admit that it seems a bit strange to me that this type of thing is not easier to achieve?

I would assume that I am not the first person with this requirement?

however, thanks a lot for your response!

The link below, has a sections that would help in explaining writing an array or structure to file. https://www.mql5.com/en/articles/2720

MQL5 Programming Basics: Files
MQL5 Programming Basics: Files
  • www.mql5.com
This practice-oriented article focuses on working with files in MQL5. It offers a number of simple tasks allowing you to grasp the basics and hone your skills.
 
KjLNi #: Hello, thanks for your reply. At least it confirms that I have made my research! However, being a self-taught programmer, I am afraid that I would not know how to do it. I have to admit that it seems a bit strange to me that this type of thing is not easier to achieve? I would assume that I am not the first person with this requirement? however, thanks a lot for your response!

For those learning MQL first, it may seem strange that such functions don't already exist, but MQL is modelled around C and C++, and for those who learned those languages first, it is common to have already learned how to write strings and structures to file in binary form, so they don't find it at all strange.

 
Fernando Carreiro #:

For those learning MQL first, it may seem strange that such functions don't already exist, but MQL is modelled around C and C++, and for those who learned those languages first, it is common to have already learned how to write strings and structures to file in binary form, so they don't find it at all strange.

Hello there, 

sure, I understand.

Just as an update, I have managed to write my Array to a file, based on the instructions in the article about "files".

The only thing is that it all seems to be formatted as text now.

So I just have to transform it in an additional step using excel or numbers, and then I can export it as CSV for further processing.

And I will study this work around "binary" a bit, which is in fact new to me ...

Best regards!

 
KjLNi #: sure, I understand. Just as an update, I have managed to write my Array to a file, based on the instructions in the article about "files". The only thing is that it all seems to be formatted as text now.

So I just have to transform it in an additional step using excel or numbers, and then I can export it as CSV for further processing. And I will study this work around "binary" a bit, which is in fact new to me ...

If you are exporting to CSV then you should not be using binary formats, only textual format.
Reason: