• Welcome to PlanetSquires Forums.
 

how to read an entire text file into a string?

Started by ChrisC, August 03, 2018, 01:11:33 PM

Previous topic - Next topic

ChrisC

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

Pierre Bellisle

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

José Roca

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

Jim Dunn

#3
(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...
3.14159265358979323846264338327950
"Ok, yes... I like pie... um, I meant, pi."


José Roca

#5
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.

José Roca

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