Writing data to a certain line in a file

 
Hello MQL4 coders,

Unfortunately the FileWrite() function lacks the flexibility to define the line number of the given file.
Is there a way to write to / overwrite only specific lines in a file without overwriting all other lines or time consuming loops?

Thanks for any suggestions or sample code!

Jim
 
Did you read the manual? Then you have your answer.
 
WHRoeder:
Did you read the manual? Then you have your answer.


With all respect, but I'm not sure if my question is clear to you.
In case you mean the File functions, yes, I read all of them and tried Google as well.

The problem seems that it's only possible to write line after line. I'm looking for a method to change the content of a certain line, without overwriting the rest.

Jim

 
Can't you https://docs.mql4.com/files/FileSeek to the correct location and write there ?
 
RaptorUK:
Can't you https://docs.mql4.com/files/FileSeek to the correct location and write there ?


No, but maybe I use this function wrong. When I create a text file with something like "don't overwrite this" on the first line, the following code overwrites this and won't write to a new line.

int SettingsFileHandle;
string SettingsFile;

int init()
{
   SettingsFile = Symbol()+".txt";
   return(0);
}
int start()
{
   SettingsFileHandle = FileOpen(SettingsFile, FILE_CSV|FILE_WRITE);
   FileSeek(SettingsFileHandle, 2, SEEK_SET);
   FileWrite(SettingsFileHandle, Close[0]);
   FileClose(SettingsFileHandle);
   return(0);
}

Jim

 
You are only seeking to 2 bytes into the file . . . I've never done anything like this with files, I guess you would have to seek, read, seek, read, etc to identify the correct location in the file that you want to overwrite.
 
RaptorUK:
You are only seeking to 2 bytes into the file . . . I've never done anything like this with files, I guess you would have to seek, read, seek, read, etc to identify the correct location in the file that you want to overwrite.

To be honest, I wasn't familiar with this function, but 2 or 20 bytes, it doesn't matter because the only result is that the data will be written on the same line with an insertion. All data on this line will be overwritten, no matter the byte size.
I'm looking for a way to define a line number. The FileSeek function can't accomplish this.

Jim

 
VirtualReal:
it's only possible to write line after line. I'm looking for a method to change the content of a certain line, without overwriting the rest.

What part of ONLY POSSIBLE don't you understand. Line width is not constant, therefor you can't seek and can't overwrite.

  1. Read lines into array
  2. modify array
  3. Overwrite file with array.
 
VirtualReal:

To be honest, I wasn't familiar with this function, but 2 or 20 bytes, it doesn't matter because the only result is that the data will be written on the same line with an insertion. All data on this line will be overwritten, no matter the byte size.
I'm looking for a way to define a line number. The FileSeek function can't accomplish this.

Jim

The FileSeek() function only seeks on a given line (horizontal). This is not what I'm looking for, so this function won't help.

Example, you have a file like this:

Line 1 = A
Line 2 = B
Line3 = C

How can I change only the content of line 2 without the need to write lines 1 & 3 again?

I hope this example made my problem more clear.


Jim

 
VirtualReal:

The FileSeek function can't accomplish this.

Jim

That's like saying that OrderSymbol() doesn't accomplish returning the symbol for a particular order . . . strictly speaking it is correct . . . but when you add the rest of the code OrderSymbol() does exactly what you want it to do.

Take a look at your txt file in a HEX editor and you will see what a file looks like in reality . . .

 
VirtualReal:

The FileSeek() function only seeks on a given line (horizontal). This is not what I'm looking for, so this function won't help.

I don't think that is correct.

"The function moves the file pointer to a new position that is an offset, in bytes, from the beginning, the end or the current file position. "

from here: https://docs.mql4.com/files/FileSeek

Files aren't arranged in lines or columns . . .

You can do what you are looking to do as long as the contents of your new data is the same size or smaller (in bytes) than the data it is replacing . . . if it's smaller you will need to pad out the replacement data to the same size. If you need to add more data, in bytes, you have to re-write all the data following to make space.