Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

STRTOFILE() may not error when you expect 1

Status
Not open for further replies.

dbMark

Programmer
Apr 10, 2003
1,515
US
FILETOSTR(), as you would expect, returns an error (#1 File does not exist.) if the file to be read is not found.

On the other hand, STRTOFILE() does not error when it fails such as when trying to write a file to a folder that does not exist or to which the user does not have rights. It is the same result whether or not you send the third parameter to overwrite or append. Instead, it simply returns 0 as the number of bytes written. I suggest that whenever you use this function, test whether the returned value is less than the size of the string being written. If so, handle accordingly.

Code:
myString = "This is a test."
IF STRTOFILE(myString,"C:\NoFolderExists\myFile.txt") <> LEN(myString)
   MESSAGEBOX("String not written or partially written to file.")
ENDIF
Okay, this is nothing new, but if you didn't realize the different behaviors between the two functions, you might have coded STRTOFILE() with the wrong assumption that this "failure" would be trapped by an error.
 
I was just testing how large a file I can write with STRTOFILE(). It is 32 bytes shy of 16MB (16,777,184 to be exact). If you need to write a larger file you will have to do it by appending to it in segments. On the other hand, FILETOSTR() is limited only by memory or disk size according to the help files.
 
You can, of course, use low-level file access (FCREATE() etc) to create files larger than 16 MB - up to 2 GB in fact. But not all in one chunk.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
If you use Win API functions you can also read and write >2GB.
 
Also, be aware that when you get to writing 320x1024 bytes in one chunk, STRTOFILE() slows down considerably.

Tamar
 
This is what i use, and it works great

LOCAL fso

fso = Createobject("Scripting.FileSystemObject")

pCreate_file = "C:\temp.txt"

NewFile = fso.CreateTextFile((pCreate_file), .t.)

LcString = "String you want!!"

NewFile.WriteLine(LcString)


NewFile.Close
fso = NULL

André

 
I was just testing how large a file I can write with STRTOFILE(). It is 32 bytes shy of 16MB (16,777,184 to be exact). If you need to write a larger file you will have to do it by appending to it in segments. On the other hand, FILETOSTR() is limited only by memory or disk size according to the help files.

I was largely incorrect in my statement above regarding size limits for STRTOFILE(). I had received the error "String is too large to fit" when I had used SPACE() or REPL() to create a string on the fly.

? STRTOFILE(SPACE(17000000),"C:\myFile.txt")

Well, it turns out that the error I encountered actually is with SPACE() and REPL() being limited to 16MB less 32 bytes. Those functions have set size limits, not STRTOFILE(). Even the help files are all slightly different.

Help for SPACE():
The maximum value of nSpaces is limited only by memory in Microsoft Visual FoxPro.

Help for REPL():
In Visual FoxPro, the maximum length of the resulting character string is restricted by the amount of available memory.

Help - Visual FoxPro System Capacities:
Maximum # of characters per character string or memory variable... 16,777,184

Obviously there are internal parsing limits that you have to work around:

Does NOT work:
x1 = SPACE(16000000) + SPACE(16000000) && error

Works:
x1 = SPACE(16000000)
x1 = x1 + SPACE(16000000) && string now 32,000,000

Still, my original post and description remains correct, that STRTOFILE() simply returns the number of bytes or characters written.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top