How can i make a FB file find something from a text file?

General FreeBASIC programming questions.
Post Reply
yoidog
Posts: 3
Joined: Jun 29, 2022 7:58

How can i make a FB file find something from a text file?

Post by yoidog »

How can you make a FreeBASIC file read from a text file.
Basically how it works:
When executed, the binary will read from a text file then find a text in that text file then execute a function.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How can i make a FB file find something from a text file?

Post by dodicat »

Simple example

Code: Select all

Function load(file As String) As String
    Dim As Long  f=Freefile
    If Open (file For Binary Access Read As #f)=0 Then
        Dim As String text
        If Lof(f) > 0 Then
            text = String(Lof(f), 0)
            Get #f, , text
        End If
        Close #f
        Return text
    Else
        Print "Unable to load " + file:Sleep:End
    End If   
End Function

Sub save(filename As String,p As String)
    Dim As Integer n
    n=Freefile
    If Open (filename For Binary Access Write As #n)=0 Then
        Put #n,,p
        Close
    Else
        Print "Unable to save " + filename:Sleep:End
    End If
End Sub

save("tester.txt","notepad " + chr(34)+ rtrim(command(0),"exe")+"bas"+chr(34))
dim as string L=load("tester.txt")
print L
shell L
Sleep
Kill "tester.txt" 
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How can i make a FB file find something from a text file?

Post by fxm »

Function that searches for a sub-string in a text file:
- If the sub-string is found, the function returns the corresponding line of text from the file (the first line where the sub-string is found).
- Otherwise (including file not found), a null string is returned.

Code: Select all

Function searchInFile(Byref file As String, Byref search As String) As String
    Dim As Long  f = Freefile
    Function = ""
    If Open (file For Input As #f) = 0 Then
        Dim As String text
        Do While Not EOF(f)
            Line Input #f, text
            If Instr(text, search) > 0 Then
                Function = Text
                Exit Do
            End If
        Loop
        Close #f
    End If
End Function
Post Reply