Download MetaTrader 5

Update CSV File

To add comments, please log in or register
domalu
26
domalu  

Hello Everybody,

I am new to mql4 programming and trying to learn how to read and update csv file. Can somebody please help me out with this

Lets say I have a csv file with sample lines like this

1, A, Y

2,B, Y

3,C,Y

My requirement is to update Y to Z for all the rows, not sure how FileWrite() function can be used for this.

Thanks.

Ex Ovo Omnia
3261
Ex Ovo Omnia  
domalu:

Hello Everybody,

I am new to mql4 programming and trying to learn how to read and update csv file. Can somebody please help me out with this

Lets say I have a csv file with sample lines like this

1, A, Y

2,B, Y

3,C,Y

My requirement is to update Y to Z for all the rows, not sure how FileWrite() function can be used for this.

Thanks.

Not sure how intelligently MQL treats CSV files, but I would avoid editing it and build a brand new file with the new content instead. Anyway, I would avoid CSV files in general.

Carl Schreiber
6989
Carl Schreiber  

To keep it simple I just write string files and add line by line:

      string row=StringFormat("%s;%.5f;%.3f;%i;%i; ...\n",
                              iName,doub1,doub2,int1,int2,...);

      int fH = FileOpen(fName,FILE_READ|FILE_WRITE);
      if( fH == INVALID_HANDLE ) Print("ERROR open ",fName,": ",_LastError);
      else {
         FileSeek(fH,0,SEEK_END);
         FileWriteString(fH,row,StringLen(row));
         FileClose(fH);
      }

Can be perfectly opened by LibreOffice or even another EA: split file into lines and split each line into items.

whroeder1
15331
whroeder1  

CSV files are variable length records (as opposed to binary files) They can't be individually updated because of the changing lengths (would create a gap or clobber the next line.)

Read the lines (or variables of the lines) in to an array, then create a new file with new values.

domalu
26
domalu  
Ovo:

Not sure how intelligently MQL treats CSV files, but I would avoid editing it and build a brand new file with the new content instead. Anyway, I would avoid CSV files in general.

Ovo, What would you recommend substituting csv files with ? Currently one of the other programs is placing/creating the csv file, which the EA would be reading from.

Thanks.

domalu
26
domalu  
gooly:

To keep it simple I just write string files and add line by line:

Can be perfectly opened by LibreOffice or even another EA: split file into lines and split each line into items.

Gooly,

I tried something like that before posting

if (data[count][2] == "Y")
         {
           data[count][2]="Z";
          FileWrite(m_handle,data[count][0]+","+data[count][1]+","+data[count][2]);
          }

however this just creates a new entry/record, under the current one. Is there a way where I can delete the old entry/record ? If we can do that, should solve the problem at hand.

Thanks for your response.

domalu
26
domalu  
WHRoeder:

CSV files are variable length (as opposed to binary files) They can't be individually updated because of the changing lengths (would create a gap or clobber the next line.)

Read the lines (or variables of the lines) in to an array, then create a new file with new values.

WHRoeder,

Considering you are referencing records, in my case the size of each record will always be fixed length.

Thanks.

Ex Ovo Omnia
3261
Ex Ovo Omnia  
domalu:

Ovo, What would you recommend substituting csv files with ? Currently one of the other programs is placing/creating the csv file, which the EA would be reading from.

Thanks.

If you need to export or import data, then it is probably the only way. But if you want to save some data and edit them directly in the file, structures with a binary file are easier and safer to use.
Carl Schreiber
6989
Carl Schreiber  
domalu:

Gooly,

I tried something like that before posting

if (data[count][2] == "Y")
         {
           data[count][2]="Z";
          FileWrite(m_handle,data[count][0]+","+data[count][1]+","+data[count][2]);
          }

however this just creates a new entry/record, under the current one. Is there a way where I can delete the old entry/record ? If we can do that, should solve the problem at hand.

Thanks for your response.


Either move the File-Pointer - if you know exactly whereto - very tricky and error-prone I guess -

or (I would) read the entire file into your 2-dim. array change some of the data and re-write the entire csv-file.

whroeder1
15331
whroeder1  
domalu: Considering you are referencing records, in my case the size of each record will always be fixed length.
1, A, Y
2,B, Y
3,C,Y
  1. Your first field is an int. Will that always be [0..9]? Otherwise it is not fixed length.
  2. Your record is three fields, not fixed length
domalu
26
domalu  
gooly:

Either move the File-Pointer - if you know exactly whereto - very tricky and error-prone I guess -

or (I would) read the entire file into your 2-dim. array change some of the data and re-write the entire csv-file.

>>or (I would) read the entire file into your 2-dim. array change some of the data and re-write the entire csv-file.

Can you please give me a sample code for this.

Thanks.

12
To add comments, please log in or register