Put the file 'fileTest.txt' in the shared folder:


Here is the code that reads the file:
//+------------------------------------------------------------------+ //| Reading different types.mq5 | //| Copyright © 2022, Vladimir Karputov | //| https://www.mql5.com/en/users/barabashkakvn | //+------------------------------------------------------------------+ #property copyright "Copyright © 2022, Vladimir Karputov" #property link "https://www.mql5.com/en/users/barabashkakvn" #property version "1.00" #property script_show_inputs //--- input parameters input string InpFileName = "fileTest.txt"; //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- correct way of working in the "file sandbox" ResetLastError(); int file_handle=FileOpen(InpFileName,FILE_READ|FILE_TXT|FILE_COMMON); if(file_handle!=INVALID_HANDLE) { PrintFormat("%s file is available for reading",InpFileName); PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_COMMONDATA_PATH)); //--- additional variables string str; //--- read data from the file while(!FileIsEnding(file_handle)) { //--- read the string str=FileReadString(file_handle,-1); //--- print the string PrintFormat(str); } //--- close the file FileClose(file_handle); PrintFormat("Data is read, %s file is closed",InpFileName); } else PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError()); } //+------------------------------------------------------------------+
Thanks for the reply, Vladimir Karputov,
The algo runs without any issues, however, when printing the string, it gives me these symbols. I changed the data in the file to just "line-1\nline-2\n...line-5) but I still get the same symbols (see screenshot_1).
I'm using a UTF-8 txt file (see screenshot_3) .
Thanks
Put the file in MQL5\Files folder via the menu File -> Open Data Folder
#property copyright "Copyright © 2022, Vladimir Karputov" #property link "https://www.mql5.com/en/users/barabashkakvn" #property version "1.00" #property script_show_inputs //--- input parameters input string InpFileName = "fileTest.txt"; //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- correct way of working in the "file sandbox" ResetLastError(); int file_handle=FileOpen(InpFileName,FILE_READ|FILE_TXT|FILE_ANSI,0,CP_UTF8); if(file_handle!=INVALID_HANDLE) { PrintFormat("%s file is available for reading",InpFileName); PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH)); //--- additional variables string str; //--- read data from the file while(!FileIsEnding(file_handle)) { //--- read the string str=FileReadString(file_handle,-1); //--- print the string PrintFormat(str); } //--- close the file FileClose(file_handle); PrintFormat("Data is read, %s file is closed",InpFileName); } else PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError()); } //+------------------------------------------------------------------+
Thanks for the reply, Vladimir Karputov,
The algo runs without any issues, however, when printing the string, it gives me these symbols. I changed the data in the file to just "line-1\nline-2\n...line-5) but I still get the same symbols (see screenshot_1).
I'm using a UTF-8 txt file (see screenshot_3) .
Thanks
Version 1.001
//+------------------------------------------------------------------+ //| Reading different types.mq5 | //| Copyright © 2022, Vladimir Karputov | //| https://www.mql5.com/en/users/barabashkakvn | //+------------------------------------------------------------------+ #property copyright "Copyright © 2022, Vladimir Karputov" #property link "https://www.mql5.com/en/users/barabashkakvn" #property version "1.001" #property script_show_inputs //--- input parameters input string InpFileName = "fileTest.txt"; //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { //--- correct way of working in the "file sandbox" ResetLastError(); int file_handle=FileOpen(InpFileName,FILE_READ|FILE_TXT|FILE_ANSI|FILE_COMMON,0,CP_ACP); if(file_handle!=INVALID_HANDLE) { PrintFormat("%s file is available for reading",InpFileName); PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_COMMONDATA_PATH)); //--- additional variables string str; //--- read data from the file while(!FileIsEnding(file_handle)) { //--- read the string str=FileReadString(file_handle,-1); //--- print the string PrintFormat(str); } //--- close the file FileClose(file_handle); PrintFormat("Data is read, %s file is closed",InpFileName); } else PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError()); } //+------------------------------------------------------------------+
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello fellow traders,
I'm trying to read a txt file with 3 collumns containg different type of data [datetime, double, double]. I get no error but I can't have my algo to read the file following the FileReadArray instructions. fileHandle is invalid.
Any idea?
Cheers!
//---fileTest.txt data
2022.04.26\t1.2354\t1.8524