• Welcome to Valhalla Legends Archive.
 

Writefile API problem

Started by Blade, August 02, 2004, 11:27 PM

Previous topic - Next topic

Blade

The Problem is that it prints wierd characters to the file instead of the string I tell it to. Here is my code.


My Declairs, types and constants.

Public Declare Function WriteFile Lib "kernel32" _
(ByVal hFile As Long, lpBuffer As Any, ByVal _
nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, _
lpOverlapped As OVERLAPPED) As Long

Public Declare Function OpenFile Lib "kernel32" _
(ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, _
ByVal wStyle As Long) As Long

Public Declare Function GetFileSize Lib "kernel32" _
(ByVal hFile As Long, lpFileSizeHigh As Long) As Long

Public Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long

Public Const OFS_MAXPATHNAME = 128
Public Const OF_WRITE = &H1
Public Const GENERIC_WRITE = &H40000000


Public Type OVERLAPPED
       Internal As Long
       InternalHigh As Long
       offset As Long
       OffsetHigh As Long
       hEvent As Long
End Type

Public Type OFSTRUCT
       cBytes As Byte
       fFixedDisk As Byte
       nErrCode As Integer
       Reserved1 As Integer
       Reserved2 As Integer
       szPathName(OFS_MAXPATHNAME) As Byte
End Type



The sub.

Public Sub savefile(strFile As String, strData As String)
Dim hHandle As Long
Dim ofData As OFSTRUCT
Dim lngFilesize As Long
Dim lngRead As Long
Dim overlap As OVERLAPPED

   hHandle = OpenFile(fileDB, ofData, OF_WRITE)
   lngFilesize = GetFileSize(hHandle, 0)
   WriteFile hHandle, strData, Len(DBdata), lngRead, overlap
   CloseHandle hHandle
   
End Sub




Usage.

dim arrData(2) as string
dim strData as string
dim strFile as string

     arrData(0) = "blah"
     arrData(1) = "ha"
     arrData(2) = "something"
     strData = join(arrData(), vbCrLf)
     strFile = app.path & "\textfile.txt"
     call savefile(strFile, strData)


The file is already created and is empty, after I call the savefile sub in my program it works with no errors but it prints wierd characters to the file. I've used GENERIC_WRITE also but that doesn't write anything to the file.  Whats wrong?

phvckmeh

#1

Open (App.Path & "\log.txt") For Append As #1
Print #1,  "My String!"
Close #1


it will make log.txt if it does not exist

Blade

Yes I know that but I want to use the API because it's supposed to be faster. Any Idea why it doesn't work right?

Banana fanna fo fanna

It won't be noticeably faster.

Tuberload

Quote from: $t0rm on August 03, 2004, 10:58 PM
It won't be noticeably faster.
Compared to using a binary stream in VB, yes it is noticeably faster.
Quote"Pray not for lighter burdens, but for stronger backs." -- Teddy Roosevelt
"Your forefathers have given you freedom, so good luck, see you around, hope you make it" -- Unknown

Blade

So any ideas whats wrong here?

Banana fanna fo fanna

Quote from: Tuberload on August 03, 2004, 11:08 PM
Quote from: $t0rm on August 03, 2004, 10:58 PM
It won't be noticeably faster.
Compared to using a binary stream in VB, yes it is noticeably faster.

If < .25sec than I don't consider it noticeable.

MyndFyre

Chances are that it would be noticeably faster if you were using many accesses in a row, but just for one file?  Pah.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Banana fanna fo fanna


Tuberload

My point was aimed more towards programs that rely heavily on disk IO. My point is also based on fact not chance. The fact of the matter is the API is faster, and VB is <insertwhateveryouwanthere>.

Now my question: wtf is "pah"?
Quote"Pray not for lighter burdens, but for stronger backs." -- Teddy Roosevelt
"Your forefathers have given you freedom, so good luck, see you around, hope you make it" -- Unknown

Banana fanna fo fanna

Quote from: Tuberload on August 05, 2004, 01:39 AM
Now my question: wtf is "pah"?

Yes, I was wondering that myself.

Flame


Public Sub savefile(strFile As String, strData As String)
Dim hHandle As Long
Dim ofData As OFSTRUCT
Dim lngFilesize As Long
Dim lngRead As Long
Dim overlap As OVERLAPPED

   hHandle = OpenFile(fileDB, ofData, OF_WRITE)
   lngFilesize = GetFileSize(hHandle, 0)
   WriteFile hHandle, DBdata, Len(DBdata), lngRead, overlap
   CloseHandle hHandle
   
End Sub


Where does DBdata come from?  It looks to me like it should be strData

MyndFyre

Quote from: Tuberload on August 05, 2004, 01:39 AM
My point was aimed more towards programs that rely heavily on disk IO. My point is also based on fact not chance. The fact of the matter is the API is faster, and VB is <insertwhateveryouwanthere>.

Now my question: wtf is "pah"?

Honestly, though, my statement was more fact than chance too.  I was just trying to preface it with something that would be more disarming.  Damned if I don't find some way to offend you with everything I say.

And as far as "pah" goes, it means the same as "bah," but it's also more of a lazy, "you-should-know-that-or-else-you're-a-dumbass" tone.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Tuberload

Quote from: Myndfyre on August 05, 2004, 03:29 PM
Quote from: Tuberload on August 05, 2004, 01:39 AM
My point was aimed more towards programs that rely heavily on disk IO. My point is also based on fact not chance. The fact of the matter is the API is faster, and VB is <insertwhateveryouwanthere>.

Now my question: wtf is "pah"?

Honestly, though, my statement was more fact than chance too.  I was just trying to preface it with something that would be more disarming.  Damned if I don't find some way to offend you with everything I say.

And as far as "pah" goes, it means the same as "bah," but it's also more of a lazy, "you-should-know-that-or-else-you're-a-dumbass" tone.

You did not offend me, and that response was not directed solely towards you, that is why I did not quote you. Damned if I cannot find a way to make you believe that. I just stated a simple fact and everybody was arguing it. Thanks for the definition of pah btw.
Quote"Pray not for lighter burdens, but for stronger backs." -- Teddy Roosevelt
"Your forefathers have given you freedom, so good luck, see you around, hope you make it" -- Unknown

Blade

Quote from: Flame on August 05, 2004, 02:36 PM

Public Sub savefile(strFile As String, strData As String)
Dim hHandle As Long
Dim ofData As OFSTRUCT
Dim lngFilesize As Long
Dim lngRead As Long
Dim overlap As OVERLAPPED

   hHandle = OpenFile(fileDB, ofData, OF_WRITE)
   lngFilesize = GetFileSize(hHandle, 0)
   WriteFile hHandle, DBdata, Len(DBdata), lngRead, overlap
   CloseHandle hHandle
   
End Sub


Where does DBdata come from?  It looks to me like it should be strData

Yes that was an error in me copying it to this forum and I fixed it here, but there is no such error in my code so whats wrong!