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 905 times)

ChrisC

  • Little Newbie
  • *
  • 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

  • Junior Member
  • **
  • Posts: 85
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

  • Guru Member
  • *****
  • Posts: 3187
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.

Jim Dunn

  • Junior Member
  • **
  • Posts: 111
  • Jim Dunn
Re: how to read an entire text file into a string?
« Reply #3 on: April 07, 2019, 12:01:23 AM »

(and don't forget about processing line-endings (CR or LF or CRLF))

I usually do something like this:

Code: [Select]
    fpIN = FREEFILE
    OPEN sfilename FOR BINARY AS #fpIN
    IF ERR THEN
        STDERR "File [" & sfilename & "] could not be opened."
        EXIT FUNCTION
    END IF
    GET$ #fpIN, LOF(#fpIN), a
    CLOSE #fpIN

    REPLACE $CRLF WITH $CR IN a ' for Windows line-endings
    REPLACE $LF   WITH $CR IN a ' for Linux line-endings
    REPLACE $CR   WITH $CRLF IN a ' for Mac line-endings

Sorry it's "PowerBASIC"... haven't moved to FreeBasic yet...
« Last Edit: April 07, 2019, 12:24:53 AM by Jim Dunn »
Logged
3.14159265358979323846264338327950
"Ok, yes... I like pie... um, I meant, pi."

Johan Klassen

  • Junior Member
  • **
  • Posts: 85
  • FF3 User
Logged

Josť Roca

  • Guru Member
  • *****
  • Posts: 3187
Re: how to read an entire text file into a string?
« Reply #5 on: April 09, 2019, 12:14:23 PM »

My CFileStream class works both with ansi and unicode.

Code: [Select]
#INCLUDE ONCE "Afx/AfxWin.inc"
#INCLUDE ONCE "Afx/CStream.inc"
USING Afx

' // Ansi
DIM pstm AS CFileStream
pstm.Open(AfxGetExePath & "\TextA1.txt", STGM_READ)
DIM strText AS STRING = pstm.ReadTextA(-1)

' // Unicode
DIM pstm2 AS CFileStream
pstm2.Open(AfxGetExePath & "\TextW1.txt", STGM_READ)
DIM cwsText AS CWSTR = pstm2.ReadTextW(-1)

Besides being easier to use.
« Last Edit: April 09, 2019, 12:38:42 PM by Josť Roca »
Logged

Josť Roca

  • Guru Member
  • *****
  • Posts: 3187
Re: how to read an entire text file into a string?
« Reply #6 on: April 09, 2019, 12:36:55 PM »

With text files, we can use CTextStream instead.

Code: [Select]
'#CONSOLE ON
#INCLUDE ONCE "Afx/AfxWin.inc"
#INCLUDE ONCE "Afx/CTextStream.inc"
USING Afx

' // Ansi
DIM pstm AS CTextStream
pstm.OpenForInputA(AfxGetExePath & "\TextA1.txt")
DIM strText AS STRING = pstm.ReadAll
PRINT strText

' // Unicode
DIM pstm2 AS CTextStream
pstm2.OpenForInputW(AfxGetExePath & "\TextW1.txt")
DIM cbsText AS CBSTR = pstm2.ReadAll
PRINT cbsText