Writing in a file

Just starting out? Need help? Post your questions and find answers here.
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Writing in a file

Post by Inf0Byt3 »

Hello guys. I've been playing with the file functions and I wanted to ask you something: I need to create a file and write in it the contents of a memory buffer, but I want to display the progress for this.
If I use WriteData() i can't know how many bits were written. I tried something like this:

Code: Select all

 global *a
 if createfile(0,"test.bin")
 for i = 1 to length
 writebyte(peekb(*a))
 ;show progress
 next i
 closefile(0)
 endif 
The problem is that the file is being created but it's empty :cry:
So can anybody please help??? Thanx in advance...
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

If length=0, no bytes will be written to the file and it'll be empty.
El_Choni
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

I've checked it and the length is > 0. Any suggestions?
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: Writing in a file

Post by SFSxOI »

never mind, it was stupid question anyway :)
Last edited by SFSxOI on Sat Jan 21, 2006 12:36 am, edited 1 time in total.
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

If you don't post the entire code, I can't help. In the code you posted, length=0.
El_Choni
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

AAA.. You were right. I defined the length as global and I was equalizing it with zero. I am stupid!!! Now the file gets created but it's very slow (200 kb/s). I'll post the code as soon as I can. Thanks!
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

This isn't good either. I was working on an installer and when the user executes is, the installer writes the data pack to a temporary file. I cannot post it because it has many includes and i don't want to release the source for it (yet). The problem is that i want to show the progress when writing to the temp file. Is there a way to do this?
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
wcardoso
User
User
Posts: 80
Joined: Fri Apr 25, 2003 5:06 pm
Location: Uruguay

Post by wcardoso »

don't refresh the progress bar each byte you write to the file, do it every 10 or 20 bytes. :wink:
with love from Uruguay
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

Thanks for the tip, but i cannot show the progress at all, because I don't know how many bytes I've written to the file. If I use writebyte() method, it's very slow. If I use WriteData() i don't know how many bytes were written :(.
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Just curious, how do you use WriteData without the length parameter?
@}--`--,-- A rose by any other name ..
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

I have the total amount of data I have to write:

Code: Select all


 global *a
 global length ;assume that length = 2312312 for example

 if createfile(0,"test.bin")

  for i = 1 to length ; I have the length 
   writedata(*a,length)
   Progress = length - a ;????? -> How much was written?
  next i

  closefile(0)
 endif

To show a progress, i need to know the total length, wich I KNOW and how much was written, right??? Then I calculate ?% of length is the data was written, right?
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Inf0Byt3 wrote:Thanks for the tip, but i cannot show the progress at all, because I don't know how many bytes I've written to the file. If I use writebyte() method, it's very slow. If I use WriteData() i don't know how many bytes were written :(.
Use WriteData() in 100 chunks.
For example, if you want to write 100 bytes, you execute WriteData() 100 times, writing one byte each time and updating the progress bar in between.
If you want to write 100 000 bytes you execute WriteData() 100 times, writing (100 000 / 100 =) 1000 bytes each time, updating the progress bar in between.
If you want to write 10 000 000 you execute WriteData() 100 times, writing (10 000 000 / 100 =) 100 000 bytes each time, updating the progress bar in between.
Dare2 wrote:Just curious, how do you use WriteData without the length parameter?
You can't, it's mandatory.
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

Damn, you're right, Trond!!! Why didn't I ever think of this???? :oops:
Thanks. I'll try it now.
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Trond wrote:
Dare2 wrote:Just curious, how do you use WriteData without the length parameter?
You can't, it's mandatory.
:)
@}--`--,-- A rose by any other name ..
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Dare2 wrote:
Trond wrote:
Dare2 wrote:Just curious, how do you use WriteData without the length parameter?
You can't, it's mandatory.
:)
Or if you're desperate, everything is possible:

Code: Select all

!push 5
!call _PB_WriteData@8

End
WriteData(1, 0)
Post Reply