Read text File and a word

New to FreeBASIC? Post your questions here.
Post Reply
Löwenherz
Posts: 46
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Read text File and a word

Post by Löwenherz »

Hello

I am looking for the right way to search a word in a Text File Here its a poem

Fürst Part is OK Second one Not
Never tried before to use Input and Open File

Code: Select all

#Include Once "windows.bi"
' alleswirdgut2.txt

Function GrabLine(file As string, lin As Integer) As String
	Open file For Input As #1
	Dim txt As String, i As Integer
	For i = 1 To lin
		Line Input #1, txt
	Next
	Close #1
	Return txt
End Function

Dim lin As Integer, i As integer
dim txt As string

'ask the user which line to get
Input "which line?: ",lin

txt=GrabLine("alleswirdgut2.txt",lin) 'myfile

If txt = "" Then
	Print "that line doesnt exist!"
Else
	Print txt
EndIf
'----------------------- first part ok --------------- /

'Sleep
Dim searchstring As String
searchstring ="alleswirdgut2.txt"
' Prompt the user for the search string
Print "Enter search string: "
' Read the file line by line

Input "which word?: ", searchstring

Do Until Eof(1)
  Line Input #1, searchstring
' Check if the line contains the search string
  If InStr(searchstring,"alles") Then
    ' Print the line if it matches the search string
    Print searchstring
  End If
Loop

Print searchstring

' Close the file
Close #1

' Wait for user input before exiting
Print "Press any key to exit..."
Sleep


[Code]
Last edited by Löwenherz on Mar 15, 2024 19:55, edited 1 time in total.
Lothar Schirm
Posts: 438
Joined: Sep 28, 2013 15:08
Location: Germany

Re: Ready Text File and a word

Post by Lothar Schirm »

Store your text line by line into an array and use "InStr" to search a word.
Löwenherz
Posts: 46
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Read text File and a word

Post by Löwenherz »

Thank you Lothar for the Tip :)

Now my example is running and Works perfect

Code: Select all

#Include Once "windows.bi"

Function GrabLine(file As String, lin As Integer) As String
    Open file For Input As #1
    Dim txt As String, i As Integer
    For i = 1 To lin
        Line Input #1, txt
    Next
    Close #1
    Return txt
End Function

Dim lin As Integer, i As Integer
Dim txt As String

' Ask the user which line to get
Input "Which line?: ", lin

txt = GrabLine("alleswirdgut2.txt", lin)

If txt = "" Then
    Print "That line doesn't exist!"
Else
    Print txt
End If

Dim searchWord As String
Dim lineArray(100) As String ' Assuming a maximum of 100 lines in the file
Dim lineNumber As Integer

Input "Enter search word: ", searchWord

' Open the file
Open "alleswirdgut2.txt" For Input As #1

' Read the file line by line and store in the array
lineNumber = 0
Do Until Eof(1)
    lineNumber = lineNumber + 1
    Line Input #1, lineArray(lineNumber)
Loop

' Close the file
Close #1

' Search for the word in the array
For i = 1 To lineNumber
    'If InStr(1, lineArray(i), searchWord, 1) Then
    If InStr(1, lineArray(i), searchWord) Then
    	
        Print "Word found in line "; i; ": "; lineArray(i)
    End If
Next

' Wait for user input before exiting
Print "Press any key to exit..."
Sleep
I must learn again to handle the freebasic language but I Like it
Last edited by Löwenherz on Mar 15, 2024 19:55, edited 1 time in total.
Löwenherz
Posts: 46
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Read text File and a word

Post by Löwenherz »

I Made this new examples as a dictionary Test but there are four five Problem zones too I suppose Help is Welcome thx Löwenherz

Code: Select all

'
'-- only a test for a dictionary
'-- load a poem and translate it to another language
'
#INCLUDE ONCE "windows.bi"

TYPE Translation
    sourceWord AS STRING
    targetWord AS STRING
END TYPE

DIM SHARED AS STRING WORDS( 200 )
Dim dictionaryFile AS STRING
Dim sourceWord AS STRING
Dim translations() AS Translation
DIM translatedWord AS STRING


SUB SPLIT( a AS STRING , cut AS STRING = " ")
  DIM i AS INTEGER , tel AS INTEGER
  i = 1
  tel = 0
  WHILE INSTR( a , cut ) <> 0
    i = INSTR( a , cut )
    WORDS( tel ) = LEFT( a , i )
    tel = tel + 1
    a = MID( a , i + 1 )
  WEND
  WORDS( tel ) = a
END SUB

' Load one dictionary from  a file
SUB LoadDictionary(BYVAL filePath AS STRING)
    DIM fileNum AS INTEGER
    DIM LINES AS STRING
    DIM fields() AS STRING
    DIM numTranslations AS INTEGER

    fileNum = FREEFILE
    OPEN filePath FOR INPUT AS fileNum

    ' Count the number of translations in the file
    numTranslations = 0
    DO WHILE NOT EOF(fileNum)
        LINE INPUT #fileNum, LINES
        numTranslations = numTranslations + 1
    LOOP

    ' Reset file pointer
    SEEK fileNum, 1

    ' Read translations into the dictionary array

    ' -------- problemzone solved --------------
    REDIM translations(numTranslations-1) AS STRING 'ok
    numTranslations = 0
    DO WHILE NOT EOF(fileNum)
        LINE INPUT #fileNum, LINES
        DIM fields() AS STRING
        SPLIT (LINES, "|")

        ' -------- problemzone 1 + 2 --------------
        translations(numTranslations).sourceWord = Trim(WORDS(0)) 'not ok
        translations(numTranslations).targetWord = Trim(WORDS(1)) 'not ok
        numTranslations = numTranslations + 1
    LOOP

    CLOSE #fileNum
END SUB

' Perform translation

' function problemzone 3 --------- /
' byref dictionary() as translation not ok
'
FUNCTION Translate(BYVAL sourceWord AS STRING, BYREF dictionary() AS Translation) AS STRING 'dictionary()
    DIM i AS INTEGER

    FOR i = 0 TO UBOUND(dictionary)
        IF LCase(sourceWord) = LCase(dictionary(i).sourceWord) THEN
            Translate = dictionary(i).targetWord
            EXIT FUNCTION
        END IF
    NEXT

    ' If translation not found, return the source word itself
    Translate = sourceWord
END Function

' Ask user for dictionary file path
INPUT "Enter path to dictionary file: ", dictionaryFile

' Load dictionary
LoadDictionary dictionaryFile

' Ask user for word to translate
INPUT "Enter the word to translate (English to German): ", sourceWord

' Translate word
'---------------- problemzone 4 ------------------- /
translatedWord = Translate(sourceWord, translations) ' not ok

' Display translation
PRINT "Translated word: "; translatedWord

' Wait for user input before exiting
PRINT "Press any key to exit..."
SLEEP
' ends
Updated code
fxm
Moderator
Posts: 12132
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Read text File and a word

Post by fxm »

I only fixed syntax and functionality errors without optimizing the code.
Changed lines are followed by '' *****.

Code: Select all

'
'-- only a test for a dictionary
'-- load a poem and translate it to another language
'
'#INCLUDE ONCE "windows.bi"  '' *****

TYPE Translation
    sourceWord AS STRING
    targetWord AS STRING
END TYPE

DIM SHARED AS STRING WORDS( 200 )
Dim dictionaryFile AS STRING
Dim sourceWord AS STRING
Dim SHARED translations() AS Translation  '' *****
DIM translatedWord AS STRING


SUB SPLIT( a AS STRING , cut AS STRING = " ")
  DIM i AS INTEGER , tel AS INTEGER
  i = 1
  tel = 0
  WHILE INSTR( a , cut ) <> 0
    i = INSTR( a , cut )
    WORDS( tel ) = LEFT( a , i - 1)  '' *****
    tel = tel + 1
    a = MID( a , i + 1 )
  WEND
  WORDS( tel ) = a
END SUB

' Load one dictionary from  a file
SUB LoadDictionary(BYVAL filePath AS STRING)
    DIM fileNum AS INTEGER
    DIM LINES AS STRING
'    DIM fields() AS STRING  '' *****
    DIM numTranslations AS INTEGER

    fileNum = FREEFILE
    OPEN filePath FOR INPUT AS fileNum

    ' Count the number of translations in the file
    numTranslations = 0
    DO WHILE NOT EOF(fileNum)
        LINE INPUT #fileNum, LINES
        numTranslations = numTranslations + 1
    LOOP

    ' Reset file pointer
    SEEK fileNum, 1

    ' Read translations into the dictionary array

    ' -------- problemzone solved --------------
    REDIM translations(numTranslations-1) AS Translation 'ok  '' *****
    numTranslations = 0
    DO WHILE NOT EOF(fileNum)
        LINE INPUT #fileNum, LINES
'        DIM fields() AS STRING  '' *****
        SPLIT (LINES, "|")

        ' -------- problemzone 1 + 2 --------------
        translations(numTranslations).sourceWord = Trim(WORDS(0)) 'not ok
        translations(numTranslations).targetWord = Trim(WORDS(1)) 'not ok
        numTranslations = numTranslations + 1
    LOOP

    CLOSE #fileNum
END SUB

' Perform translation

' function problemzone 3 --------- /
' byref dictionary() as translation not ok
'
FUNCTION Translate(BYVAL sourceWord AS STRING, dictionary() AS Translation) AS STRING 'dictionary()  '' *****
    DIM i AS INTEGER

    FOR i = 0 TO UBOUND(dictionary)
        IF LCase(sourceWord) = LCase(dictionary(i).sourceWord) THEN
            Translate = dictionary(i).targetWord
            EXIT FUNCTION
        END IF
    NEXT

    ' If translation not found, return the source word itself
    Translate = sourceWord
END Function

' Ask user for dictionary file path
INPUT "Enter path to dictionary file: ", dictionaryFile

' Load dictionary
LoadDictionary dictionaryFile

' Ask user for word to translate
INPUT "Enter the word to translate (English to German): ", sourceWord

' Translate word
'---------------- problemzone 4 ------------------- /
translatedWord = Translate(sourceWord, translations()) ' not ok  '' *****

' Display translation
PRINT "Translated word: "; translatedWord

' Wait for user input before exiting
PRINT "Press any key to exit..."
SLEEP
' ends
Löwenherz
Posts: 46
Joined: Aug 27, 2008 6:26
Location: Bad Sooden-Allendorf, Germany

Re: Read text File and a word

Post by Löwenherz »

Many thanks fxm tested all is running fine :)
Löwenherz
Post Reply