FileSeek()ing Troubles - page 2

 
Ovo:

I may save you time -there is no debugging ability for MT4 code. All you can do is to place Print() statement everywhere to monitor values during a program run.

I know there is no debugger. But atleast, one can learn how to detect errors.
 
WHRoeder:
FileSeek - MQL4 Documentation from the beginning, the end

Are you sure? 5 from the beginning is 5 in. -5 would be before the start, impossible. 5 in from the end is toward the beginning -5 would be beyond the end.

I tested it, -ve goes backwards from the end.
 
WHRoeder:
FileSeek - MQL4 Documentation from the beginning, the end

Are you sure? 5 from the beginning is 5 in. -5 would be before the start, impossible. 5 in from the end is toward the beginning -5 would be beyond the end.

@WHRoeder:

When I reviewed the documentation, I concluded the same as you...and that is why I asked the OP about the use of a negative offset.

I conducted a small test which appears to support the OP's use of negative offset from the eof.

int start()
{

   int fhandle = FileOpen("test1.csv", FILE_CSV | FILE_READ | FILE_WRITE, ',');
   if (fhandle < 1) {
      Print("Error Opening File: ", GetLastError());
      return(0);
   }
   FileSeek(fhandle, 0, SEEK_SET);
   Print ("File Offset after seeking to bof: ", FileTell(fhandle), " Error Code: ", GetLastError());
   FileWrite(fhandle, TimeToStr(TimeCurrent(), TIME_DATE|TIME_MINUTES|TIME_SECONDS), 101, 102, 103, 104, 105, 106, 107, 108, 109, 110);
   Print ("File Offset after writing to file: ", FileTell(fhandle), " Error Code: ", GetLastError());
   FileSeek(fhandle, 0, SEEK_END);
   Print ("File Offset after seeking to eof: ", FileTell(fhandle), " Error Code: ", GetLastError());
   FileSeek (fhandle, 5, SEEK_END);
   Print ("The number at offset ", FileTell(fhandle), " is ", FileReadNumber(fhandle), ". Error Code: ", GetLastError());
   FileClose(fhandle);

   return (0);
}

The above code uses a positive offset from eof. However, it generates a 4099 error (ERR_END_OF_FILE). See output below:

FileSeek Test #1

If I change the above code so that the offset is -5 rather than +5 . . .

   FileSeek (fhandle, -5, SEEK_END);

. . . then FileSeek seems to move backward (not forward) from eof and no error is generated. See output below:

FileSeek Test #2

 

It is not so surprising conclusion, if you have seen the SetFilePointer from Kernel32.dll. The FileSeek seems to encapsulate it.

... lDistanceToMove is a 32-bit signed value. A positive value forlDistanceToMove moves the file pointer forward in the file, and a negative value moves the file pointer back.

 

Old topic, I know, but this may really help someone who is stuck with a related issue: if you only use FILE_WRITE, then using FileSeek() to find the end of the file (if you want to append to a file) will fail!   To fix, add FILE_READ as a open flag, and FileSeek will work. It is very annoying to run into this issue as FileSeek will just return 0, as will FileSize. Instead, use correct code as per below.

int fh=FileOpen(fh,FILE_TXT|FILE_COMMON|FILE_READ|FILE_WRITE,"\n");
if(fh!=INVALID_HANDLE){
  FileSeek(fh,0,SEEK_END);
  FileWriteString("your string\n");
  FileWriteString(string(FileSize(fh)+"\n");
  FileFlush(fh);FileClose(fh);
}

(If you (incorrectly - assuming you want to append to a file) drop FILE_READ from this code below, FileSeek() will still work (returns true)...(!), but the pointer will stay at the start of the file, i.e. it will ovewrite what is already there. And as mentioned FileSize() will also return "0" when FILE_READ is not used.)

Reason: