answersLogoWhite

0

This is a simple function which will read the file line by line and search each individual line until the target string is found, then return the line number it was found in. It will return zero if the string was not found.

This is a simple function whipped up to demonstrate the basic technique, it has not been debugged or tested at all, and is certainly not the best solution possible. It also lacks any form of error trapping/handling.

You should not use this code in any program intended for production without rewriting it.

public function FindStringInFile(byval strTarget as string) as long

Dim iFile as integer 'file number

Dim iLineNum as long 'counter for line number

Dim strTmp as string 'variable length string to hold string from file

Dim bFound as boolean 'flag to indicate it was found

'First, open the file for input:

iFile = freefile 'get the next free file number

open "filename.txt" for input as #iFile 'open the file in input mode

do until eof(ifile) 'continue reading lines until the end of the file

line input #ifile, strTmp 'read a line into the string variable iLineNum = iLineNum + 1 'increment the line number counter

if instr$(strTarget, strTmp) <> 0 then 'set the flag and exit the loop

bFound = true

exit do

end if

loop

'check the flag

if bFound then

'found, return the line number

FindStringInFile = iLineNum

else

'not found, return zero

FindStringInFile = 0

end if

end function

User Avatar

Wiki User

18y ago

What else can I help you with?

Related Questions

How do you create a string and reverse a string in vb?

gov


What is a string of characters associated with a file to help find a document?

The Search Key


Write a program to implement the various operations on string such as length of string concatenation reverse of a string copy of a string to another in VB?

what is string


What is a Str function in VB?

Str converts a number to a string.


What is a VB script to sort a string in ascending order?

uitihyiuiphoiphuihyi


What is the advantages of vb over c or c?

vb called visual basic where as c the third letter of english alphabet so the basic advantage is the spelling of vb is very large as compared to c although both are bekar language but still attendence demands completion of file n completion of file demands a good friend who helps in writing a file and for helping that friend we have to search such a "goody goody" questions like this so it is the major disadvantage of vb practical over c practical.


What utility is used to search for a particular string within a file or group of files in linux?

Grep


What is the extension name of Visual Basic file?

.vb


Vb Codes to save a file and vb codes for a web browser?

Try using Microsoft internet controlin vb. It is in the components list. Its is very easy to use.


How do you declare the text in vb?

In Visual Basic (VB), you can declare a text variable using the Dim statement followed by the variable name and the As String type. For example: Dim myText As String. You can then assign a value to it, like myText = &quot;Hello, World!&quot;. This allows you to store and manipulate string data within your program.


How do you print a .prn file using vb code?

give print then select print to file give the file name that file may prn file by


How do you convert a pdf file into a doc file using VB code?

You will need a PDF server running somewhere that can be accessed by your VB code. It will need to be called from the code through the server and get the PDF file in return. I did that with Tweak PDF Converter, an pdf editor for converting pdf file to word form.

Trending Questions
What turbine trip is designed to occur at 10 percent above normal operation? Seminar topic for mechanical engineering? How do you live in underwater biodome? Hydraulic directional valves what is the difference bet open center and closed center? What are the types of biasing in pn junction diode? How many paths can take in circuit B? Who designed the original java language and wrote Emacs for Unix systems? What is the approximate efficiency of a 60 cycle 6 pole induction motor running at 1050 rpm and having a synchronous speed of 1200 rpm? How do you adjust if flat belt conveyor moving towards left even after adjusting the take-up screw at tail end? Which operation is not possible in data structure? When was James the Red Engine created? What is 13 d n? What do a direct electrical current is different from an alternating current because only the direct electrical current means? What is the name of the pulley used to pull a bucket out of a water well? What is concept of time? Write a complete well-documented C language program The program should repeatedly ask for another input of a non-negative real number ie X until a negative value is inputted and the program then? What happens if there are duplicate values in the array during a linear search? Syallbus for aieee 2009? Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store An Invoice should include four pieces of information as instance variables a part n? Why is the top of water heater extremely hot hot enough to melt the rubber red and blue bushings around the in out plumbing?