Write array to CSV file issue

 
I am trying to overwrite a .csv file. I open the file, read the contents, calculate the result, then overwrite the line.

(for some context, this file will have 10 lines, with from 4 to 25 doubles, the file is read into an array[10][25], processed then written back to the file ready for the next iteration)

As an example, I start with 10,10,10,10 in the file.

I do the calculations.

When I write the file, It write a new line for each answer.

10

78.29

10

12.57

I want to output all to one line, overwriting the old numbers with the new ones each time.

10,78.29,10,12.57

I will then do the same for the next line. I read the file using FileReadString() into a 2D array. No problem.

I,ve tried all sorts of variations and combinations, strings double array[], all types of FileWrite , just cant get it to work.

The one thing I know is that there is an elegant solution. I just haven't found it yet.

Any help will be much appreciated, and thank you for all the times I've used the forum.

             double tmp,var = 0;
             string var_array[4];
             int jj;
             int qq;

             Handle_9 = FileOpen("Feature_Scores.csv",FILE_CSV|FILE_WRITE, ',');
             for (row = 0; row < 1; row++)
               {         
               for (col = 0; col < 4; col++)
                  {
                    var = buy_feature_scores[row][col];
                    if (GetQuad() == qq){tmp = NormalizeDouble(var*1.05*1.33*1*1*1*0.9,2);} else tmp = var;
                    qq++;                   
                    var_array[jj] = tmp;
                    jj++; 
                    Print(qq + "  " + tmp);          
                  }  
                  FileWriteArray(Handle_9,var_array,0,WHOLE_ARRAY);                                  
              }
 

Do not double post!

I have deleted your duplicate topic.

 
Robin:

I deleted just after I posted it when I realized it was in the wrong place.

 
Robin:

I deleted just after I posted it when I realized it was in the wrong place.

Apparently you didn't, I wouldn't have been able to delete it 15 minutes later if you had.

 
Keith Watford:

Apparently you didn't, I wouldn't have been able to delete it 20 minutes later if you had.

Thanks, I thought I had deleted my post, are you talking about my post or the topic? Still learning,  I must have created a new topic when I wanted to create a new post.

 
Please look here.
Documentation on MQL5: File Functions / FileSeek
Documentation on MQL5: File Functions / FileSeek
  • www.mql5.com
//|                                                Demo_FileSeek.mq5 | //|                        Copyright 2013, MetaQuotes Software Corp. | //|                                              https://www.mql5.com | //| Script program start function                                    |...
 
Marco vd Heijden:
Please look here.

Thanks for your reply Marco. I have read pages and pages of documentation, tried all sorts of combinations described in the documentation and on posts on the forum. FileSeek takes me to the start, end or some place inbetween. I want to overwrite the line of the file. If I use FILE_WRITE|FILE_READ then I cant overwrite I can only append as far as I understand it.

 

I don't know how big your file is but if it's not too big you can also read in the entire file then modify / add or remove what you want and overwrite the entire file in one go.

 
Marco vd Heijden:

I don't know how big your file is but if it's not too big you can also read in the entire file then modify / add or remove what you want and overwrite the entire file in one go.

Marco,


That is kind of the intention, I just don't know how. The data must be stored in a file as it gets used to make trading decisions and gets updated after each new trade, power failure, system closing down, whaterver wont be an issue.

The file is only 5 lines x 25 numbers max (all double) It might increase to 10 line, but no more.

Each line needs to be processed separately, I increment one number from each line (see calculation in code above) then I replace the whole line. (this is a score, one of the 4 numbers gets increased or decreased after analysing the last order closed) Then I go to the next line and do the same for the relevant feature, in the example above its GetQuad().

There are two issues, the first relates to my question above, I get output one number per line, instead of all 4 numbers on the same line separated by  a comma.

Sounds simple enough, but every solution in one part seems to introduce another issue.

Reading the file is no problem.

             int Handle_9=FileOpen("Feature_Scores.csv",FILE_CSV|FILE_READ, ',');      
             for (int row = 0; row < 5; row++)
               {         
               for (int col = 0; col < 24; col++)
                  {
                    buy_feature_scores[row][col] = FileReadString(Handle_9);          
                  }                                                   
               }
               FileClose(Handle_9);
 
  1. Robin:
    I am trying to overwrite a .csv file. I open the file, read the contents, calculate the result, then overwrite the line.
    The one thing I know is that there is an elegant solution. I just haven't found it yet.
                 Handle_9 = FileOpen("Feature_Scores.csv",FILE_CSV|FILE_WRITE, ',');
                 ⋮
                      FileWriteArray(Handle_9,var_array,0,WHOLE_ARRAY);             
    

    Perhaps you should read the manual. FileWriteArray only works in binary files.

  2. Robin: When I write the file, It write a new line for each answer.

    Just FileWrite(var_array[0],var_array[1],var_array[2],var_array[3]) // one line/record

  3. Robin: FileSeek takes me to the start, end or some place inbetween. I want to overwrite the line of the file. If I use FILE_WRITE|FILE_READ then I cant overwrite I can only append as far as I understand it.
    Seek won't work on text files because the records are variable sizes. To overwrite you must read in the entire file, modify whatever and write the entire file.
 
William Roeder:
  1. Perhaps you should read the manual. FileWriteArray only works in binary files.

  2. Just FileWrite(var_array[0],var_array[1],var_array[2],var_array[3]) // one line/record

  3. Seek won't work on text files because the records are variable sizes. To overwrite you must read in the entire file, modify whatever and write the entire file.

William, thanks for your reply. I managed to get it working using a string, but I will give the array another try.

string var_write = qd0_0+","+qd0_1+","+qd0_2.... and so on. Also had to pad out some  with zero's  because not all lines are 24 numbers long, so it was reading from another line.

Looking at it now, the array is an elegant solution. Thank you.

Reason: