PlanetSquires Forums

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: how to read an entire text file into a string?  (Read 94 times)

ChrisC

  • FireFly3 User
  • Little Newbie FireFly
  • *
  • Posts: 27
how to read an entire text file into a string?
« on: August 03, 2018, 01:11:33 PM »

Hello all

can someone please give me some code to read an entire text file into a string?

and what kind of string shall i use ?  WSTRING or ordinary string

thanks a lot
Logged

Pierre Bellisle

  • FireFly3 User
  • Junior Member
  • *
  • Posts: 69
Re: how to read an entire text file into a string?
« Reply #1 on: August 03, 2018, 02:19:42 PM »

Try this...

Code: [Select]
'_____________________________________________________________________________

FUNCTION FileDataGet(BYVAL sFileName AS STRING, BYREF sFileData AS STRING) AS UINT32
 Dim hFile        AS HWND
 Dim FileSizeLo   AS DWORD
 Dim FileSizeHi   AS DWORD
 Dim ReturnCount  AS DWORD

 sFileData = ""
 hFile = CreateFileA(Cast(LPCSTR, STRPTR(sFileName)), GENERIC_READ, FILE_SHARE_READ, _
                     BYVAL 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
 IF hFile <> INVALID_HANDLE_VALUE THEN
   FileSizeLo = GetFileSize(hFile, @FileSizeHi)
   sFileData  = String(FileSizeLo, 0)
   ReadFile(hFile, Cast(LPVOID, STRPTR(sFileData)), FileSizeLo, @ReturnCount, BYVAL 0)
   CloseHandle(hFile)
   IF ReturnCount = FileSizeLo THEN
     FUNCTION = ReturnCount
   END IF
 END IF

END FUNCTION
'_____________________________________________________________________________

FUNCTION FileDataSet(BYVAL sFileName AS STRING, BYREF sFileData AS STRING) AS UINT32
 Dim hFile        AS HWND
 Dim WrittenCount AS DWORD

 hFile = CreateFileA(Cast(LPCSTR, STRPTR(sFileName)), GENERIC_READ OR GENERIC_WRITE, _
                     FILE_SHARE_READ, BYVAL 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
 IF hFile <> INVALID_HANDLE_VALUE THEN
   WriteFile(hFile, Cast(LPCSTR, STRPTR(sFileData)), LEN(sFileData), @WrittenCount, BYVAL 0)
   SetEndOfFile(hFile)
   CloseHandle(hFile)
   IF WrittenCount = LEN(sFileData) THEN
     FUNCTION = WrittenCount
   END IF
 END IF

END FUNCTION
'_____________________________________________________________________________
'
Logged

Josť Roca

  • FireFly3 Registered User
  • Master Member
  • *
  • Posts: 3028
    • Jos
Re: how to read an entire text file into a string?
« Reply #2 on: August 03, 2018, 02:35:42 PM »

What kind of string you have to use depends on if the contents of the file are ansi or unicode.