Problum in reading text file

 
Hi all I am trying to read text file using MQL4. the issue I am facing is FileReadString method always returns empty string. following is my code:
void OnTick()
{
      Read();

}


void Read()
{
   ResetLastError(); 
   
   int file_handle = FileOpen("investing_technical_technical-summary.txt", FILE_READ|FILE_BIN|FILE_ANSI); 
   
   if(file_handle!=INVALID_HANDLE) 
   { 
      PrintFormat("%s file is available for reading", "investing_technical_technical-summary.txt");      
      
      int    str_size; 
      string str; 
      
      while(!FileIsEnding(file_handle)) 
      { 
         str_size=FileReadInteger(file_handle, INT_VALUE); 
         
         str=FileReadString(file_handle);
         
         Print(str);
      } 
      
      FileClose(file_handle); 
      
      PrintFormat("Data is read, %s file is closed", "investing_technical_technical-summary.txt");
   } 
   else 
      PrintFormat("Failed to open %s file, Error code = %d", "investing_technical_technical-summary.txt", GetLastError()); 
}
 
salirazataqvi I am trying to read text file using MQL4. the issue I am facing is FileReadString method always returns empty string. following is my code:
  1.  int file_handle = FileOpen("investing_technical_technical-summary.txt", FILE_READ|FILE_BIN|FILE_ANSI); 
    You aren't reading a text file.

  2. Perhaps you should read the manual.
    When reading from a bin-file. the length of a string to read must be specified.
              FileReadString - File Functions - MQL4 Reference
 

Thanks whroeder1,

I have changed my code as follows but still not able to read file. I am getting  5004 Error code.


void OnTick()
{
      Read();
}

void Read()
{
   ResetLastError(); 
   
   int file_handle = FileOpen("investing_technical_technical-summary.txt", FILE_READ|FILE_TXT); 
   
   if(file_handle!=INVALID_HANDLE) 
   { 
      PrintFormat("%s file is available for reading", "investing_technical_technical-summary.txt");      
      
      int    str_size; 
      string str; 
      
      while(!FileIsEnding(file_handle)) 
      { 
         str_size=FileReadInteger(file_handle, INT_VALUE); 
         
         str=FileReadString(file_handle);
         
         Print(str);
      } 
      
      FileClose(file_handle); 
      
      PrintFormat("Data is read, %s file is closed", "investing_technical_technical-summary.txt");
   } 
   else 
      PrintFormat("Failed to open %s file, Error code = %d", "investing_technical_technical-summary.txt", GetLastError()); 
}
 

It's still wrong go read that documentation.

Function

Action

FileFindFirst

Starts the search of files in a directory in accordance with the specified filter

FileFindNext

Continues the search started by the FileFindFirst() function

FileFindClose

Closes search handle

FileOpen

Opens a file with a specified name and flag

FileDelete

Deletes a specified file

FileFlush

Writes to a disk all data remaining in the input/output file buffer

FileGetInteger

Gets an integer property of a file

FileIsEnding

Defines the end of a file in the process of reading

FileIsLineEnding

Defines the end of a line in a text file in the process of reading

FileClose

Closes a previously opened file

FileIsExist

Checks the existence of a file

FileCopy

Copies the original file from a local or shared folder to another file

FileMove

Moves or renames a file

FileReadArray

Reads arrays of any type except for string from the file of the BIN type

FileReadBool

Reads from the file of the CSV type a string from the current position till a delimiter (or till the end of a text line) and converts the read string to a value of bool type

FileReadDatetime

Reads from the file of the CSV type a string of one of the formats: "YYYY.MM.DD HH:MM:SS", "YYYY.MM.DD" or "HH:MM:SS" - and converts it into a datetime value

FileReadDouble

Reads a double value from the current position of the file pointer

FileReadFloat

Reads a float value from the current position of the file pointer

FileReadInteger

Reads int, short or char value from the current position of the file pointer

FileReadLong

Reads a long type value from the current position of the file pointer

FileReadNumber

Reads from the file of the CSV type a string from the current position till a delimiter (or til the end of a text line) and converts the read string into double value

FileReadString

Reads a string from the current position of a file pointer from a file

FileReadStruct

Reads the contents from a binary file  into a structure passed as a parameter, from the current position of the file pointer

FileSeek

Moves the position of the file pointer by a specified number of bytes relative to the specified position

FileSize

Returns the size of a corresponding open file

FileTell

Returns the current position of the file pointer of a corresponding open file

FileWrite

Writes data to a file of CSV or TXT type

FileWriteArray

Writes arrays of any type except for string into a file of BIN type

FileWriteDouble

Writes value of the double type from the current position of a file pointer into a binary file

FileWriteFloat

Writes value of the float type from the current position of a file pointer into a binary file

FileWriteInteger

Writes value of the int type from the current position of a file pointer into a binary file

FileWriteLong

Writes value of the long type from the current position of a file pointer into a binary file

FileWriteString

Writes the value of a string parameter into a BIN or TXT file starting from the current position of the file pointer

FileWriteStruct

Writes the contents of a structure passed as a parameter into a binary file, starting from the current position of the file pointer

FileLoad

Reads all data of a specified binary file into a passed array of numeric types or simple structures

FileSave

Writes to a binary file all elements of an array passed as a parameter

FolderCreate

Creates a folder in the Files directory

FolderDelete

Removes a selected directory. If the folder is not empty, then it can't be removed

FolderClean

Deletes all files in the specified folder

If the file is opened for writing using FileOpen(), all subfolders specified in the path will be created if there are no such ones.

 
salirazataqvi: I have changed my code as follows but still not able to read file. I am getting  5004 Error code.
  1. You changed the open from binary to text. You didn't change the reading code.
  2. FileReadInteger is for reading binary files. You're not. Try number.
  3. You opened a TXT file, you can't split lines while reading. Try CSV with a separator. Only you know what is in your file.
  4. You know what the error is. There is no such file in MQL4\Files or tester\files.
 

Thanks All,

I managed to solved the issue and red the file via same code. The issue was file was not at the correct location as you mentioned in 4 point.  I put the file at the location and issue resolved.


I was getting 5004 error and its description written in the documentation is:

5004

ERR_FILE_CANNOT_OPEN

Cannot open file


I must say documentation provided for MQL is extremely pathetic and most of the time misleading.

Reason: