View Single Post
  #2   Spotlight this post!  
Unread 10-01-2010, 20:22
slavik262's Avatar
slavik262 slavik262 is offline
We do what we must because we can.
AKA: Matt Kline
FRC #0537 (Charger Robotics)
Team Role: Alumni
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Sussex, WI
Posts: 310
slavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to beholdslavik262 is a splendid one to behold
Send a message via AIM to slavik262
Re: Search for text in a file

There's two options. You can use the C++ standard library and read in a file with ifstream, or use a FILE* pointer and fopen() and read in the file the old-fashioned way with cstdio. If you need an explanation on how to open and read a file, feel free to post here again or PM me. Once you've opened the file, you have to options.

1. You can read the entire text file all at once into a memory buffer and then search for your text using strstr(), which returns a pointer to the start of the sub-string you searched for.

1. Alternatively, you can read in the file line at a time (using gets() with FILE* pointers/fopen() and ifstream::getline() for ifstream) and search each line you load in with strstr().

If you're using FILE* pointers, don't forget to call fclose() on the file when you're done.

As I said, feel free to let me know if you need any more help.
__________________
Reply With Quote