Function MemoRead() Foundation

Reads the contents of a file from disk.

Syntax
MemoRead( <cFilename> ) --> cString
Parameters
<cFileName>
<cFilename> is a character string containing the name of a file (including the drive and path, if necessary).
Return

MemoRead() reads an entire file and returns its contents as a character string. Any size file can be read by MemoRead(). When <cFilename>contains an invalid name or is not found, MemoRead() returns a null string ("").

Description

The memo function MemoRead() provides an easy way to read complete files from disk to main memory. The contents of the file are returned as a character string and can be further processed with other memo functions like MemoEdit() or MemoLine(). Using the function MemoWrit() the character string can later be written back into the file.

MemoRead() searches for <cFilename> in the current directory. SET DEFAULT and SET PATH settings are ignored by MemoRead(). When <cFilename> is found, MemoRead() attempts to open the file in Shared and Read-only mode. If the file is already open exclusively, MemoRead() returns a null string ("").

Examples
MemoRead()
// The example reads the contents of a file into a 
// memory variable. 

PROCEDURE Main 
   LOCAL cString := MemoRead("TEST.PRG") 

   IF Len(cString) == 0 
      ? "File not found or empty!" 
   ELSE 
      ? "File has", Len(cString) ,"bytes." 
   ENDIF 
RETURN 
Transfer files into memo fields

// The example generates a script file containing file names. The 
// script file is read and the contents of the individual files 
// are written into a DBF file. 

PROCEDURE Main 
   LOCAL cString, n, nMax, aFiles 

   SET PRINTER TO Help.scr 
   SET PRINTER ON 
   ?? "Help1.txt" 
   ?  "Help2.txt" 
   ?  "Help3.txt" 
   SET PRINTER OFF 
   SET PRINTER TO 

   DbCreate( "HELP", { {"HelpID"  , "C", 10, 0 }, ; 
                       {"HelpText", "M", 10, 0 }  } ) 

   cString := MemoRead("HELP.SCR") 
   nMax    := MlCount( cString, 80 ) 
   aFiles  := Array( nMax ) 
   FOR n:=1 TO nMax 
       aFiles[n] := MemoLine( cString, 80, n ) 
   NEXT 

   MakeHelp( aFiles ) 
RETURN 

PROCEDURE MakeHelp( aFiles ) 
   LOCAL n, nMax := Len( aFiles ) 
   USE Help NEW 

   FOR n:=1 TO nMax 
      APPEND BLANK 
      REPLACE HelpID   WITH Str(n,10) , ; 
              HelpText WITH MemoRead( aFiles[n] ) 
   NEXT 

   USE 
RETURN 
Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.