Has anyone been able to successfully read from a file using MEL - FILEREAD (fhnd)?
I have been stuck on this for a little more than a day and i can write to a file, run a separate process but for some reason i can not get the read function to work properly :/
Every time i attempt to call the function it just returns "FALSE" ... Is there a specific directory it forces the read from? (Also i have reversed the forward slashes to back slashes and get the same result.)
Im using VFE
Read File Function:
!fn fnFileRead()
{
local fname = "c:/temp/output.txt"
local fhnd = FILEOPEN(fname,"r")
FILEOPEN(fname,"r")
if fhnd <> NULL then
text = FILEREAD(fhnd)
FILECLOSE(fhnd)
else ""
endif
}
any assistance would be greatly appreciated!!
-Chris
I use it all the time for user preferences and occasionally for writing log files. Its great. My guess is the FILECLOSE is returning the FALSE, add return text at the end of the function and I think it should work.
!fn fnFileRead()
{
local fname = “c:/temp/output.txt”
local fhnd = FILEOPEN(fname,”r”)
local text = ""
if fhnd NULL then
text = FILEREAD(fhnd)
FILECLOSE(fhnd)
endif
return text
}
Agree with above comments, the routine is returning the logical value of FILECLOSE(fhnd). In addition, I am always concerned about executing functions on load. It is prone to timing problems. Here is working code snippet I use, the file name is in call, strTextComp:
{fn Get_File(strTextComp)
{
local strFile=""
local fhnd
local fname="F:/ACG Text Components/"+strTextComp
fhnd=fileopen(fname,"r")
if fhnd==NULL then
userok("Unable to open file: "+strTextComp)
else
strFile=fileread(fhnd)
fileclose(fhnd)
endif
return strFile
}
}
That was it!!!
Wow i cant believe i forgot to put the return test at the end of the function ... my bad! But thank you guys for the help!!! got it up and working!!
🙂