WriteFile() function - Replace the whole content of file via WinAPI

 

Hi Programmers!


There is an example article here: File Operations via WinAPI


I would like to use the WriteFile() function based on kernel32.dll for exchange the whole content of the editing file. So, I don't want to append some text or replace some text, but I want to delete all characters of the old content and replace it with the new ones. (Something like when I use only the FILE_CSV|FILE_WRITE mode againt the FILE_CSV|FILE_READ|FILE_WRITE at the FileOpen() function.


The other way to solve my problem could be to delete the file. But I can't use the general FileDelete() function of the MQL becuase my file is outside of the terminal_dir\experts\files directory.


Could you help me? What can be the solution?


Thank you!


Relative

 

You can use

ShellExecuteA(0,"Open","cmd.exe"," /C del \""+FileName+"\" ","",0);

 
Relative:

[...] Could you help me? What can be the solution?

See https://www.mql5.com/en/forum/118999 for one possible solution. Covers overwriting files plus deleting them.

 
Thank you very much Matutin and Jjc!
 
Relative:

Hi Programmers!


There is an example article here: File Operations via WinAPI


I would like to use the WriteFile() function based on kernel32.dll for exchange the whole content of the editing file. So, I don't want to append some text or replace some text, but I want to delete all characters of the old content and replace it with the new ones. (Something like when I use only the FILE_CSV|FILE_WRITE mode againt the FILE_CSV|FILE_READ|FILE_WRITE at the FileOpen() function.


The other way to solve my problem could be to delete the file. But I can't use the general FileDelete() function of the MQL becuase my file is outside of the terminal_dir\experts\files directory.


Could you help me? What can be the solution?


Thank you!


Relative

There's also this library: ZI_File_Functions_Full_Lib

 
gordon:

There's also this library: ZI_File_Functions_Full_Lib

Great! Thank you Gordon!

 

What I was looking for is this:

// *************************************************************************************
// Opens a file for writing, and overwrites its contents (setting its length to zero) if
// it already exists. The return value is the file handle for use in subsequent calls, 
// or INVALID_HANDLE_VALUE if the operation fails. This is a simple wrapper around CreateFileA, 
// and overwrites the file if it already exists by specifying CREATE_ALWAYS. The call will 
// fail if the file is already in use by something else.
// *************************************************************************************

int OpenNewFileForWriting(string FileName, bool ShareForReading = false)
{
   int ShareMode = 0;
   if (ShareForReading) ShareMode = FILE_SHARE_READ;
   return (CreateFileA(FileName, GENERIC_WRITE, ShareMode, 0, CREATE_ALWAYS, 0, 0));
}
This code is in this WinFile.mql file on the page/topic Reading and writing files anywhere on disk using CreateFileA() etc.
 
Relative:

What I was looking for is this: [...]

Glad it was useful. But watch out for the errors in some of the code comments which I mention in a subsequent post in https://www.mql5.com/en/forum/118999. (There's no known problem with the code itself, but I'm not promising anything...)

 
jjc:

Glad it was useful. But watch out for the errors in some of the code comments which I mention in a subsequent post in https://www.mql5.com/en/forum/118999. (There's no known problem with the code itself, but I'm not promising anything...)

Ok. Thx!

Reason: