News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

Bug with WiteString and WriteStringAt

Started by thaaks, September 20, 2005, 03:17:09 PM

Previous topic - Next topic

thaaks

Hi,
I want to create some logging functions where a log message shall be appended to an existing log file or written to a new log file.
I use WriteString because the online help says that the string is appended at the end of the file.
Seems that's wrong. Here is some sample code to prove it:
PlayBASIC Code: [Select]
; PROJECT : Project2
; AUTHOR : Tommy Haaks
; CREATED : 20.09.2005
; EDITED : 20.09.2005
; ---------------------------------------------------------------------

File$ = "c:\test_write_string.txt"

; this doesnot work. only the second line is in the file although documentation for WriteString says text will be appended at
; end of file
FileHandle = GetFreeFile()
WriteFile File$, FileHandle
WriteString FileHandle, "This is the first line"
CloseFile FileHandle

FileHandle = GetFreeFile()
WriteFile File$, FileHandle
WriteString FileHandle, "This should be the second line"
CloseFile FileHandle

Print "Please have a look at " + File$ + ". It should contain two lines but only contains 1 line."
Print "Press any key to continue."
Sync
WaitKey
; second try: Use Filesize() to position to end of file. also fails...
; even worse: seems that first line is overwritten with zero bytes
FileHandle = GetFreeFile()
WriteFile File$, FileHandle
WriteString FileHandle, "This is the first line"
CloseFile FileHandle

FileHandle = GetFreeFile()
Size = FileSize(File$)
Print "Size is " + Str$(Size)
WriteFile File$, FileHandle
WriteStringAt FileHandle, Size, "This should be the second line"
CloseFile FileHandle

Print "Please have a look at " + File$ + ". It should contain two lines but it is corrupted."
Print "Press any key to continue."
Sync
WaitKey



And by the way: Why is it that some commands cannot be used with brackets (like WriteFile, WriteString) and other commands insist in brackets (like FileSize(file$))?
This is pretty confusing and annoying. I am used to type brackets all the time  :D

Cheers,
Tommy

kevin

>>> here your writing a file

FileHandle = GetFreeFile()
WriteFile File$, FileHandle
WriteString FileHandle, "This is the first line"
CloseFile FileHandle

Now your re-writing over this file, 'writefile' isn't append. although it prolly should be.

FileHandle = GetFreeFile()
WriteFile File$, FileHandle
WriteString FileHandle, "This should be the second line"
CloseFile FileHandle

empty

QuoteAnd by the way: Why is it that some commands cannot be used with brackets (like WriteFile, WriteString) and other commands insist in brackets (like FileSize(file$))?
This is pretty confusing and annoying. I am used to type brackets all the time biggrin.gif

Actually that's standard behaviour of Basic languages. If the command returns a value it takes parameters in parantheses, if it doesn't return anything it doesn't take parantheses.
You'd write
Print "Hello World"
rather than
Print("Hello World")

thaaks

QuoteNow your re-writing over this file, 'writefile' isn't append. although it prolly should be.
That's exactly what I expected. If you look at the online documentation of WriteString it says that the text is appended at the end of the file.

I want to open the log file, append the log message at the end of the file and close the log file.
This way I don't have an open file as long as the program is running and the chance of corrupting a file is reduced in case of a crash...

At least that was what I thought, but WriteFile/WriteString behaved differently  <_<

thaaks

Any news on this one?
Is there a way to open an existing file, append some string at the end of the file and close the file afterwards?

kevin

#5
Since readfile + writefile are for, well purely reading or writing i've added a generic version call OpenFile.   Which allows you to read/write to an existing file at the same time.   See example for useage

OpenFile Filename$,FileINdex  


added in PB1.089x beta3



PlayBASIC Code: [Select]
; The File name of the Temp log file
File$="C:\PB_LogFile.txt"

DumpToLog(File$,"---------------------------")
DumpToLog(File$,"First String")
DumpToLog(File$,"Second String")
DumpToLog(File$,"Third String")
DumpToLog(File$,"Last String")


; Dump the Contents of the Log file to the screen
readfile file$,1
repeat
print ReadString$(1)
until EndOfFIle(1)
CloseFIle 1

; Display the screena and wait for a key press
Sync
waitkey



Function DumpToLog(Filename$,ThisString$)
ThisFile=GetFreeFile()

; Check if the file exists ?, if it doesn't, we'll need to create an empty file
if FileExist(Filename$)=false then WriteFile Filename$,ThisFile: CLoseFile ThisFile

; Get the Size of the file (the place to write the next string)
Pos=FileSize(Filename$)

; Open it for random access (read or write)
OpenFile Filename$,ThisFile

; Write The string to the end of the file
WriteStringat ThisFile,Pos,ThisString$

; Close the Used File index
CloseFIle ThisFile
EndFunction