Page 1 of 1

Possible bug in write strings

Posted: Mon Nov 18, 2024 10:48 pm
by Sergey
Maybe I'm wrong, so correct me

Code: Select all

If OpenFile(0, "test.txt")
	FileSeek(0, Lof(0))
	Repeat
		WriteStringN(0, Str(ElapsedMilliseconds()))
		Delay(1000)	
	ForEver
; 	CloseFile(0)
Else
	MessageRequester("Error", "Can't open file!")
EndIf
If I run code in the debug mode and then kill the process the file has data
And If I run compiled executable file or run code without a debugger there are no strings in the file
Why?

And <ForEver> not hilighted in post ;-)


// Moved from "Bugs - Windows" to "Coding Questions" (Kiffi)

Re: Possible bug in write strings

Posted: Mon Nov 18, 2024 11:28 pm
by mk-soft
Programme is never terminated and the file buffer is not written.
Something like this should never be written !!!

Re: Possible bug in write strings

Posted: Tue Nov 19, 2024 4:05 am
by BarryG
How can you run it when you've got "FileSeek(Lof(0))" without a file number? The code doesn't even compile.

Re: Possible bug in write strings

Posted: Tue Nov 19, 2024 7:53 am
by infratec

Code: Select all

WriteStringN(0, Str(ElapsedMilliseconds()))
FlushFileBuffers(0)
Should do the trick.

Re: Possible bug in write strings

Posted: Tue Nov 19, 2024 8:57 am
by BarryG
Maybe even using the #PB_File_NoBuffering flag with OpenFile().

Re: Possible bug in write strings

Posted: Tue Nov 19, 2024 10:12 am
by NicTheQuick
Closing the file is essential to write every buffer persistently.

Re: Possible bug in write strings

Posted: Tue Nov 19, 2024 10:16 am
by Sergey
infratec wrote: Tue Nov 19, 2024 7:53 am

Code: Select all

WriteStringN(0, Str(ElapsedMilliseconds()))
FlushFileBuffers(0)
Should do the trick.
Ha, it's work!

Thank you all for understanding

BarryG this is a test code of LOG file in case the system turns off unexpected
Yes, I need #PB_File_NoBuffering, cool!
Each time will learn something new in PureBasic

Re: Possible bug in write strings

Posted: Tue Nov 19, 2024 1:20 pm
by Olli
infratec wrote: Tue Nov 19, 2024 7:53 am

Code: Select all

WriteStringN(0, Str(ElapsedMilliseconds()))
FlushFileBuffers(0)
Should do the trick.
Thank you infratec. My suggest would have pushed on the shared i/o option in the OpenFile() arguments.

I note that the BarryG suggest seems to be safer, in the shut down ways.

Re: Possible bug in write strings

Posted: Tue Nov 19, 2024 4:06 pm
by infratec
For logging I always open with the flag #PB_File_Append (no FileSeek() needed) and close the file for each log entry.
Because then I can use tail :wink:

Re: Possible bug in write strings

Posted: Tue Nov 19, 2024 6:26 pm
by idle
What's faster in a multi writer senario
Keeping a file open appending and flush or open append close?

Re: Possible bug in write strings

Posted: Tue Nov 19, 2024 11:29 pm
by Sergey
idle wrote: Tue Nov 19, 2024 6:26 pm What's faster in a multi writer senario
Keeping a file open appending and flush or open append close?
It all depends on the situation, in my case, the recording is made with many strings, so I chose a flush method
If there aren't so many strings, it's better to use closefile :wink:

Re: Possible bug in write strings

Posted: Tue Nov 19, 2024 11:57 pm
by idle
Sergey wrote: Tue Nov 19, 2024 11:29 pm
idle wrote: Tue Nov 19, 2024 6:26 pm What's faster in a multi writer senario
Keeping a file open appending and flush or open append close?
It all depends on the situation, in my case, the recording is made with many strings, so I chose a flush method
If there aren't so many strings, it's better to use closefile :wink:
I'm interested in the latencies between the methods, especially from multiple threads, though it won't be hard to test but I haven't gotten to the stage where I'm focusing on writing to disk.

Re: Possible bug in write strings

Posted: Thu Nov 21, 2024 3:03 am
by Olli
The worst thing, I have got on this theme of mass memory recording, is a total copy of all my file I wanted to copy to an USB key, with all my datas zeroed, after having unplugged, and then unplugged.