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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

manipulating files - FWRITE

Status
Not open for further replies.

marcela24

Programmer
Feb 17, 2003
72
0
0
NL
I am trying to write a string into a file in my PC, using FOXPRO 2.6. My code is:

_path = "C:\Projetos\SINTEGRA\Sinteg.txt"

HANDLE=FOPEN(_path)
if HANDLE > 0
BYTE = "HI!!!"
=FSEEK(HANDLE,0)
=FWRITE(HANDLE,BYTE)
=FCLOSE(HANDLE)
endif

This code never enter in my IF. Iam sure there is a file in my PC and I´ve already tried using the FCREATE function.

Can you help solve this stupid thing! Iam completly neebie in FOXPRO! =)
 
You probably need a second parameter in the FOPEN() function. If you don't include that second parameter, it's opened in read only mode.

If the file isn't written yet it is then failing to open it in read mode and therefore does not return a valid handle.

handle=FOPEN(_path,0) && read only
handle=FOPEN(_path,1) && write only
handle=FOPEN(_path,2) && read and/or write
 
Also, keep in mind that if it doesn't enter the IF statement, it doesn't get closed using the FCLOSE().
It is a good idea to place the FCLOSE() outside the IF test. Thet way, whether the IF test fails or not, the file handle is sure to be released.

You can check this with DISPLAY STATUS. You will see a line something like "User-opened files". If your variable has been released, you can always close the file using the handle number from DISPLAY STATUS, or issuing a CLOSE ALL statement.


-Dave Summers-
[cheers]
Even more Fox stuff at:
 
When getting any program working with FOPEN() and related calls, you must start with CLOSE ALL to ensure all files are closed. You might not be successful at opening the file because it was opened in a previous run and a bug or crash prevented it from being closed. When your program gets working, you can remove the CLOSE ALL since it may interfere with other things. This will write a string to a new files.

CLOSE ALL && debug
HANDLE=fcreate("MYFILE.TXT",0)
if handle>=0 then
=fputs(handle,"My String")
=fclose(handle)
endif

>HANDLE=FOPEN(_path)
Even opening Read-Write is a bad idea. You must be ready to manage the existing contents. fcreate() starts with a blank file.

>if HANDLE > 0
In my experience, the lowest HANDLE Foxpro DOS ever uses is 4 so handle>=0 works fine for me.

>BYTE = "HI!!!"
>=FSEEK(HANDLE,0)
The file is always at position 0 when first opened unless opened for append, and Fox-DOS doesn't offer that.

>=FWRITE(HANDLE,BYTE)
>=FCLOSE(HANDLE)
The FCLOSE() should go inside the IF because there you should not close the file unless it was opened. If the program is buggy and doesn't correctly detect that the file was opened, that is a different problem.
>endif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top