Writing in a file
This s**t is freaking me out...
I simply can't get it to work
. I need to write to a file from a filled memory buffer and show the progress for this.
Please help me cause i'm getting nuts. It's simple but i spent 1 week trying to make it and ... no sucess...
Please help me cause i'm getting nuts. It's simple but i spent 1 week trying to make it and ... no sucess...
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
- netmaestro
- PureBasic Bullfrog

- Posts: 8452
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
There are some examples of this on the forum. Freak has an excellent tutorial on downloading a file that shows how to do it.
Last edited by netmaestro on Tue Feb 21, 2006 7:35 pm, edited 2 times in total.
BERESHEIT
The problem is not the progressbar itself. It's showing the progress when creating the file, i mean
Code: Select all
if createfile(0,"blah.txt")
writedata(*sdasdads,length)
;now show progress- but how do i know how many bytes were written?
closefile(0)
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
- netmaestro
- PureBasic Bullfrog

- Posts: 8452
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Oh. OK, well then what you'll do is continually interrupt your writing process to update a variable or memory bank, and start a thread that watches the memory bank and displays a progress bar gadget according to what it finds.
Last edited by netmaestro on Tue Feb 21, 2006 7:37 pm, edited 2 times in total.
BERESHEIT
Example without a thread:
Code: Select all
#MemSize = 30594500
MyMem = AllocateMemory(#MemSize)
*byte.byte = MyMem
For I = 0 To #MemSize ; Fill with some readable characters
*byte\b = (I % 100)+32
*byte + 1
Next
OpenWindow(0, 0, 0, 400, 100, #PB_Window_ScreenCentered | #PB_Window_SystemMenu, "")
CreateGadgetList(WindowID())
ProgressBarGadget(0, 10, 10, 300, 20, 0, 100, #PB_ProgressBar_Smooth)
ButtonGadget(10, 10, 50, 97, 25, "Start")
TextGadget(20, 120, 55, 100, 20, "Press start")
Procedure UpdateStatus(Percent.l)
SetGadgetState(0, Percent)
SetGadgetText(20, Str(Percent)+"%")
EndProcedure
Procedure WriteToFile(Pointer.l, Length.l)
Protected I
Protected P
Protected O
P = Pointer
O = Int(Length * (I+1) / 100.0)
For I = 0 To 99
WriteData(P, O)
UpdateStatus(I+1)
P + O
Next
WriteData(P, Length-(P-Pointer))
DisableGadget(10, 0)
EndProcedure
Repeat
Select WaitWindowEvent()
Case #PB_Event_Gadget
Select EventGadgetID()
Case 10
DisableGadget(10, 1)
SetGadgetText(20, "0%")
CreateFile(0, "c:\_hopethisisjunk.txt")
WriteToFile(MyMem, #MemSize)
CloseFile(0)
EndSelect
Case #PB_Event_CloseWindow
Break
EndSelect
ForEver

