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

17y ago

What else can I help you with?

Continue Learning about Engineering

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 VB script to sort a string in ascending order?

uitihyiuiphoiphuihyi


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 print a .prn file using vb code?

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


C plus plus program for searching a word from text file?

#include&lt;iostream&gt; #include&lt;fstream&gt; #include&lt;vector&gt; #include&lt;string&gt; std::string get_input_file() { while (true) { std::cout &lt;&lt; "Enter the input file name: "; std::string filename; std::getline (std::cin, filename); std::ifstream file (filename); if (file.is_open()) { file.close(); return filename; } std::cerr &lt;&lt; "The file could not be opened.\n" &lt;&lt; std::endl; } } std::string get_search_word() { while (true) { std::cout &lt;&lt; "Enter the word to search for: "; std::string search; std::cin &gt;&gt; search; if (search.size()) return search; std::cerr &lt;&lt; "The search word cannot be an empty string.\n" &lt;&lt; std::endl; } } std::vector&lt;size_t&gt; get_offsets(const std::string&amp; filename, const std::string&amp; search) { std::vector&lt;size_t&gt; offsets; std::ifstream file (filename); if (!file.is_open()) throw std::exception("The file could not be opened."); size_t offset = 0; while (!file.eof()) { size_t pos = 0; char c; while (!file.read(&amp;c, 1).eof() &amp;&amp; c!=search[pos]) ++offset; while (pos!=search.size()-1 &amp;&amp; !file.read(&amp;c, 1).eof() &amp;&amp; c==search[++pos]); if (pos==search.size()-1 &amp;&amp; c==search[pos]) offsets.push_back (offset); offset += pos; pos = 0; } file.close(); return offsets; } int main() { std::cout &lt;&lt; "Word Search\n" &lt;&lt; std::endl; std::string filename = get_input_file(); std::string search = get_search_word(); std::vector&lt;size_t&gt; offsets = get_offsets (filename, search); std::cout &lt;&lt; "The word was found " &lt;&lt; offsets.size() &lt;&lt; " time" &lt;&lt; (offsets.size()==1?"":"s") &lt;&lt; ".\n" &lt;&lt; std::endl; if (offsets.size()) { std::cout &lt;&lt; "Offsets:\n"; for (auto off : offsets) std::cout &lt;&lt; off &lt;&lt; std::endl; } }

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 utility is used to search for a particular string within a file or group of files in linux?

Grep


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 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 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.


C plus plus program for searching a word from text file?

#include&lt;iostream&gt; #include&lt;fstream&gt; #include&lt;vector&gt; #include&lt;string&gt; std::string get_input_file() { while (true) { std::cout &lt;&lt; "Enter the input file name: "; std::string filename; std::getline (std::cin, filename); std::ifstream file (filename); if (file.is_open()) { file.close(); return filename; } std::cerr &lt;&lt; "The file could not be opened.\n" &lt;&lt; std::endl; } } std::string get_search_word() { while (true) { std::cout &lt;&lt; "Enter the word to search for: "; std::string search; std::cin &gt;&gt; search; if (search.size()) return search; std::cerr &lt;&lt; "The search word cannot be an empty string.\n" &lt;&lt; std::endl; } } std::vector&lt;size_t&gt; get_offsets(const std::string&amp; filename, const std::string&amp; search) { std::vector&lt;size_t&gt; offsets; std::ifstream file (filename); if (!file.is_open()) throw std::exception("The file could not be opened."); size_t offset = 0; while (!file.eof()) { size_t pos = 0; char c; while (!file.read(&amp;c, 1).eof() &amp;&amp; c!=search[pos]) ++offset; while (pos!=search.size()-1 &amp;&amp; !file.read(&amp;c, 1).eof() &amp;&amp; c==search[++pos]); if (pos==search.size()-1 &amp;&amp; c==search[pos]) offsets.push_back (offset); offset += pos; pos = 0; } file.close(); return offsets; } int main() { std::cout &lt;&lt; "Word Search\n" &lt;&lt; std::endl; std::string filename = get_input_file(); std::string search = get_search_word(); std::vector&lt;size_t&gt; offsets = get_offsets (filename, search); std::cout &lt;&lt; "The word was found " &lt;&lt; offsets.size() &lt;&lt; " time" &lt;&lt; (offsets.size()==1?"":"s") &lt;&lt; ".\n" &lt;&lt; std::endl; if (offsets.size()) { std::cout &lt;&lt; "Offsets:\n"; for (auto off : offsets) std::cout &lt;&lt; off &lt;&lt; std::endl; } }