read a file and find text

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Ralf.

Hello i am new and finally i got purebasic to work on my computer.
how do i find text in a file (even if it exist a few times in the
text) and display it on the windows. Since PureBasic is different
to other basic dialekts i need some samples.
the file looks like this:
"start test (then carriage return)
B5100 ralf michelberger weinstadt [url]mailto:ralf.michelberger@gmx.net[/url] (carriage return)
B5100 hein test stuttgart [url]mailto:hein.test@gmx.net[/url] (carriage return)
B5100 manfred help purecity [url]mailto:mhelp@aol.com[/url] (carriage return)
this is it" (carriage return)

So how can i display each line, which starts with B5100 on the screen or on a
printer. For each B5100 one page.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Shawn.

Hi.

Here is one example below: (I would appreciate comments from the veterans)

Code: Select all

;initialize variables
FileName$ = "C:\PBExamples\filetoread.txt"
RowPos.w = 0 ;used for drawing text
StringToFind$ = "B5100" 
 
;open a window and set the background colour
If OpenWindow(0,200,200,600,400,#PB_Window_SystemMenu, "Test Output")  0
  StartDrawing(WindowOutput())
  FrontColour(255,255,223)
  Box(0,0,600,400)
  ;set the text colour
  FrontColour(0,0,0)
  BackColour(255,255,223)
 
  ;open the file for reading
  ;read each line
  ;test the line to see if it starts with the string you want
  ;if so, then print it to the window
 
   If ReadFile(0, FileName$)
    While Eof(0) = 0
      Text$ = ReadString() 
      If Left(Text$, Len(StringToFind$)) = StringToFind$
        RowPos.w = RowPos.w + 20
        Locate(20,RowPos.w)
        DrawText(Text$) 
      EndIf
    Wend
    CloseFile(0)
  EndIf
  StopDrawing() 
 
   ;wait until the user closes the window
  Repeat
    ev.l=WaitWindowEvent()
  Until ev=#PB_EventCloseWindow
EndIf


BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.
Text$ = ReadString()
If Left(Text$, Len(StringToFind$)) = StringToFind$
No veteran of Pure but... Try something simpler for both output and finding such as:

If OpenConsole()
Text$ = ReadString()
FoundPos = FindString(SearchString$, StringToFind$, StartPosition)
Found$ = Mid(SearchString$, FoundPos, Len(StringToFind$))
PrintN(Found$)
Temp$ = Input(); Wait for user to press any key to continue
CloseConsole; Closes the console after user has pressed key

Opening a console is simpler to display data, no need to compute the printing positions really. And FindString allows you to find a string in any position, not just at the start.

Regards Fang

Fangles
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Shawn.

Opening a console is simpler to display data, no need to compute the printing positions really. And FindString allows you to find a string in any position, not just at the start.
Thanks - much simpler.

I originally could not get the console to work. My problem was that I had changed Compiler Options Executable format to "Console". After some time, I changed the Compiler Options back to "Windows" and got the console to work.

I have amended the example with Fang's improvements.

(We still haven't tackled the printing question. Ralf, do you still need to print? If so, please describe again the printing requirements.)

Shawn

Code: Select all

;initialize variables
FileName$ = "C:\PBExamples\filetoread.txt"
StringToFind$ = "B5100" 
 
If OpenConsole()
  If ReadFile(0, FileName$)
    While Eof(0) = 0
      Text$ = ReadString()
      FoundPos = FindString(Text$, StringToFind$, 1) 
      If FoundPos  0
        PrintN(Text$)
      EndIf
    Wend
    CloseFile(0)
    Temp$ = Input(); Wait for user to press any key to continue
    CloseConsole(); Closes the console after user has pressed key
  EndIf
EndIf
 
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Ralf.

Hallo and thanks for all the answers. i will try them.
to the questions for the output to printer ?
It's still there the need too print each line that starts with 'B51000' on a
eparat page like this :
Name : Ralf Michelberger
City : Weinstadt-Endersbach
EMAIL: [url]mailto:ralf.michelberger@gmx.net[/url]

then comes the next page:
NAME: Hein Test
City: ......
EMAIL: ....

and so on until the last line (page).
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Ralf.

Hello,
with your help i developed a little bit further in the programm.
But now i am stucked again. Can you still help me ?
1.) How can i print the result on on printer (for each line that start with "B51000" one page).
2.) How can i programm the variable "Verwendungszweck" when this field is a varaible file (
it could have 27 digits until 15*27 digits). all other fields in this line are fix (like you can see in the following
programm):
For your help i would be very thankful:
OpenConsole()
Print ("Bitte, Dateiname eingeben: ") ; Ask for Filename
FileName$=Input()

StringToFind$ = "B51000"

; If OpenConsole()
If ReadFile(0, FileName$)
While Eof (0) = 0
Text$ = ReadString()

BLZ$ = Mid(Text$,7,8)
KontoNr$ = Mid(Text$,15,10)
Empfaenger$ = Mid(Text$,25,53)
Betrag$ = Mid(Text$,79,10)
Verwendungszweck$ = Mid(Text$,90,27) ;is variable from 27 to 15*27 digits
Bankverbindung$ = Right(Text$,30) ;is at the end of the string - 30 digits

FoundPos = FindString(Text$, StringToFind$, 1)

If FoundPos 0
;PrintN (Text$)
;OpenFile(1,"LPT1:")
PrintN ("")
Print ("Bankleitzahl : ")
PrintN (BLZ$)
Print ("Konto-Nr. : ")
PrintN (KontoNr$)
Print ("Empfaenger : ")
PrintN (Empfaenger$)
Print ("Betrag : ")
PrintN (Betrag$)
Print ("Verwendungszweck : ")
PrintN (Verwendungszweck$)
Print ("Bankverbindung : ")
PrintN (Bankverbindung$)
;CloseFile(1)
EndIf

Wend
CloseFile(0)
Temp$ = Input();
CloseConsole();
EndIf
; EndIf
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Shawn.
1.) How can i print the result on on printer (for each line that start with "B51000" one page).
Hi.

To print on the printer, you can use OpenFile, but should use the WriteStringN()function instead. I replaced your PrintN with WriteStringN for the demo below. Sending a Form Feed character, Chr(12), should take care of the page breaks.

Code: Select all

 
;print 2 pages as an example
If OpenFile(1,"LPT1:")
  WriteStringN ("")
  WriteStringN ("Bankleitzahl : ")
  WriteStringN ("Konto-Nr. : ")
  WriteStringN ("Empfaenger : ")
  WriteStringN ("Betrag : ")
  WriteStringN ("Verwendungszweck : ")
  WriteStringN ("Bankverbindung : ")
  WriteStringN (Chr(12)) 
  WriteStringN ("")
  WriteStringN ("Bankleitzahl : ")
  WriteStringN ("Konto-Nr. : ")
  WriteStringN ("Empfaenger : ")
  WriteStringN ("Betrag : ")
  WriteStringN ("Verwendungszweck : ")
  WriteStringN ("Bankverbindung : ")
  WriteStringN (Chr(12)) 
  CloseFile(1)
EndIf
I am not sure I understand about the data. Could you include a sample?

Shawn
Post Reply