Searching a .txt file?
Hi, I was wondering how I could search a .txt file for a certain string. What I mean is this, if I have a string:
and I I have a .txt file that contains the following:
the program would search the file and compare each line. It would be like this: if somestring equals first line, say "somestring is in the .txt file!", else go to the next line. If somestring equals the second line....etc. Does anyone know how to do this? In other words, I want to know if a string is in a .txt file or not. Thanks in advance! :D
You said it in your own post. Open up the file, start reading each line one at a time. Then compare the read line with your desired string. If it is the same, output some message or whatever. Otherwise keep reading. You can find some good File IO tutorials on this web site, just look around. Now if you have a specific implementation detail I'd be glad to help.
You can also read the data into a buffer and then search it with string-handling functions (strstr, et al).