How to use INPUT() to read the entire content of a text document

New to FreeBASIC? Post your questions here.
Post Reply
objet-a
Posts: 13
Joined: Jul 13, 2021 0:04
Location: Hubei, China

How to use INPUT() to read the entire content of a text document

Post by objet-a »

At first, I tried using BINARY type to read, soon I canceled this idea due to unsupported set encoding type.
I also tried VB code. However, it seems is not compatible with FB.

Code: Select all

Function LoadFile(ByRef filename As String,code as string="ascii") As string
    Dim h As Integer
    Dim txt As String
	dim buffer as string
	'[need] error handle
    h = FreeFile
   if code="ascii" then
    If Open ( filename For Binary Access Read As #h ) <> 0 Then Return ""
	If LOF (h) > 0 then
       
        txt = String (LOF ( h ) , 0 )
        If Get ( #h, ,txt ) <> 0 Then txt = ""
       
        End If
   
    Close #h
    Return txt
   else 
   
    Open filename For Input encoding code As #h	
    Do While Not EOF(h) ' vb code
      /'line'/ Input #h, buffer
	   txt+=buffer
    Loop
	close #h
	Return txt
   endif
End Function
LoadFile("@Test.txt","utf-8") ' crash or empty text....
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: How to use INPUT() to read the entire content of a text document

Post by grindstone »

Not sure what you intend to do nor what exactly your problem is, but have a look at this modified sample from https://www.freebasic.net/wiki/KeyPgEncoding

Code: Select all

'' This example will:
'' 1) Write a string to a text file with utf-16 encoding
'' 2) Display the byte contents of the file
'' 3) Read the text back from the file
''
'' WSTRING's will work as well but STRING has been
'' used in this example since not all consoles support
'' printing WSTRING's.

'' The name of the file to use in this example
Dim f As String
f = "sample.txt"

''
Scope
	Dim s As String
	s = "FreeBASIC"
	
	Print "Text to write to " + f + ":"
	Print s
	Print
	
	'' open a file for output using utf-16 encoding
	'' and print a short message
	Open f For Output Encoding "utf-16" As #1
	
	'' The ascii string is converted to utf-16
	Print #1, s;
	Close #1
End Scope

''
Scope
	Dim s As String, n As LongInt
	
	'' open the same file for binary and read all the bytes
	Open f For Binary As #1
	n = Lof(1)
	s = Space( n )
	Get #1,,s
	Close #1
	
	Print "Binary contents of " + f + ":"
	For i As Integer = 1 To n
		Print Hex( Asc( Mid( s, i, 1 )), 2); " ";
	Next
	Print
	Print

End Scope

''
Scope
	Dim s As String
	
	'' open a file for input using utf-16 encoding
	'' and read back the message
	Open f For Input Encoding "utf-16" As #1
	
	'' The ascii string is converted from utf-16
	Line Input #1, s
	Close #1
	
	'' Display the text
	Print "Text read from " + f + ":"
	Print s
	Print
End Scope

Scope
	Dim As String s
	Open f For Binary As #1
	s = Input(Lof(1), #1) 'read the whole content of the file
	Print "Length of file: ";Lof(1)
	Print "Length of string: ";Len(s)
	Print s
	Print
	Close 1
End Scope

Scope
	Dim As String s
	Open f For Input Encoding "utf-16" As #1
	s = Input(Lof(1), #1) 'read the whole content of the file
	Print "Length of file: ";Lof(1)
	Print "Length of string: ";Len(s)
	Print s
	Close 1
End Scope

Sleep
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to use INPUT() to read the entire content of a text document

Post by dodicat »

You could try the barebones ushort pointer to handle non ascii text.
This ensures you can capture wchr(0) if needed.
You can overload (the subs) to handle ascii.
The C runtime is needed to block load block save and handle non ascii filenames.

Code: Select all


#include "crt.bi"
#include "file.bi"

Function filelength(filename As wstring Ptr)As Long
      Dim As wstring * 4 k="r"
      Var fp=_wfopen(filename,@k)
      If fp = 0 Then Print "Error opening file":Sleep:End
      fseek(fp, 0, SEEK_END)
      Var length=ftell(fp)
      fclose(fp)
      Return(length)
End Function

Sub savefile overload(content As String ,filename As wstring Ptr)
      Dim As wstring * 4 k="wb"
      Var fp= _wfopen(filename,@k)
      If fp = 0 Then Print "Error opening file":Sleep:End
      fwrite(@content[0], 1, Len(content), fp)
      fclose(fp)
End Sub

Sub loadfile overload( content As String,filename As wString Ptr)
      Var l=Filelength(filename)
      content=String(l,0)
      Dim As wstring * 4 k="rb"
      Var fp= _wfopen(filename,@k)
      If fp = 0 Then Print "Error loading file ";filename:Sleep:End
      fread(@content[0], 1,l, fp)
      fclose(fp)
End Sub

Sub savefile(content As ushort ptr ,filename As wstring Ptr)
      Dim As wstring * 4 k="wb"
      Var fp= _wfopen(filename,@k)
      If fp = 0 Then Print "Error opening file":Sleep:End
      fwrite(content, 2, Len(*cast(wstring ptr,content)), fp)
      fclose(fp)
End Sub

Sub loadfile(content As ushort ptr,filename As wString Ptr)
      Var l=Filelength(filename)
      Dim As wstring * 4 k="rb"
      Var fp= _wfopen(filename,@k)
      If fp = 0 Then Print "Error loading file ";filename:Sleep:End
      fread(content, 2,l, fp)
      fclose(fp)
End Sub

Function exists(filename As wString Ptr) As boolean
      Dim As wstring * 4 k="r"
      Var fp= _wfopen(filename,@k)
      If fp=0 Then
            Return false
      Else
            fclose(fp)
            Return true
      End If
      
End Function

dim as wstring * 20 zw=wchr(&h48,&h65,&h6c,&h6c,&h6f,&h20,&h57,&h6f,&h72,&h6c,&h64,&h21,&h20)

Dim As wstring * 40 filename=wchr(&h48,&h65,&h6c,&h6c,&h6f,&h20,&h57,&h6f,&h72,&h6c,&h64,&h21,&h20)+".txt"
Print "Filename ";filename

dim as wstring * 10000 w=zw

for n as long=1 to 7
      w+=w
next
print "Wstring length ";len(w)
print "To file:"
print w

savefile(@w[0],filename)
print
print "filelen "; filelen(filename)

redim as ushort us(1 to filelen(filename)\2)
loadfile(@us(1),filename)

dim as wstring * 10000 ans
for n as long=1 to ubound(us)
    ans+= wchr(us(n))
next
print
print "From file:"

print ans

print
print "Are loaded wstring and returned wstring equal? "; cbool(ans=w)


savefile("Goodbye from overloaded sub, press a key to exit","dummy.txt")
dim as string s
loadfile(s,"dummy.txt")
print s

_wremove(@filename)
kill "dummy.txt"
Print "Does file " ; filename; " exist? ";exists(filename)
Print "Does file dummy.txt exist? ";exists("dummy.txt")
Sleep

 
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: How to use INPUT() to read the entire content of a text document

Post by srvaldez »

here are some more ideas viewtopic.php?t=26410
objet-a
Posts: 13
Joined: Jul 13, 2021 0:04
Location: Hubei, China

Re: How to use INPUT() to read the entire content of a text document

Post by objet-a »

dodicat wrote: Apr 21, 2022 20:17 You could try the barebones ushort pointer to handle non ascii text.
This ensures you can capture wchr(0) if needed.
You can overload (the subs) to handle ascii.
The C runtime is needed to block load block save and handle non ascii filenames.

Code: Select all


#include "crt.bi"
#include "file.bi"

Function filelength(filename As wstring Ptr)As Long
      Dim As wstring * 4 k="r"
      Var fp=_wfopen(filename,@k)
      If fp = 0 Then Print "Error opening file":Sleep:End
      fseek(fp, 0, SEEK_END)
      Var length=ftell(fp)
      fclose(fp)
      Return(length)
End Function

Sub savefile overload(content As String ,filename As wstring Ptr)
      Dim As wstring * 4 k="wb"
      Var fp= _wfopen(filename,@k)
      If fp = 0 Then Print "Error opening file":Sleep:End
      fwrite(@content[0], 1, Len(content), fp)
      fclose(fp)
End Sub

Sub loadfile overload( content As String,filename As wString Ptr)
      Var l=Filelength(filename)
      content=String(l,0)
      Dim As wstring * 4 k="rb"
      Var fp= _wfopen(filename,@k)
      If fp = 0 Then Print "Error loading file ";filename:Sleep:End
      fread(@content[0], 1,l, fp)
      fclose(fp)
End Sub

Sub savefile(content As ushort ptr ,filename As wstring Ptr)
      Dim As wstring * 4 k="wb"
      Var fp= _wfopen(filename,@k)
      If fp = 0 Then Print "Error opening file":Sleep:End
      fwrite(content, 2, Len(*cast(wstring ptr,content)), fp)
      fclose(fp)
End Sub

Sub loadfile(content As ushort ptr,filename As wString Ptr)
      Var l=Filelength(filename)
      Dim As wstring * 4 k="rb"
      Var fp= _wfopen(filename,@k)
      If fp = 0 Then Print "Error loading file ";filename:Sleep:End
      fread(content, 2,l, fp)
      fclose(fp)
End Sub

Function exists(filename As wString Ptr) As boolean
      Dim As wstring * 4 k="r"
      Var fp= _wfopen(filename,@k)
      If fp=0 Then
            Return false
      Else
            fclose(fp)
            Return true
      End If
      
End Function

dim as wstring * 20 zw=wchr(&h48,&h65,&h6c,&h6c,&h6f,&h20,&h57,&h6f,&h72,&h6c,&h64,&h21,&h20)

Dim As wstring * 40 filename=wchr(&h48,&h65,&h6c,&h6c,&h6f,&h20,&h57,&h6f,&h72,&h6c,&h64,&h21,&h20)+".txt"
Print "Filename ";filename

dim as wstring * 10000 w=zw

for n as long=1 to 7
      w+=w
next
print "Wstring length ";len(w)
print "To file:"
print w

savefile(@w[0],filename)
print
print "filelen "; filelen(filename)

redim as ushort us(1 to filelen(filename)\2)
loadfile(@us(1),filename)

dim as wstring * 10000 ans
for n as long=1 to ubound(us)
    ans+= wchr(us(n))
next
print
print "From file:"

print ans

print
print "Are loaded wstring and returned wstring equal? "; cbool(ans=w)


savefile("Goodbye from overloaded sub, press a key to exit","dummy.txt")
dim as string s
loadfile(s,"dummy.txt")
print s

_wremove(@filename)
kill "dummy.txt"
Print "Does file " ; filename; " exist? ";exists(filename)
Print "Does file dummy.txt exist? ";exists("dummy.txt")
Sleep

 
Converting a Unicode file name to hexadecimal is not a good idea.
Source code files should be saved in UTF-8 with BOM format(https://www.freebasic.net/wiki/ProPgSourceFiles)
That way, the FBC can identify
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to use INPUT() to read the entire content of a text document

Post by dodicat »

I am just trying to solve for the worst possible scenarios, wstring ptr filename with ushort ptr content.
The lowest denominator, ushort pointer for the file content.
(You did mention encoding type, which I assume you mean non-ascii)
The hexidecimal format for utf8 is that used by various web sites e.g. https://www.browserling.com/tools/utf8-encode.
But you can try any utf8 filename, and it should be accepted by the crt functions, whereas fb's open will not work with utf8 filenames (wstring)
True, you didn't mention wstring filenames, but I included them anyway, and using the crt file functions is an alternative.
Grindstone and srvaldez cover the use of input for file content with encoding.
Post Reply