WriteData doesn't advance file pointer second time?

Just starting out? Need help? Post your questions and find answers here.
jonljacobi
User
User
Posts: 67
Joined: Mon Jan 16, 2006 10:12 pm

WriteData doesn't advance file pointer second time?

Post by jonljacobi »

Code: Select all


WriteData(1, @AnswerCorrect(0, 0, 0, 0), BytesToWrite)
Debug Loc(1)
WriteData(1, @AnswerIncorrect(0, 0, 0, 0), BytesToWrite)
Debug Loc(1)

BytesToWrite = 40608

The first Debug reads 40608
The second debug reads 40608
Total file size is 40608

What am I missing?

Cheers, Jon :?:
Xombie
Addict
Addict
Posts: 898
Joined: Thu Jul 01, 2004 2:51 am
Location: Tacoma, WA
Contact:

Post by Xombie »

You're using WriteData() wrong. It expects a properly allocated block of memory or a pointer to some memory. Using an @Procedure passes the address to the procedure itself, not the return value of the procedure. If your procedures are returning pointers to a memory block, I'd recommend storing that return into a variable first and then calling WriteData() with that variable.

Unless I'm totally off my rocker here and missing something obvious.
jonljacobi
User
User
Posts: 67
Joined: Mon Jan 16, 2006 10:12 pm

Post by jonljacobi »

Those are the addresses of arrays.

Cheers, Jon
Xombie
Addict
Addict
Posts: 898
Joined: Thu Jul 01, 2004 2:51 am
Location: Tacoma, WA
Contact:

Post by Xombie »

Ah. So like this?

Code: Select all

Dim HoldArray.l(10, 10, 10, 10)
Dim HoldArray2.l(10, 10, 10, 10)
For i.l = 0 To 9
   For j.l = 0 To 9
      For k.l = 0 To 9
         For l.l = 0 To 9
            HoldArray(i, j, k, l) = Random(358911)
            HoldArray2(i, j, k, l) = Random(358911)
         Next l
      Next k
   Next j
Next i
iFile.l = CreateFile(#PB_Any, "c:\temp\test.txt")
If iFile
   Debug Loc(iFile)
   WriteData(iFile, @HoldArray(), 10000)
   Debug Loc(iFile)
   WriteData(iFile, @HoldArray2(), 10000)
   Debug Loc(iFile)
   CloseFile(iFile)
EndIf
jonljacobi
User
User
Posts: 67
Joined: Mon Jan 16, 2006 10:12 pm

Post by jonljacobi »

In the actual program, I write a string and Loc(1) comes back with 9. Then I write the 40608 bytes and the Loc(1) comes back with 40617. Then I write another 40608 bytes and Loc(1) comes back again with 40617.

I'm sorry, but I'm completely unable to fathom a reason for this.

Cheers, Jon
jonljacobi
User
User
Posts: 67
Joined: Mon Jan 16, 2006 10:12 pm

Post by jonljacobi »

Your example works, but I must be missing something because mine does not. Here's the actual code. Forgive the sloppiness as I'm porting it and hacking it up trying different stuff.

Code: Select all

  BytesToWrite = ArraySize(@AnswerCorrect()) * 4

  If Logic = #FILE_SAVE
    BackupFile(Globe\ScorePath, "scb")
  
    If Not CreateFile(1, Globe\ScorePath)
      ProcedureReturn
    EndIf 

    LoadingOrSaving$ = "Saving "

  Else
    LoadingOrSaving$ = "Loading "
    If Not ReadFile(1, Globe\ScorePath)
      ProcedureReturn
    EndIf 

  EndIf
  LoadingOrSaving$ = LoadingOrSaving$ + LCase(Globe\ScorePath) + "..."

  If lNot(Globe\ProgramStartup)
    SetWindowText_(hwndAttach, @LoadingOrSaving$)
  EndIf

  

  
;  SettingsWriteReadString(Logic, @FileHeader$)

 ; If FileHeader$ = "tn2scores"

     If Logic = #FILE_SAVE
       ;MOVE CURRENT SCORES TO PREVIOUS
       
       For i = 0 To 140
         For j = 0 To 2
           For k = 0 To 1

             lTemp = AnswerCorrect(0, i, j, k)
             AnswerCorrect(1, i, j, k) = AnswerCorrect(0, i, j, k)
             AnswerIncorrect(1, i, j, k) = AnswerIncorrect(0, i, j, k)

             AnswerCorrect(2, i, j, k) = AnswerCorrect(2, i, j, k) + AnswerCorrect(0, i, j, k)
             AnswerIncorrect(2, i, j, k) = AnswerIncorrect(2, i, j, k) + AnswerIncorrect(0, i, j, k)


             AnswerCorrect(0, i, j, k) = 0
             AnswerIncorrect(0, i, j, k) = 0

           Next k
         Next j
       Next i


       ;SettingsWriteReadBinary(Logic, @AnswerCorrect(0, 0, 0, 0), BytesToWrite)
       ;SettingsWriteReadBinary(Logic, @AnswerIncorrect(0, 0, 0, 0), BytesToWrite)
       *mem = @AnswerCorrect()
       WriteData(1, *mem, BytesToWrite*4)

       FlushFileBuffers(1)
       Debug Loc(1)
       ;FileSeek(1, Loc(1))
       *mem = @AnswerIncorrect()
       WriteData(1, *mem, BytesToWrite*4)
       Debug Loc(1)


     Else
       SettingsWriteReadBinary(Logic, @AnswerCorrect(0, 0, 0, 0), BytesToWrite)
       SettingsWriteReadBinary(Logic, @AnswerIncorrect(0, 0, 0, 0), BytesToWrite)

     EndIf


  ; Else
  ;   Globe\MessageBoxText = "File is not a Take Note 2.1 score File!  File Not Loaded"+ Chr(0)
  ;  Globe\MessageBoxTitle = "Warning!"
  ;  DoMessageBox(MainWindow\Handle, 1)

  ; EndIf

   CloseFile(1)
   SetWindowText_(hwndAttach, @szWindowTitle$)
I'd be thankful to know I'm doing something stupid.

Jon
jonljacobi
User
User
Posts: 67
Joined: Mon Jan 16, 2006 10:12 pm

Post by jonljacobi »

Hmmm, I was multipying the the byte count by 4 twice which made it write four times as much memory as it should have.

Cutting that back to the single multiplication, the routine now works.

Who knows what I was stepping on?

Cheers, Jon

sigh..
Post Reply