FileWriteString mystery wrong data written

 

I am writing to a CSV file and for some reason it is writing the wrong information. The string to be written contains data such as 0.59;0.37;0.04 but instead 0.00;0.00;0.00 is written. I confirmed during debugging that the correct information is contained in the string right before it is written. Stranger still, during debugging I paused the write operation just after the first data line was written. That line in the file contained the correct data but when I continued the run that first line (like all the others) contained 0.00;0.00;0.00 as noted above.

Any clues as to why this would happen? 

The file is opened once in OnTick (it is closed in OnDeinit):

fileHandle=FileOpen(fileName,FILE_CSV|FILE_READ|FILE_WRITE,';');

The writing is done as follows:

  if(fileHandle!=INVALID_HANDLE)
  {
        FileSeek(fileHandle,0,SEEK_END);
        FileWriteString(fileHandle,dataLine,StringLen(dataLine));
  }
  else
  {
                PrintFormat("Failed to open %s file, Error code = %d",filePath,GetLastError());
    return;
  }
  
  return;
 

Hi!

Open the file in OnInit.

 
eddie:

Open the file in OnInit.

@eddie. That has nothing to do with the case at hand.

rwbta:

... The string to be written contains data such as 0.59;0.37;0.04 but instead 0.00;0.00;0.00 is written ...

@rwbta: You have not included the code that generates the "dataLine" so we are unable to comment on the situation. I tested the code you have provided and it does not present any problems with the data you gave as example. Also, I suggest you code and supply a complete example code that can be compiled and tested as a whole in order to verify the problem.

 
FMIC:

@eddie. That has nothing to do with the case at hand.

@rwbta: You have not included the code that generates the "dataLine" so we are unable to comment on the situation. I tested the code you have provided and it does not present any problems with the data you gave as example. Also, I suggest you code and supply a complete example code that can be compiled and tested as a whole in order to verify the problem.

@FMIC The code that generates the data is proprietary and cannot be released. The dataLine code is a simple concatenation similar to this: sDataLine=sPropVar1+";"+sPropVar2+";"+sPropVar3+"\r\n";

I understand your desire to test the code however that would be of no use. I do not need the problem to be verified; I have personally seen it occurring. As I mentioned previously the variable dataLine contains the correct values having been verified during debugging with a watch; when the first line is written; the correct info is in the file. By the time the run is completed the data is all zeros. Thank you for your efforts.

If no one else is experiencing problems with FileWriteString and FileWrite (it happens with both functions), then I will have to resolve it myself.

I have attached a simple EA to just write to a file the same data over and over again. It changed this "0.59;0.37;0.04" into this "0.00;0.00;0.00". I am using Build 971 and ran the EA in Strategy Tester from 2016.04.01 to 2016.05.31.

Update: I just changed the delimiter from ";"  to "," and it saved the info in the test EA correctly; unfortunately the problem persists in the original EA even with the delimiter change

 

eddie:

Hi!

Open the file in OnInit.

@eddie: Thank you for the suggestion but I already tried that; it made no difference but I appreciate your input

 

Files:
 

Please read the example of FileOpen in the editor's reference!

If you open a csv-file like you do just write the values to the file - or open a text file and write strings into it:

      int fH = FileOpen(fName,FILE_READ|FILE_WRITE);
      FileSeek(fH,0,SEEK_END); 
      FileWriteString(fH, row, StringLen(row) );
      FileClose(fH);
 
rwbta:

@FMIC The code that generates the data is proprietary and cannot be released. The dataLine code is a simple concatenation similar to this: sDataLine=sPropVar1+";"+sPropVar2+";"+sPropVar3+"\r\n";

I understand your desire to test the code however that would be of no use. I do not need the problem to be verified; I have personally seen it occurring. As I mentioned previously the variable dataLine contains the correct values having been verified during debugging with a watch; when the first line is written; the correct info is in the file. By the time the run is completed the data is all zeros. Thank you for your efforts.

If no one else is experiencing problems with FileWriteString and FileWrite (it happens with both functions), then I will have to resolve it myself.

I have attached a simple EA to just write to a file the same data over and over again. It changed this "0.59;0.37;0.04" into this "0.00;0.00;0.00". I am using Build 971 and ran the EA in Strategy Tester from 2016.04.01 to 2016.05.31.

Update: I just changed the delimiter from ";"  to "," and it saved the info in the test EA correctly; unfortunately the problem persists in the original EA even with the delimiter change

I DID NOT SAY for you to supply your original code. I said (and I quote), "code and supply a complete example" in order for us to test it properly.

However, it seems you DID do that via the "filewritetest,mq4" file, which I tested and it worked correctly (see attachment, which was renamed to ".txt" so that the site would except it).

The question I have for you, is HOW are you looking at the resulting file? Are you opening it with a simple text editor or are you opening it directly via a spreadsheet application such as Excel?

I suspect that you are opening it directly in an spreadsheet application. So, first, open it via a text editor/viewer to make sure it has the correct output, because the spreadsheet app may be applying incorrect conversions.

Files:
 
FMIC: HOW are you looking at the resulting file?
Also WHERE are you looking for the file? MQL4/files or tester/files? Data Structure in MetaTrader 4 Build 600 and Higher - MQL4 Articles 17.02.2014
 
FMIC:

I DID NOT SAY for you to supply your original code. I said (and I quote), "code and supply a complete example" in order for us to test it properly.

However, it seems you DID do that via the "filewritetest,mq4" file, which I tested and it worked correctly (see attachment, which was renamed to ".txt" so that the site would except it).

The question I have for you, is HOW are you looking at the resulting file? Are you opening it with a simple text editor or are you opening it directly via a spreadsheet application such as Excel?

I suspect that you are opening it directly in an spreadsheet application. So, first, open it via a text editor/viewer to make sure it has the correct output, because the spreadsheet app may be applying incorrect conversions.

No need to shout; after rereading your reply, I decided to try the test EA which I uploaded.

While it may work for you, the test EA did not work for me until after I changed the delimiter from ';' to ','. However, that had no effect on the original EA. Apparently, something is different on your system.

I looked at the files in Notepad, Notepad++ and then in OpenOffice Calc. The values to which I referred were incorrectly changed by the time the backtest completed.

 
WHRoeder:
FMIC: HOW are you looking at the resulting file?
Also WHERE are you looking for the file? MQL4/files or tester/files? Data Structure in MetaTrader 4 Build 600 and Higher - MQL4 Articles 17.02.2014
When using Strategy Tester I look at the files in tester\files. When debugging, I look in MQL4\Files
 
gooly:

Please read the example of FileOpen in the editor's reference!

If you open a csv-file like you do just write the values to the file - or open a text file and write strings into it:


Thanks for your suggestion. The code in the test EA is just one of several different methods I have tried. Btw, your suggested code sample did not resolve the problem either.

Actually, I did read the documentation; I've been reading it for years to stay up to date with changes. I have previously successfully written to and read from numerous different csv files many times. This problem is new to me.

Apparently, no one else is experiencing this problem so it must be something on my system.

Thank you to everyone who has offered suggestions. I will try to resolve this issue on my own. 

Reason: