CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2000
    Posts
    163

    Read file to String variable

    Hello

    Is any body know how can I read File to String Variable ??? (With API function)

    I try with ReadFile, but it read to Byte Array, and it take too long time to convert it to string.

    Thanks for any help or examples.


  2. #2
    Join Date
    Apr 2000
    Posts
    737

    Re: Read file to String variable

    try http://vblib.virtualave.net, there is a function called ReadFileStr which read a entire file into string variable

    HTH




  3. #3
    Join Date
    Dec 2000
    Posts
    163

    Re: Read file to String variable

    Thanks, But the fucntion there use with VB function, and I want to read the file with API functions


  4. #4
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Read file to String variable

    Declare Function ReadFile Lib "kernel32.dll" (ByVal hFile As Long, lpBuffer As Any, _
    ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Any) As Long

    Description & Usage
    ReadFile reads the desired amount of bytes from a file. The file must of course have been opened with at
    least read-level access. If the file is synchronous (not overlapped), the function begins reading the file from
    the current position of the file pointer, and the function automatically adjusts the file pointer to point to the
    byte immediately after the last byte read. If the file is asynchronous (overlapped), the structure passed as
    lpOverlapped identifies the point to begin reading from, and the program calling the function is responsible for
    updating the file pointer.

    Return Value
    If an error occured, the function returns 0 (use GetLastError to get the error code).
    If successful (including if the function attempted to read past the end of the file), the function returns a non-zero value.

    Parameters

    hFile
    A handle to the file to read from. The file must have at least read-level access.
    lpBuffer
    The variable, array, or structure which receives the data read from the file.
    nNumberOfBytesToRead
    The number of bytes of data to read from the file and put into lpBuffer.
    lpNumberOfBytesRead
    Receives the number of bytes of data actually read from the file. If this is less than lpNumberOfBytesToRead, the function has attempted to read beyond the end of the file.
    lpOverlapped
    If the file is asynchronous (overlapped), this is an OVERLAPPED structure specifying where to begin reading from. If the file is synchronous (not overlapped), this must be 0.

    Example

    ' Read both a Long (32-bit) number and a String from the file
    ' C:\Test\myfile.txt. Notice how the ByVal keyword must be used
    ' when reading a string variable.
    Dim longbuffer As Long ' receives long read from file
    Dim stringbuffer As String ' receives string read from file
    Dim numread As Long ' receives number of bytes read from file
    Dim hFile As Long ' handle of the open file
    Dim retval As Long ' return value

    ' Open the file for read-level access.
    hFile = CreateFile("C:\Test\myfile.txt", GENERAL_READ, FILE_SHARE_READ, ByVal CLng(0), OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, 0)
    If hfile = -1 Then ' the file could not be opened
    Debug.Print "Unable to open the file -- it probably does not exist."
    End ' abort the program
    End If

    ' Read a Long-type number from the file
    retval = ReadFile(hFile, longbuffer, Len(longbuffer), numread, ByVal CLng(0))
    If numread < Len(longbuffer) Then ' EOF reached
    Debug.Print "End of file encountered -- could not read the data."
    Else
    Debug.Print "Number read from file:"; longbuffer
    End If

    ' Read a 10-character string from the file
    stringbuffer = Space(10) ' make room in the buffer
    retval = ReadFile(hFile, ByVal stringbuffer, 10, numread, ByVal CLng(0))
    If numread = 0 Then ' EOF reached
    Debug.Print "End of file encountered -- could not read any data."
    ElseIf numread < 10 Then ' read between 0 and 10 bytes
    Debug.Print "Incomplete string read: "; Left(stringbuffer, numread)
    Else
    Debug.Print "String read from file: "; stringbuffer
    End If

    ' Close the file.
    retval = CloseHandle(hFile)


    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  5. #5
    Join Date
    Jun 2000
    Location
    Nepal
    Posts
    108

    Re: Read file to String variable


    To convert a byte array to a string, you can simply assign it to the string variable. For example, the following code correctly produces the output ABC. Note that the byte array is in Unicode format. You can use the StrConv function (VB) to convert to and from Unicode.


    Dim arrByte(5) as Byte, str as string
    arrByte(0) = 65
    arrByte(1) = 0
    arrByte(2) = 66
    arrByte(3) = 0
    arrByte(4) = 67
    arrByte(5) = 0
    str = arrByte
    MsgBox str





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured