HELP COPYING FILE TO ANOTHER FILE

 

Hi,

I want to copy from "AdjustFile.txt" to "TransferAdjustFile.txt" one record at a time

The content of the "AdjustFile.txt" is as follows:

AdjustFile.txt

But I am getting the following content for "TransferAdjustFile.txt"

TransferAdjustFile.txt


I used the following codes:

      string Tra[], TraRec;
      int AdjustHandle=FileOpen("AdjustFile.txt",FILE_READ|FILE_WRITE|FILE_CSV|FILE_TXT|FILE_ANSI);
      int TransferAdjustHandle=FileOpen("TransferAdjustFile.txt",FILE_READ|FILE_WRITE|FILE_CSV|FILE_TXT);
      for (int ID = 0; ID <= 4; ID++)
        {
         TraRec = FileReadString(AdjustHandle);StringSplit(TraRec,';', Tra); 
         FileWrite(TransferAdjustHandle, Tra[0] ,Tra[1], Tra[2], Tra[3], Tra[4]);
        }
      FileClose(AdjustHandle);
      FileClose(TransferAdjustHandle);

What could be the problem? Thanks in advance.

 

Hello friend,

Does your code really do what you say it does?

 
GrumpyDuckMan:

Hello friend,

Does your code really do what you say it does?

No. It does not do what I want it to do?

 

MQL5 Reference :

     FileReadString ... When reading from a csv-file, the string length isn't required also, the string will be read from the current position till the nearest delimiter or till the text string end character.


This means the for loop only reads the first 5 words of the first line. But it looks like you wanted to read the whole file, line by line.

If you drop FILE_CSV from the file flags it should work much better.

 
lippmaje:

MQL5 Reference :

     FileReadString ... When reading from a csv-file, the string length isn't required also, the string will be read from the current position till the nearest delimiter or till the text string end character.


This means the for loop only reads the first 5 words of the first line. But it looks like you wanted to read the whole file, line by line.

If you drop FILE_CSV from the file flags it should work much better.

Thanks.  Another question is how I can insert a record to overwrite  an exsting record? Also, how can I delete a record in a file?
 
lippmaje:

MQL5 Reference :

     FileReadString ... When reading from a csv-file, the string length isn't required also, the string will be read from the current position till the nearest delimiter or till the text string end character.


This means the for loop only reads the first 5 words of the first line. But it looks like you wanted to read the whole file, line by line.

If you drop FILE_CSV from the file flags it should work much better.

But I am doing both reading and writing using one FileOpen command, and I understand that opening with FILE_CSV while writing removes delimiters (;) from the file even if specified in the code. And like you said, reading with FILE_CSV reads one cell at a time and not one record row at a time. So how do I go about it?
 
macpee:
But I am doing both reading and writing using one FileOpen command, and I understand that opening with FILE_CSV while writing removes delimiters (;) from the file even if specified in the code. And like you said, reading with FILE_CSV reads one cell at a time and not one record row at a time. So how do I go about it?
      string Tra[], TraRec;
      //int AdjustHandle=FileOpen("AdjustFile.txt",FILE_READ|FILE_WRITE|FILE_CSV|FILE_TXT|FILE_ANSI);
      int AdjustHandle=FileOpen("AdjustFile.txt",FILE_READ|FILE_TXT|FILE_ANSI);
      //int TransferAdjustHandle=FileOpen("TransferAdjustFile.txt",FILE_READ|FILE_WRITE|FILE_CSV|FILE_TXT);
      int TransferAdjustHandle=FileOpen("TransferAdjustFile.txt",FILE_WRITE|FILE_CSV|FILE_TXT);
      for (int ID = 0; ID <= 4; ID++)
        {
         TraRec = FileReadString(AdjustHandle);StringSplit(TraRec,';', Tra); 
         FileWrite(TransferAdjustHandle, Tra[0] ,Tra[1], Tra[2], Tra[3], Tra[4]);
        }
      FileClose(AdjustHandle);
      FileClose(TransferAdjustHandle);
Just a guess. Only change are the file flags. Give it a try.
Reason: