read file into memory manipulate and write it back

Everything else that doesn't fall into one of the other PB categories.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by ricardo.

Hi,

Im trying to read some binary data, then manipulate it and write it back.

Can somebody post here a simple example of the most speed method to do it? (an example doing all in memory if its possible)

I want to manipulate bit by bit (suppouse that i want to add 1 to ev ery bit).

I tried to do by fileopen but it was so slow because the files are laaarge (35 Megas).

Thanks in advance

Best Regards

Ricardo

Dont cry for me Argentina...
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> Im trying to read some binary data, then manipulate it and write
> it back.

Actual binary data, or just text in a binary file? Because both Pupil
and Rings posted examples of replacing text in a binary file at the
Resources Site:

http://www.reelmediaproductions.com/pb/ ... ppets.html

Look for "findandreplacemem" and "replacefilestring" at that page.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by ricardo.

Hi PB,

Actual Binary Data (Wav Fike). I want to read every word and manipulate it and at finish save it again on disc.

My experiments has 2 problems:

1.- Huge size of the Wav file.
¿Loading to memory could slow the PC?

2.- Im not sure how to read certain word from memory - something like FileSeek()- be cause i need to read the first bits (the header) and start manipulating from 44 to the end of the file.


At the moment im doing it on a very simple way, and works great but its very very very slow to do a complete Wav file.
Im very happy that i could do this stuff in PB since my last try was on C++/VB and its HUUUGE, and the PB version is very small.

If i could only make it speeder!!!!!!!

If OpenFile(0,InWav)
If OpenFile(1,OutWav)


; Read The Header and Copy it to WavOutFile
For i = 0 To 43
FileSeek(i)
HeaderBit = ReadByte()
UseFile(1)
WriteByte(HeaderBit)
UseFile(0)
Next i

For i = 44 To Tamano Step 2
FileSeek(i)

Frame = ReadWord()

;... Here i Manipulate every Frame

UseFile(1)
WriteWord(EditedFrame)
UseFile(0)
Next i
CloseFile(0)
CloseFile(1)
EndIf
EndIf

Best Regards

Ricardo
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.

You could load and manipualte the data in chunks, something like this:

Code: Select all

structure WordType
  a.w
endstructure

*ptr.WordType = allocatememory(0, 32768)
if *ptr = 0
  end
endif

if readfile(0, InWav)
  if openfile(1, OutWav)
    usefile(0)
    length.l = readdata(*ptr, 44)
    usefile(1)
    writedata(*ptr, length)
    repeat
      usefile(0)
      length = readdata(*ptr, 32768)
      for i = 1 to (length/2)
        ChangeWord(*ptr) ; change the word at address *ptr
        *ptr+2
      next
      usefile(1) : *ptr = usememory(0)
      writedata(*ptr, length)
    until length < 32768
  endif
endif
This was written on the fly so it has not been tested, but the logic should be sound...
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by ricardo.

Hi Pupil

Thanks for your answer.

Maybe im missing something or im so dumb, but what i dont know is exactly the ChangeWord part.
I mean, how to get THAT word, manipulate and put it again in the memory in the point where i take it in first place.

Lets say i want only to add one to every word (its not really the case, but lets use this example to let me be clear).

How do i get every word, add one to it and put it back to its place in the memory chunk?

Thanks



Best Regards

Ricardo

Dont cry for me Argentina...
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Rings.

Hello Guys,
I`m writing a new library (Purebasic-commands, nativly in asm)called 'FastFile' .
Its pointed to that problem you have.
For example some simple Speed-Results:
Loading a 5,3 MB MP3-File and xor every byte tooks 81 millisecs,
scanning (every byte) a 640MB ISO-File tooks 20,31 seconds.
So this depends on my Hard-diskspeed i guess.

Its currently in a early stage, but simple Bytehandling is already done.Plz contact me (ricardo) so that i can send you a beta-version.

If everything goes right(No more gpf) i release that lib in a few days(as long as my others)

regards, Codeguru

Its a long way to the top if you wanna .....CodeGuru
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.
Originally posted by ricardo

Hi Pupil

Thanks for your answer.

Maybe im missing something or im so dumb, but what i dont know is exactly the ChangeWord part.
I mean, how to get THAT word, manipulate and put it again in the memory in the point where i take it in first place.

Lets say i want only to add one to every word (its not really the case, but lets use this example to let me be clear).

How do i get every word, add one to it and put it back to its place in the memory chunk?
OK, so the ChangeWord() procedure could look something like this, for the case with the add one method:

Code: Select all

Procedure ChangeWord(*ptr.WordType)
  *ptr\a+1 ; Not to advanced!  :wink:
EndProcedure

; If you feel uncomfortabe with pointers
; this method is the same
Procedure ChangeWord(address.l)
  a.w = Peekw(address)
  a+1
  Pokew(address, a)
EndProcedure
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by ricardo.

RINGS:

An e mail was sent a few minutes ago, Thanks!!

PUPIL:
Procedure ChangeWord(*ptr.WordType)
*ptr\a+1 ; Not to advanced! :wink:
EndProcedure

; If you feel uncomfortabe with pointers
; this method is the same
Procedure ChangeWord(address.l)
a.w = Peekw(address)
a+1
Pokew(address, a)
EndProcedure
Yes, that was exactly wath i think but seems pretty simple and i was afraid i was missing something : )

Thanks you very much !!!!!



Best Regards

Ricardo

Dont cry for me Argentina...
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by ricardo.
Loading a 5,3 MB MP3-File and xor every byte tooks 81 millisecs,
scanning (every byte) a 640MB ISO-File tooks 20,31 seconds.
So this depends on my Hard-diskspeed i guess.
Wowww, 20 seconds 640MB?
I want to have this lib!!!! : )


Best Regards

Ricardo

Dont cry for me Argentina...
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Andre.
If everything goes right(No more gpf) i release that lib in a few days(as long as my others)
Great news, Rings :)


Regards
André

*** German PureBasic Support ***
Post Reply