what is wrong with this piece of code?? 
Author Message
 what is wrong with this piece of code??

hello,
I can not read from the file.
I can not write to a file propperly.

greetings
frank

Option Explicit

Public Declare Function ReadFile Lib "Coredll" (ByVal hFile As Long,
lpBuffer As String, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead
As Long, lpOverlapped As Long) As Long
Public Declare Function WriteFile Lib "Coredll" (ByVal hFile As Long,
lpBuffer As String, ByVal nNumberOfBytesToWrite As Long,
lpNumberOfBytesWritten As Long, lpOverlapped As Long) As Long
Public Declare Function CreateFile Lib "Coredll" Alias "CreateFileW" (ByVal
lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As
Long, lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long,
ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Public Declare Function SetFilePointer Lib "Coredll" (ByVal hFile As Long,
ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal
dwMoveMethod As Long) As Long
Public Declare Function CloseHandle Lib "Coredll" (ByVal hObject As Long) As
Long

Const GENERIC_READ = &H80000000
Const GENERIC_WRITE = &H40000000
Const GENERIC_EXECUTE = &H20000000
Const GENERIC_ALL = &H10000000

Const CREATE_NEW = 1
Const CREATE_ALWAYS = 2
Const OPEN_EXISTING = 3
Const OPEN_ALWAYS = 4
Const TRUNCATE_EXISTING = 5

Const FILE_SHARE_READ = &H1
Const FILE_SHARE_WRITE = &H2
Const FILE_ATTRIBUTE_READONLY = &H1
Const FILE_ATTRIBUTE_HIDDEN = &H2
Const FILE_ATTRIBUTE_SYSTEM = &H4
Const FILE_ATTRIBUTE_DIRECTORY = &H10
Const FILE_ATTRIBUTE_ARCHIVE = &H20
Const FILE_ATTRIBUTE_NORMAL = &H80
Const FILE_ATTRIBUTE_TEMPORARY = &H100
Const FILE_ATTRIBUTE_COMPRESSED = &H800
Const FILE_NOTIFY_CHANGE_FILE_NAME = &H1
Const FILE_NOTIFY_CHANGE_DIR_NAME = &H2
Const FILE_NOTIFY_CHANGE_ATTRIBUTES = &H4
Const FILE_NOTIFY_CHANGE_SIZE = &H8
Const FILE_NOTIFY_CHANGE_LAST_WRITE = &H10
Const FILE_NOTIFY_CHANGE_SECURITY = &H100
Const MAILSLOT_NO_MESSAGE = (-1)
Const MAILSLOT_WAIT_FOREVER = (-1)
Const FILE_CASE_SENSITIVE_SEARCH = &H1
Const FILE_CASE_PRESERVED_NAMES = &H2
Const FILE_UNICODE_ON_DISK = &H4
Const FILE_PERSISTENT_ACLS = &H8
Const FILE_FILE_COMPRESSION = &H10
Const FILE_VOLUME_IS_COMPRESSED = &H8000
Const IO_COMPLETION_MODIFY_STATE = &H2

Const FILE_BEGIN = 0
Const FILE_CURRENT = 1
Const FILE_END = 2

Const FILE_FLAG_WRITE_THROUGH = &H80000000
Const FILE_FLAG_OVERLAPPED = &H40000000
Const FILE_FLAG_NO_BUFFERING = &H20000000
Const FILE_FLAG_RANDOM_ACCESS = &H10000000
Const FILE_FLAG_SEQUENTIAL_SCAN = &H8000000
Const FILE_FLAG_DELETE_ON_CLOSE = &H4000000
Const FILE_FLAG_BACKUP_SEMANTICS = &H2000000
Const FILE_FLAG_POSIX_SEMANTICS = &H1000000

'******************************** modul
************************************************/
Private Sub cmdStart_Click()
    Dim r_datei As String
    Dim w_datei As String
    Dim w_file As Long 'handle write file
    Dim r_file As Long 'handle read file

    Dim x As Long

    Dim w_buff As String    'write buffer
    Dim w_bytes As Long     'write nrs of bytes
    Dim w_bytesRead As Long 'written bytes
    Dim w_pos As Long       'file position

    Dim r_buff As String    'read buffer
    Dim r_bytes As Long     'read nrs of bytes
    Dim r_bytesRead As Long 'gelesene zeichen

    Dim dwDesiredAccess As Long
    Dim dwShareMode As Long
    Dim lpSecurityAttributes As Long
    Dim dwCreationDisposition As Long
    Dim dwFlagsAndAttributes As Long
    Dim hTemplateFile As Long

    On Error Resume Next
    Err.Clear

    '********************** write file
    w_datei = App.Path & "\" & "2.txt"
    dwDesiredAccess = GENERIC_WRITE '3
    dwShareMode = 0
    lpSecurityAttributes = 0
    dwCreationDisposition = CREATE_NEW
    dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL '0
    hTemplateFile = 0
    nNumberOfBytesToWrite = 1
    lpNumberOfBytesWritten = 0
    lpOverlapped = 0
    w_file = CreateFile(w_datei, dwDesiredAccess, dwShareMode,
lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes,
hTemplateFile)

    '************************* read file
    r_datei = App.Path & "\" & "1.txt"
    dwDesiredAccess = GENERIC_READ
    dwShareMode = 0
    lpSecurityAttributes = 0
    dwCreationDisposition = OPEN_EXISTING
    dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL '0
    hTemplateFile = 0
    r_bytes = 1
    r_bytesRead = 0
    r_file = CreateFile(r_datei, dwDesiredAccess, dwShareMode,
lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes,
hTemplateFile)

    '************************ read and write
    x = 1
    While (x = ReadFile(r_file, r_buff, r_bytes, r_bytesRead,
FILE_FLAG_OVERLAPPED) > 0)
        w_pos = SetFilePointer(w_file, 0, 0, FILE_END)
        WriteFile w_file, r_buff, nNumberOfBytesToWrite,
lpNumberOfBytesWritten, FILE_FLAG_OVERLAPPED
    Wend

    '************************* close files
    CloseHandle (r_file)
    CloseHandle (w_file)

End Sub
'***************************************************************************
****************/



Sat, 03 Jan 2004 19:18:56 GMT  
 what is wrong with this piece of code??
I have a similar problem.  I can create the file and I can write to the
file, my problems is that I cannot read from the file.  Everytime I try to
read I get an error

I am not sure if the call to the functions are wrong or if its a limitation
of eVB.  Something is missing but its not obvious.  I have been struggling
with the same issue

Enzo

Quote:
> hello,
> I can not read from the file.
> I can not write to a file propperly.

> greetings
> frank

> Option Explicit

> Public Declare Function ReadFile Lib "Coredll" (ByVal hFile As Long,
> lpBuffer As String, ByVal nNumberOfBytesToRead As Long,
lpNumberOfBytesRead
> As Long, lpOverlapped As Long) As Long
> Public Declare Function WriteFile Lib "Coredll" (ByVal hFile As Long,
> lpBuffer As String, ByVal nNumberOfBytesToWrite As Long,
> lpNumberOfBytesWritten As Long, lpOverlapped As Long) As Long
> Public Declare Function CreateFile Lib "Coredll" Alias "CreateFileW"
(ByVal
> lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As
> Long, lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long,
> ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
> Public Declare Function SetFilePointer Lib "Coredll" (ByVal hFile As Long,
> ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal
> dwMoveMethod As Long) As Long
> Public Declare Function CloseHandle Lib "Coredll" (ByVal hObject As Long)
As
> Long

> Const GENERIC_READ = &H80000000
> Const GENERIC_WRITE = &H40000000
> Const GENERIC_EXECUTE = &H20000000
> Const GENERIC_ALL = &H10000000

> Const CREATE_NEW = 1
> Const CREATE_ALWAYS = 2
> Const OPEN_EXISTING = 3
> Const OPEN_ALWAYS = 4
> Const TRUNCATE_EXISTING = 5

> Const FILE_SHARE_READ = &H1
> Const FILE_SHARE_WRITE = &H2
> Const FILE_ATTRIBUTE_READONLY = &H1
> Const FILE_ATTRIBUTE_HIDDEN = &H2
> Const FILE_ATTRIBUTE_SYSTEM = &H4
> Const FILE_ATTRIBUTE_DIRECTORY = &H10
> Const FILE_ATTRIBUTE_ARCHIVE = &H20
> Const FILE_ATTRIBUTE_NORMAL = &H80
> Const FILE_ATTRIBUTE_TEMPORARY = &H100
> Const FILE_ATTRIBUTE_COMPRESSED = &H800
> Const FILE_NOTIFY_CHANGE_FILE_NAME = &H1
> Const FILE_NOTIFY_CHANGE_DIR_NAME = &H2
> Const FILE_NOTIFY_CHANGE_ATTRIBUTES = &H4
> Const FILE_NOTIFY_CHANGE_SIZE = &H8
> Const FILE_NOTIFY_CHANGE_LAST_WRITE = &H10
> Const FILE_NOTIFY_CHANGE_SECURITY = &H100
> Const MAILSLOT_NO_MESSAGE = (-1)
> Const MAILSLOT_WAIT_FOREVER = (-1)
> Const FILE_CASE_SENSITIVE_SEARCH = &H1
> Const FILE_CASE_PRESERVED_NAMES = &H2
> Const FILE_UNICODE_ON_DISK = &H4
> Const FILE_PERSISTENT_ACLS = &H8
> Const FILE_FILE_COMPRESSION = &H10
> Const FILE_VOLUME_IS_COMPRESSED = &H8000
> Const IO_COMPLETION_MODIFY_STATE = &H2

> Const FILE_BEGIN = 0
> Const FILE_CURRENT = 1
> Const FILE_END = 2

> Const FILE_FLAG_WRITE_THROUGH = &H80000000
> Const FILE_FLAG_OVERLAPPED = &H40000000
> Const FILE_FLAG_NO_BUFFERING = &H20000000
> Const FILE_FLAG_RANDOM_ACCESS = &H10000000
> Const FILE_FLAG_SEQUENTIAL_SCAN = &H8000000
> Const FILE_FLAG_DELETE_ON_CLOSE = &H4000000
> Const FILE_FLAG_BACKUP_SEMANTICS = &H2000000
> Const FILE_FLAG_POSIX_SEMANTICS = &H1000000

> '******************************** modul
> ************************************************/
> Private Sub cmdStart_Click()
>     Dim r_datei As String
>     Dim w_datei As String
>     Dim w_file As Long 'handle write file
>     Dim r_file As Long 'handle read file

>     Dim x As Long

>     Dim w_buff As String    'write buffer
>     Dim w_bytes As Long     'write nrs of bytes
>     Dim w_bytesRead As Long 'written bytes
>     Dim w_pos As Long       'file position

>     Dim r_buff As String    'read buffer
>     Dim r_bytes As Long     'read nrs of bytes
>     Dim r_bytesRead As Long 'gelesene zeichen

>     Dim dwDesiredAccess As Long
>     Dim dwShareMode As Long
>     Dim lpSecurityAttributes As Long
>     Dim dwCreationDisposition As Long
>     Dim dwFlagsAndAttributes As Long
>     Dim hTemplateFile As Long

>     On Error Resume Next
>     Err.Clear

>     '********************** write file
>     w_datei = App.Path & "\" & "2.txt"
>     dwDesiredAccess = GENERIC_WRITE '3
>     dwShareMode = 0
>     lpSecurityAttributes = 0
>     dwCreationDisposition = CREATE_NEW
>     dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL '0
>     hTemplateFile = 0
>     nNumberOfBytesToWrite = 1
>     lpNumberOfBytesWritten = 0
>     lpOverlapped = 0
>     w_file = CreateFile(w_datei, dwDesiredAccess, dwShareMode,
> lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes,
> hTemplateFile)

>     '************************* read file
>     r_datei = App.Path & "\" & "1.txt"
>     dwDesiredAccess = GENERIC_READ
>     dwShareMode = 0
>     lpSecurityAttributes = 0
>     dwCreationDisposition = OPEN_EXISTING
>     dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL '0
>     hTemplateFile = 0
>     r_bytes = 1
>     r_bytesRead = 0
>     r_file = CreateFile(r_datei, dwDesiredAccess, dwShareMode,
> lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes,
> hTemplateFile)

>     '************************ read and write
>     x = 1
>     While (x = ReadFile(r_file, r_buff, r_bytes, r_bytesRead,
> FILE_FLAG_OVERLAPPED) > 0)
>         w_pos = SetFilePointer(w_file, 0, 0, FILE_END)
>         WriteFile w_file, r_buff, nNumberOfBytesToWrite,
> lpNumberOfBytesWritten, FILE_FLAG_OVERLAPPED
>     Wend

>     '************************* close files
>     CloseHandle (r_file)
>     CloseHandle (w_file)

> End Sub

'***************************************************************************

- Show quoted text -

Quote:
> ****************/



Sat, 03 Jan 2004 21:37:35 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. what is wrong with this piece of code ?

2. Whats wrong with this Submit piece of code?

3. what is wrong with this piece of code ?

4. help with this piece of code

5. Why does this piece of code work with Word 97 but not Word 2000

6. A very smart piece of code for databases... :)

7. Help with this piece of code ...

8. Can someone debug this small piece of code?

9. Help with piece of SQL code

10. Help with this piece of coding

11. VBX OCX or any else piece of code send email using SMTP

12. OVER 150 PIECES OF CODE FREE

 

 
Powered by phpBB® Forum Software