How to read data from a CSV file into an EA??? Nothing seems to work for me...

 

Hi guys, I realise this question has been covered before, many times it seems.... but please, forgive me, I simply cannot find anything that works for me. I'm beginning to pull my hair out with this. I must be missing something very basic....

I have a CSV file, with a single column of data, say 12 values.... I want to read in a value (say the first value) from this file and assign it to an integer. A few hours later, I want to update this integer value with the second value in the CSV, and so on.

To do this I've been using:

handle=FileOpen(Input_File, FILE_CSV | FILE_READ,", ");

VAL = FileReadNumber(handle);

FileClose(handle);

...

But this only returns the first value in the file, and does not step down through the file when I want to read the next value... makes sense.

So I updated the code to:

handle=FileOpen(Input_File, FILE_CSV | FILE_READ,", ");

FileSeek(handle, n, SEEK_SET);

VAL = FileReadNumber(handle);

FileClose(handle);

...

where n increments upwards when I am reading values in. But this returns some strange values, presumably values of the specific byte where I have offset it to from the start of the file.

So my question is, how to I read in the NEXT value from the CSV file? Is there a way I can tell the pointer to go to the next 'line' and return the value there? Incidentally, I guess it's important that I mention that some of the

integer values are single digit, some 2 digit, 3 digit, and the rest 4 digit, so presumably reading in a string & substring would not work as the length of the string changes from value to value.

Any help or advice would be appreciated.... and thanks in advance. I've spent about 6 hours today ready posts and different threads, and nothing seems to work for me. I also tried assigning all of the values to an array of size [12] but it kept returning zero.

In case it makes any difference, I have been testing this by reading in the values from the CSV file and then trying to export the data to another (output) CSV file, so the code I have been using is basically:

handle=FileOpen(Input_File, FILE_CSV | FILE_READ,", ");

FileSeek(handle, n, SEEK_SET);

VAL = FileReadNumber(handle);

FileClose(handle);

...

handle=FileOpen(Output_File, FILE_CSV | FILE_WRITE | FILE_READ,", ");

FileSeek(handle, 0, SEEK_END);

FileWrite(handle, VAL);

FileClose(handle);

Would this make any difference?

Cheers folks,

FB.

 

The only way I seem to be able to get this working is by using FileSize to read the size of the file, and then using FileSeek to position the pointer to the next location where I know there's data.

So for instance, if I know that each integer uses 3 bytes, I use the following:

handle_in=FileOpen(Input_File,FILE_CSV|FILE_READ,",");

size = FileSize(handle_in);

pos = pos + size/3;

FileSeek(handle_in, pos, SEEK_SET);

iVAL = FileReadNumber(handle_in);

FileClose(handle_in);

This works ok, but I need to change the divisor (3 in this case) as the numbers in the input file get bigger right? Surely there is an easier way to do this!?

Also, as fbj states here (https://www.mql5.com/en/forum/118975) my delimiter should automatically mark the end of the line in my CSV file, so on the next iteration it should read from the next field.... but it doesn't. If I use

this approach (as per my first post) my output file simply contains the very first value of my input file, over and over again.

I have tried Phy's approach to a similar example (https://www.mql5.com/en/forum/105476/page2) but it doesn't work either for me, so I'm clearly doing something wrong. Any help would be greatly appreciated.

Thanks,

FB

 

Remember the EA gets called every tick. So file operations in the Start() must be complete - i.e. Open - Write - Close.

Writing more values to the file you must first seek the end of the file before writing.

 

Hi Ickyrus, the writing of the file seems to be working fine, no problems with it - I have been writing to CSV files from EAs without any problem. However I simply cannot read data from a CSV, at least no useful data... I wrote a very basic EA to make sure I was able to read correctly from a CSV file, simply by importing data from one CSV file, and then dumping it to another. The code I have at the moment looks like this:


int iVAL;

....

int start()

{

int handle_in, handle_out;

int i_Minute = TimeMinute(TimeCurrent());

Input_File = "Input.csv";

Output_File = "Output.csv";

if (i_Minute != i_Minute_Prev)

{

handle_in=FileOpen(Input_File,FILE_CSV|FILE_READ,";");

iVAL = FileReadNumber(handle_in);

FileClose(handle_in);

handle_out=FileOpen(Output_File, FILE_CSV | FILE_WRITE | FILE_READ,";");

FileSeek(handle_out, 0, SEEK_END);

FileWrite(handle_out, iVAL, size);

FileClose(handle_out);

}

i_Minute_Prev = i_Minute;

}

The above does not work, it simply outputs the first value from the CSV file, again and again.

I think I might have realised my mistake though - do I need to put the loop above into a FOR or WHILE loop so that it continues to read in the data from the file before executing the entire EA? Does that make sense?

FB

 

Sorted it - I used the example as show here https://www.mql5.com/en/forum/124096

So I guess I'm correct in saying that you must use a while loop when accessing ALL of the data in a CSV file.

 
good luck
 

  1. handle_in=FileOpen(Input_File,FILE_CSV|FILE_READ,";");
    FileOpen takes three parameters. The third one is an int not a string. Use ';' not ";"



 
Hi Dave 
Did you solved the problem? I have the same interests 
Thanks 
 
  1. Do you really expect that they are still waiting for a solution after six (6) years? Don't resurrect old threads without a very good reason. A lot has changed since Build 600 and Higher. 20 14.02.03
  2. Is your file ANSI or Unicode encoded. That option didn't exist back then.
Reason: