About writing a string on a csv file...

 

Hi all,

I try to save a string to a csv file. However, I couldn't do that. I search it on forum although I get an aswer, I couldn't do that again.

My code is as follows;

   int filehandle=FileOpen("MQL4\\Files\\updated_Q.csv",FILE_WRITE|FILE_CSV); 
   string str="Hello!";

   Print("filehandle=", filehandle);

   if(filehandle>0)
   {
    FileWriteString(filehandle, str);
    FileClose(filehandle);  
    Print("OK!");
   }

When I run it, I get filehandle=1 and OK!. However, there are anything about Hello! in my updated_Q.csv document.

I understand that the file is opened because filehandle=1 and I get OK!. However, I couldn't write on the csv.

What should I do?
Best,

Murat Y.

 
Murat Yazici:

Hi all,

I try to save a string to a csv file. However, I couldn't do that. I search it on forum although I get an aswer, I couldn't do that again.

My code is as follows;

When I run it, I get filehandle=1 and OK!. However, there are anything about Hello! in my updated_Q.csv document.

I understand that the file is opened because filehandle=1 and I get OK!. However, I couldn't write on the csv.

What should I do?
Best,

Murat Y.

Your snippet works properly but if you want to write your file into MQL4\Files directory (not into MQL4\Files\MQL4\Files) you should use this:
int filehandle=FileOpen("updated_Q.csv",FILE_WRITE|FILE_CSV);
 
Petr Nosek:
Your snippet works properly but if you want to write your file into MQL4\Files directory (not into MQL4\Files\MQL4\Files) you should use this:
Thank you, Petr! However, my document is empty! Before close the document in the code, should I save it? However, I couldn't find any filesave function. Maybe, the problem is to save it before closing.
 
Murat Yazici:
Thank you, Petr! However, my document is empty! Before close the document in the code, should I save it? However, I couldn't find any filesave function. Maybe, the problem is to save it before closing.

I tried your code and it works properly. There is "Hello!" in the file. You don't have to save the file before closing. There isn't a "Save" function (only FileFlush) in MQL. You can test the return value from FileWriteString.

   int filehandle=FileOpen("updated_Q.csv",FILE_WRITE|FILE_CSV); 
   string str="Hello!";

   Print("filehandle=", filehandle);

   if(filehandle>0)
   {
    uint count=FileWriteString(filehandle, str);
    if(count<1) printf("FileWriteString Error: %d",GetLastError());
    else printf("It has been written %d characters in the file",count);
    FileClose(filehandle);  
    Print("OK!");
   }
   


My result in the log is:

filehandle=1
It has been written 6 characters in the file
OK!

 

When it comes to writing CSV files most people don't know you can write the entire row using FileWrite, and you also don't have to cast any of your data to string.  Additionally, there is a file wrapper in the std library which I got in the habit of using since I always forget to explicitly close the file and it automatically closes the file. So a program could look something like this. 


#include <Files\File.mqh>
#include <stdlib.mqh>
void OnStart()
{
   CFile f;
   string file_name = StringFormat("%s_%s.csv",
      _Symbol, EnumToString((ENUM_TIMEFRAMES)_Period)
   );
   if(f.Open(file_name, FILE_CSV|FILE_WRITE, ',') == INVALID_HANDLE){
      Alert("FileOpenError: ", ErrorDescription(GetLastError()));
      return;
   }
   MqlRates r[];
   int total =  CopyRates(_Symbol, PERIOD_CURRENT, 0, 100, r);
   for(int i=0; i<total; i++){
      FileWrite(f.Handle(), 
         r[i].time, 
         r[i].open, 
         r[i].high, 
         r[i].low, 
         r[i].close, 
         r[i].tick_volume
      );
   } 
}
 
Petr Nosek:

I tried your code and it works properly. There is "Hello!" in the file. You don't have to save the file before closing. There isn't a "Save" function (only FileFlush) in MQL. You can test the return value from FileWriteString.


My result in the log is:

filehandle=1
It has been written 6 characters in the file
OK!

I used your code.


My result in the log is:

filehandle=1

It has been written 6 characters in the file

Ok!

However, the csv file is still empty. I don't understand. I deleted the csv and create it again. The results are same. The csv is empty.

 
Murat Yazici:

I used your code.


My result in the log is:

filehandle=1

It has been written 6 characters in the file

Ok!

However, the csv file is still empty. I don't understand. I deleted the csv and create it again. The results are same. The csv is empty.

What program are you using to open the csv?

 
Murat Yazici:

I used your code.


My result in the log is:

filehandle=1

It has been written 6 characters in the file

Ok!

However, the csv file is still empty. I don't understand. I deleted the csv and create it again. The results are same. The csv is empty.

It works well for me. Don't you work with the file in the rest of your code?
 
@nicholi shen Excel
@Petr Nosek  I don't work with the file on any other parts.
 
Murat Yazici:
@nicholi shen Excel
@Petr Nosek  I don't work with the file on any other parts.
This is strange. Here is my csv file created by this code.

Note: I had to change csv into txt to be able attaching

Files:
updated_Q.txt  1 kb
 
Petr Nosek:
This is strange. Here is my csv file created by this code.

Note: I had to change csv into txt to be able attaching

Hi Petr,

I had created a funtion as follows;

void QLearn::write_file(string str)
{  
   string filename="updated_Q.csv";

   int filehandle=FileOpen(filename,FILE_CSV|FILE_WRITE, ","); 
   Print("filehandle=", filehandle);
   if(filehandle<0) 
     { 
      Print("Failed to open the file by the absolute path "); 
      Print("Error code ",GetLastError()); 
     } 
  
//--- correct way of working in the "file sandbox" 
   ResetLastError(); 
   if(filehandle>0)
   {
    uint count=FileWriteString(filehandle, str);
    if(count<1) printf("FileWriteString Error: %d",GetLastError());
    else printf("It has been written %d characters in the file",count);
    FileClose(filehandle);  
    Print("OK!");
   }
} 

I call this function as follows;

string str="Hello!";
write_file(str);

I get same logs. However, the csv file is not created.
Then, I created a csv file as updated_Q csv. However, after run the code, the file is empty.
I look at the path. The path is correct.

Reason: