Problem with FileOpen

 

I am trying to open .txt file for read and write access to log some information. Following is the code snippet.


int OnInit() {
   string fileHandle = FileOpen("test.txt", FILE_CSV | FILE_READ | FILE_WRITE, "+");
   if (fileHandle < 0) {
      Message("Can't open test.txt in MQL4\\files folder for logging changes. ERROR:"+fileHandle, "Initialization", MB_OK | MB_ICONERROR);
      return (INIT_FAILED);
   }
   if (FileSeek(fileHandle, 0, SEEK_END) == false) {
      Message("Error moving pointer to end of file test.txt in files folder for logging changes.", "Initialization", MB_OK | MB_ICONERROR);
      return (INIT_FAILED);
   }
}

This code returns fileHandle (-1) as return from FileOpen. It is hard to know what causes it as it happens in OnInit on initalization.

Any help will be appreciated.

 
  1. Please use
SRC
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. string fileHandle = FileOpen("test.txt", FILE_CSV | FILE_READ | FILE_WRITE, "+");
    if (fileHandle < 0) {
    The third parameter to FileOpen is a Character Constant (a short,) not a string.
  3. The value returned is not a string.
  4. Why did you ignore the warning "implicit conversion from 'number' to 'string' "
 
whroeder1:
  1. Please edit your post.
    For large amounts of code, attach it.

  2. The third parameter to FileOpen is a Character Constant (a short,) not a string.
  3. The value returned is not a string.
  4. Why did you ignore the warning "implicit conversion from 'number' to 'string' "

Thanks whroeder1 for your prompt response. Appreciate your suggestion for posting code properly. Yes, I have corrected mistake in 3rd parameter for FileOpen. Sorry, I didn't have fileHandle type as string. It was a typo while placeing code snippet. Actually I do have int type for fileHandle in my .mqh include file.

What could happen if I ignore implicit conversion. I thought that MT4 internally do type conversion properly. Please advise.
 
jpodda: What could happen if I ignore implicit conversion. I thought that MT4 internally do type conversion properly.
  1. It is going to do the conversion correctly, but the result probably won't be what you want. It's a warning; you are probably doing something dumb. Check it, and if it is correct add a cast (telling the compiler: I know what I'm doing, be quiet.)
  2. string fileHandle = ...
    if (fileHandle < 0) {
    That if becomes fileHandle < "0" and since minus sign (0x2D) happens to come before the zero character (0x30) and non-zero characters come after, that statement happens do work. What would happen if you add a space or a octothorpe to the beginning of the string? The condition would always be true. What if the declaration was at the top of the code and the if was way down? You would never see the problem probably for days.
 
whroeder1:
  1. It is going to do the conversion correctly, but the result probably won't be what you want. It's a warning; you are probably doing something dumb. Check it, and if it is correct add a cast (telling the compiler: I know what I'm doing, be quiet.)
  2. That if becomes fileHandle < "0" and since minus sign (0x2D) happens to come before the zero character (0x30) and non-zero characters come after, that statement happens do work. What would happen if you add a space or a octothorpe to the beginning of the string? The condition would always be true. What if the declaration was at the top of the code and the if was way down? You would never see the problem probably for days.
Well explained, Thanks. FYI, I do have fileHandle type as int, but good to know that try not to overlook implicit conversion warnings. Thanks again.
Reason: