You are not logged in. You can browse in the PowerBASIC Community, but you must click Login (top right) before you can post. If this is your first visit, check out the FAQ or Sign Up.
hi,,
assume i have a text file with many lines of information.
how can i search the file for characters.
e.g. I want to find 2222 which is part of SEARCHID2222
??
Open the file in binary mode to a string, the search the string with instr ....
thanks
but INSTR gives the first occurrence of the character. If i need to look for second occurrence,i have to save the position and start searching again starting from the saved position again?? or there is another method?
but INSTR gives the first occurrence of the character. If i need to look for second occurrence,i have to save the position and start searching again starting from the saved position again?? or there is another method?
That's about right. Search for the string using InStr and if found then increment your starting search offet by the length of the search string. Something like (not tested, this is straight from my brain):
Code:
nBufferSize = Len(sBuffer)
nOffSet = 1
Do Until nOffSet > nBufferSize
f = InStr( nOffset, sBuffer, sSearchString )
If f Then
' You've found a match... do what you want to do here
' increment the offset to keep searching from
nOffSet = f + Len(sSearchString)
Else
Exit Do
End If
Loop
Paul Squires
FireFly Visual Designer (for PowerBASIC Windows 10+)
Version 3 now available. http://www.planetsquires.com
If it is a text file, then quite possibly you will want to know some other contents of the line containing the target data, and/or the line number, as well as its absolute position within the file.
So having loaded the file into a single string, you can use PARSE$ to transform it into a string array, then either search each string in the array, or having established the target position in the single string, subtract the lengths of each string in the array until the line number is known. If the line number is not required, then the text in which the target occurs can be extracted by winding backwards and forwards from the target position to the nearest line delimiter ($CRLF, probably), or to the limits of the string.
Chris is correct the various PARSE functions and statements are probably the fastest depending on what you are trying to determine. Here is a very simple example (appologies for the simple variable names). I suspect PARSECOUNT and TALLY from Rodney are basically the same code except PARSECOUNT adds 1 to the count
Code:
#COMPILE EXE
FUNCTION PBMAIN () AS LONG
f& = FREEFILE
OPEN "textfile" FOR BINARY AS f&
GET$ f$, LOF(f&), t$ 'convert the whole file to a single string t$
c& = PARSECOUNT(t$, "2222") 'returns the number of occurances plus 1
REDIM ta(1 TO c&) AS STRING
PARSE t$, ta$(), "2222" 'will give an array of strings broken by each
'occurance of 2222 with the 2222 actualy removed
'Note the reason the count is plus 1 is because the last count will be for
' the remainder of the string or an empty string if the file ennded with 2222
END FUNCTION
'RemoteFiles may look like "Text1.txt|Txt2.txt|txt4.txt|"
FileCount = Tally(RemoteFiles,"|")
For lLoop = 1 to FileCount
FileSpec = Parse$(RemoteFiles,"|",lLoop)
Do whatever to string/file
Next
Function CCSGetDirListing(InputDir As String) Export As String
Local lCount As Long
Local lLoop As Long
Local FileSpec As String
Local Extension As String
Local FileList As String
Local f As Asciiz * %MAX_PATH
Local FindData As WIN32_FIND_DATA
Local hDir As Long
ChDir InputDir
Extension = "log"
FindData.dwFileAttributes = %FILE_ATTRIBUTE_DIRECTORY
f = "*.*" 'Read all files, filter later
hDir = FindFirstFile(f, FindData)
If hDir = %INVALID_HANDLE_VALUE Then
Function = "Unale to read directory!"
Exit Function
End If
Do
Select Case Left$(FindData.cFileName,1)
Case "." 'Ignore these directories
Case "_" 'Front page directories
Case Else
'Verify the extention is what we are looking for:
FileSpec = FindData.cFileName
'At this point we have a valid filename
Incr lCount
If IsTrue Len(FileList) Then
FileList = FileList & "|" & FileSpec 'Add Pipe so we can parse on it later
Else
FileList = FileSpec
End If
End Select
Loop While FindNextFile(hDir, FindData)
FindClose hDir
Function = FileList
End Function
Scott Turchin
MCSE, MCP+I http://www.tngbbs.com
---------------------- True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, and to analyze site activity. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.
Comment