PlanetSquires Forums

Support Forums => General Board => Topic started by: ChrisC on August 03, 2018, 01:41:33 PM

Title: how to read an entire text file into a string?
Post by: ChrisC on August 03, 2018, 01:41: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
Title: Re: how to read an entire text file into a string?
Post by: Pierre Bellisle on August 03, 2018, 02:49:42 PM
Try this...


'_____________________________________________________________________________

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
'_____________________________________________________________________________
'
Title: Re: how to read an entire text file into a string?
Post by: José Roca on August 03, 2018, 03:05:42 PM
What kind of string you have to use depends on if the contents of the file are ansi or unicode.
Title: Re: how to read an entire text file into a string?
Post by: Jim Dunn on April 07, 2019, 12:31:23 AM
(and don't forget about processing line-endings (CR or LF or CRLF))

I usually do something like this:

    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...
Title: Re: how to read an entire text file into a string?
Post by: Johan Klassen on April 09, 2019, 09:31:26 AM
here are some ways https://www.freebasic.net/forum/viewtopic.php?p=243407
Title: Re: how to read an entire text file into a string?
Post by: José Roca on April 09, 2019, 12:44:23 PM
My CFileStream class works both with ansi and unicode.


#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.
Title: Re: how to read an entire text file into a string?
Post by: José Roca on April 09, 2019, 01:06:55 PM
With text files, we can use CTextStream instead.


'#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