Read or write file

Syntax

s.getfile(file [from] [nbytes])
s.setfile(file [from] [nbytes] [flags])

 

Parameters

s - str variable.

file - file.

from - offset in file. Default: 0.

nbytes - number of bytes to read or write. Default: -1.

flags (used only with setfile):

1 Append new line characters.
2 Set end of file at the position from + number of added bytes. It deletes old content that follows. This flag is not necessary if from is 0 or -1 (append).
4 QM 2.3.2. Don't delete old content. Without this flag, deletes old content if from is 0.

 

Remarks


getfile loads file content to s.

 

Error if the file does not exist.

 

If from and nbytes are not used or are 0 and -1, loads whole file.

If from is >0 and nbytes not used or -1, loads whole file starting from from.

Not error if from+nbytes is more than file length. Then s will contain less than nbytes or will be empty.

 

Don't use this function to load large files, because then may fail to allocate memory for s. Instead read parts of the file. It is more efficient with Windows API functions; you can use class __HFile.

 

QM 2.3.0. getfile supports file syntax ":resourceid filepath". When the macro runs in QM, it gets data from the file. When the macro runs in exe, it gets data from the exe resource. The files are automatically added to exe if "Auto add files ..." is checked. They are added as resources of type RT_RCDATA (10). If the syntax is used with setfile, it just ignores the ":resourceid " part. With both functions, from, nbytes and flags must be not used.

 


setfile writes s to file.

 

If the file does not exist, creates.

 

If from is 0 and the file already exists, replaces old content, unless flag 4 used.

If from is -1, appends.

If nbytes is -1, writes whole s.

 

Some possible setfile errors:

The folder does not exist. To create new folder, use mkdir.

The filename contains invalid filename characters. To avoid it, you can use str function ReplaceInvalidFilenameCharacters.

The file is opened for exclusive access by another process or some QM function.

QM is running not as administrator and the file is in Program Files, Windows, or some other protected folder.

 


Note: In Unicode mode, other string functions interpret text as UTF-8. If you use getfile to load an ANSI text file that contains non ASCII characters, and pass the text to other string functions, you may have to convert ANSI to UTF-8. You also may have to do conversions in some other cases.

 

Tips

You can use the My QM folder (in My Documents) to save various files used by your macros. Use special folder $my qm$.

You can use functions rget and rset to write to or read from ini files.

These functions open the file, write/read, and close. To perform multiple write/read operations without reopening, instead use Windows API functions; you can use class __HFile.

If it is possible that several threads (running functions) access the same file simultaneously, use lock to prevent it. If several processes or computers write to the file simultaneously, use CFileInterlocked class that is available in the forum.

 

Example

str s1 s2 f
f="$my qm$\test.txt"
s1="test data"
s1.setfile(f)
s2.getfile(f)
out s2